Skip to content

Commit 660d81b

Browse files
baremetalgoCopilot
andcommitted
Make Windows runtime relocatable
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent fa61636 commit 660d81b

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

packaging/runtime_builder.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@
1111

1212

1313
ROOT = Path(__file__).resolve().parents[1]
14+
HOSTEDTOOLCACHE_MARKER = b"hostedtoolcache"
15+
16+
17+
def _copy_windows_runtime(runtime: Path) -> Path:
18+
"""Copy the complete Python installation so the runtime is self-contained."""
19+
source = Path(sys.base_prefix).resolve()
20+
if source == runtime or source in runtime.parents:
21+
raise RuntimeError(f"Cannot copy Python installation into itself: {source}")
22+
shutil.copytree(source, runtime, dirs_exist_ok=True)
23+
return runtime / "python.exe"
24+
25+
26+
def _validate_windows_runtime(runtime: Path, python: Path) -> None:
27+
"""Reject Windows runtimes that retain a hosted runner interpreter path."""
28+
candidates = [python, *runtime.glob("pyvenv.cfg"), *runtime.glob("*._pth")]
29+
candidates.extend(runtime.glob("*.pth"))
30+
for path in candidates:
31+
if not path.is_file():
32+
continue
33+
if HOSTEDTOOLCACHE_MARKER in path.read_bytes().lower():
34+
raise RuntimeError(
35+
f"Generated Windows runtime contains a hostedtoolcache reference in {path}. "
36+
"Use a self-contained Python installation instead of a hosted runner launcher."
37+
)
1438

1539

1640
def main() -> int:
@@ -19,13 +43,12 @@ def main() -> int:
1943
parser.add_argument("--gpu", action="store_true", help="Install the CUDA runtime selected by runtime_setup.py.")
2044
args = parser.parse_args()
2145
runtime = args.output.resolve()
22-
# Keep macOS/Linux defaults, but force a real Windows interpreter copy:
23-
# hosted runners may otherwise create a launcher tied to hostedtoolcache.
24-
venv_command = [sys.executable, "-m", "venv"]
2546
if sys.platform == "win32":
26-
venv_command.append("--copies")
27-
subprocess.run([*venv_command, str(runtime)], check=True)
28-
python = runtime / ("Scripts/python.exe" if sys.platform == "win32" else "bin/python")
47+
python = _copy_windows_runtime(runtime)
48+
_validate_windows_runtime(runtime, python)
49+
else:
50+
subprocess.run([sys.executable, "-m", "venv", str(runtime)], check=True)
51+
python = runtime / "bin/python"
2952
pip_check = subprocess.run([str(python), "-m", "pip", "--version"], check=False)
3053
if pip_check.returncode != 0:
3154
subprocess.run([str(python), "-m", "ensurepip", "--upgrade"], check=True)

0 commit comments

Comments
 (0)