Repository navigation
Expand file tree
/
Copy pathpython_runner_common.py
More file actions
113 lines (91 loc) · 3.75 KB
/
Copy pathpython_runner_common.py
File metadata and controls
113 lines (91 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
import importlib.util
import os
import sys
from types import ModuleType
def strip_extended_length_prefix(path: str) -> str:
r"""Strip a Windows ``\\?\`` extended-length (verbatim) prefix.
Verbatim drives (``\\?\C:``) read as different from ``C:`` under
``os.path.commonpath``, so cwd comparisons drop every source.
"""
if path.startswith("\\\\?\\UNC\\"):
return "\\\\" + path[len("\\\\?\\UNC\\") :]
if path.startswith("\\\\?\\"):
return path[len("\\\\?\\") :]
return path
def normalize_file_list(files: list[str]) -> list[str]:
unique: set[str] = set()
for file_path in files:
unique.add(os.path.abspath(strip_extended_length_prefix(file_path)))
return sorted(unique)
def resolve_module_info(in_file: str) -> tuple[str, list[str]]:
in_file = os.path.abspath(strip_extended_length_prefix(in_file))
module_dir = os.path.dirname(in_file)
module_name = os.path.splitext(os.path.basename(in_file))[0]
package_parts: list[str] = []
current = module_dir
while os.path.isfile(os.path.join(current, "__init__.py")):
package_parts.insert(0, os.path.basename(current))
current = os.path.dirname(current)
extra_paths = [module_dir]
if package_parts:
module_name = ".".join(package_parts + [module_name])
if current not in extra_paths:
extra_paths.append(current)
return module_name, extra_paths
def import_file(module_name: str, file_path: str, extra_paths: list[str]) -> ModuleType:
for extra_path in reversed(extra_paths):
if extra_path not in sys.path:
sys.path.insert(0, extra_path)
spec = importlib.util.spec_from_file_location(module_name, file_path)
if spec is None or spec.loader is None:
raise ImportError(f"Unable to load module spec for {file_path}")
sys.modules.pop(module_name, None)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
def purge_local_modules(cwd: str, preserve_modules: set[str] | None = None) -> None:
preserved = preserve_modules or set()
cwd_abs = os.path.abspath(cwd)
for module_name, module in list(sys.modules.items()):
if module_name in preserved:
continue
module_file = getattr(module, "__file__", None)
if not module_file:
continue
candidate = module_file[:-1] if module_file.endswith(".pyc") else module_file
candidate_abs = os.path.abspath(candidate)
if not os.path.isfile(candidate_abs):
continue
try:
common = os.path.commonpath([candidate_abs, cwd_abs])
except ValueError:
continue
if common == cwd_abs:
sys.modules.pop(module_name, None)
def collect_python_sources(cwd: str, input_file: str) -> list[str]:
sources: set[str] = set()
cwd = strip_extended_length_prefix(cwd)
input_abs = os.path.abspath(strip_extended_length_prefix(input_file))
sources.add(input_abs)
for module in list(sys.modules.values()):
module_file = getattr(module, "__file__", None)
if not module_file:
continue
candidate = module_file[:-1] if module_file.endswith(".pyc") else module_file
candidate_abs = os.path.abspath(candidate)
if not os.path.isfile(candidate_abs):
continue
if not candidate_abs.endswith(".py"):
continue
try:
common = os.path.commonpath([candidate_abs, cwd])
except ValueError:
continue
if common != cwd:
continue
sources.add(candidate_abs)
return sorted(sources)
def python_version() -> str:
return f"{sys.version_info.major}.{sys.version_info.minor}"