-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstantiate.py
More file actions
executable file
·61 lines (49 loc) · 1.78 KB
/
instantiate.py
File metadata and controls
executable file
·61 lines (49 loc) · 1.78 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
#!/usr/bin/env python3
# This is "Portable Python Distro - the Instantiate script"
# Copyright 2025 Andreas Zwinkau
# SPDX-License-Identifier: Apache-2.0
import os
import shutil
import sys
from pathlib import Path
def main():
if len(sys.argv) == 1 or len(sys.argv) > 3:
sys.exit(
"Usage: instantiate.py <target-directory> <optional: requirements.txt>"
)
script_path = Path(sys.argv[0]) # lib/python3.12/x.py
base_python = script_path.parent.parent.parent.resolve()
target_dir = Path(sys.argv[1]).resolve()
assert base_python.is_dir(), base_python
# Create structure and hardlink binaries
(target_dir / "bin").mkdir(parents=True)
for binary in ["python3"]:
shutil.copyfile(base_python / "bin" / binary, target_dir / "bin" / binary)
os.chmod(target_dir / "bin" / binary, 0o755)
(target_dir / "bin/python").symlink_to("python3")
# Hardlink base libraries
_ = shutil.copytree(base_python / "lib", target_dir / "lib")
# Create pyvenv.cfg
(target_dir / "pyvenv.cfg").write_text(
f"home = {base_python / 'bin'}\ninclude-system-site-packages = false\n"
)
if len(sys.argv) == 3:
requirements_file = Path(sys.argv[2])
assert requirements_file.is_file(), requirements_file
import subprocess
cmd = [
target_dir / "bin" / "python3",
"-m",
"pip",
"install",
"-r",
requirements_file,
]
subprocess.run(cmd, check=True)
# clean up __pycache__ folders at target
for root, dirs, files in os.walk(target_dir):
for dir in dirs:
if dir == "__pycache__":
shutil.rmtree(os.path.join(root, dir))
if __name__ == "__main__":
main()