-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_cli.py
More file actions
54 lines (46 loc) · 2.02 KB
/
Copy path_cli.py
File metadata and controls
54 lines (46 loc) · 2.02 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
"""Launch the version-locked Yield runtime carried by this wheel."""
from __future__ import annotations
import os
import shutil
import subprocess
import sys
from pathlib import Path
from typing import NoReturn, Sequence
def runtime_path(platform: str | None = None) -> Path:
"""Return the wheel-local runtime or fail without checking PATH."""
selected = platform or sys.platform
name = "yskill.exe" if selected == "win32" else "yskill"
path = Path(__file__).with_name("_runtime") / name
if not path.is_file():
raise FileNotFoundError(
f"the packaged Yield runtime is missing at {path}; reinstall yieldskill"
)
return path
def run(argv: Sequence[str] | None = None, platform: str | None = None) -> int | NoReturn:
"""Replace this process on Unix; preserve the child exit code on Windows."""
args = [str(runtime_path(platform)), *(argv if argv is not None else sys.argv[1:])]
selected = platform or sys.platform
python = os.environ.get("YIELD_PYTHON", sys.executable)
selected_python = shutil.which(python) or python
# Keep the path used to enter the virtual environment. Resolving this
# symlink would replace .venv/bin with the system interpreter directory,
# hiding Python and environment-local console scripts from RunCommand.
python_bin = str(Path(selected_python).absolute().parent)
inherited_path = os.environ.get("PATH", "")
environment = {
**os.environ,
"YIELD_LANGUAGE": os.environ.get("YIELD_LANGUAGE", "python"),
"YIELD_PYTHON": python,
"PATH": python_bin + (os.pathsep + inherited_path if inherited_path else ""),
}
if selected == "win32":
return subprocess.run(args, check=False, env=environment).returncode
os.execve(args[0], args, environment)
raise AssertionError("os.execve returned")
def main() -> NoReturn:
try:
code = run()
except FileNotFoundError as error:
print(f"yskill: {error}", file=sys.stderr)
raise SystemExit(1) from error
raise SystemExit(code)