-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_py_ver.py
103 lines (76 loc) · 2.35 KB
/
run_py_ver.py
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
"""Run in python version."""
import os
import sys
import typing
from pathlib import Path
import automata
import print_site
import setup_py_env
is_nt = os.name == "nt"
def _set_py_env(
PR: Path, worklist: typing.List[typing.List[typing.Any]]
) -> typing.Dict[str, str]:
"""Set python env."""
print("\nset_py_env")
# modify copy of env
env = os.environ.copy()
for job in worklist:
ename = job[0]
vallist = job[1:]
# update the env
fpath = os.pathsep.join([str(val) for val in vallist])
automata.prepend_path(env, ename, fpath)
# report result
print_site.print_env(env, ename)
return env
def set_unix_py_env(PR: Path) -> typing.Dict[str, str]:
"""Set unix python environment."""
# assume python 3.8
pyvdir = "python3.8"
worklist = [
["PATH", PR / "bin"], # exe
["LD_LIBRARY_PATH", PR / "lib"], # .so
["PYTHONPATH", PR / "lib" / pyvdir, PR / "lib"],
]
return _set_py_env(PR, worklist)
def set_win_py_env(PR: Path) -> typing.Dict[str, str]:
"""Set windows python environment."""
# Python 3.8 set LoadLibrary for DLLS to IGNORE PATH
# only dlls alongside pyd...
worklist = [
["PATH", PR / "Scripts", PR], # .exe
["PYTHONPATH", PR / "lib", PR / "DLLs"], # pyd
]
return _set_py_env(PR, worklist)
def run_script(PR: Path, SCRIPT: str, ARGS: typing.List[str]) -> None:
"""Run a script in python environment."""
print("run_py_ver")
print(f"PYTHONROOT={PR}")
print(f"SCRIPT={SCRIPT}")
# spawn python child env with corrected env
pyexe = setup_py_env.pyexe(PR)
childenv = set_unix_py_env(PR) if not is_nt else set_win_py_env(PR)
automata.sp_run([pyexe.as_posix(), "-s", SCRIPT] + ARGS, env=childenv)
def usage() -> str:
"""Return usage string."""
msg = f"Usage: python {__file__} python_root script_path"
print(msg)
sys.exit(-1)
def main(argv: typing.List[str] = None) -> int:
"""Run Main."""
if argv is None:
argv = sys.argv
if len(argv) < 3:
usage()
# caller to pass pyroot
PR = Path(argv[1])
# and script name to run
SCRIPT = argv[2]
# now arguments to script
ARGS = []
if len(argv) > 2:
ARGS = argv[3:]
run_script(PR, SCRIPT, ARGS)
return 0
if __name__ == "__main__":
sys.exit(main())