-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_vesper_env.py
More file actions
87 lines (69 loc) · 2.98 KB
/
setup_vesper_env.py
File metadata and controls
87 lines (69 loc) · 2.98 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
import os
import subprocess
import sys
def run_command(command, check=True):
"""
Runs a shell command and handles errors gracefully.
"""
try:
print(f"Running: {command}")
subprocess.run(command, shell=True, check=check, text=True)
except subprocess.CalledProcessError as e:
print(f"Error: Command '{command}' failed with exit code {e.returncode}")
sys.exit(1)
def patch_cstag_lazy_import():
"""
Patch cstag's to_pdf.py to lazy-import weasyprint only when needed.
"""
to_pdf_path = os.path.join(sys.prefix, "lib", "python3.9", "site-packages", "cstag", "to_pdf.py")
if not os.path.exists(to_pdf_path):
print(f"⚠️ cstag's to_pdf.py not found at {to_pdf_path}. Skipping patch.")
return
with open(to_pdf_path, "r") as f:
lines = f.readlines()
new_lines = []
import_removed = False
for line in lines:
if "from weasyprint import HTML" in line and not import_removed:
print("🔧 Removing top-level weasyprint import...")
import_removed = True
continue
new_lines.append(line)
for i, line in enumerate(new_lines):
if line.strip().startswith("def to_pdf("):
indent = ' ' * (len(line) - len(line.lstrip()) + 4)
new_lines.insert(i + 1, f"{indent}from weasyprint import HTML # lazy import\n")
print("✅ Injected lazy import inside `to_pdf()`.")
break
else:
print("❌ Couldn't find `def to_pdf()` in cstag. No changes made.")
return
with open(to_pdf_path, "w") as f:
f.writelines(new_lines)
print("🎉 Successfully patched cstag to lazy-load weasyprint.")
def main():
### ensure script is running in a virtual environment ###
if not hasattr(sys, 'real_prefix') and not (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix):
print("Please activate your virtual environment before running this script.")
sys.exit(1)
### Define installation path ###
env_bin = os.path.join(sys.prefix, "bin")
print(f"Virtual environment detected: {sys.prefix}")
print(f"Installing tools into: {env_bin}")
### Upgrade pip and install Python dependencies ###
run_command("pip install --upgrade pip")
run_command("pip install -r requirements.txt")
### Install Samtools ###
run_command("wget https://github.com/samtools/samtools/releases/download/1.16.1/samtools-1.16.1.tar.bz2")
run_command("tar -xvjf samtools-1.16.1.tar.bz2")
run_command("cd samtools-1.16.1 && ./configure --prefix=$VIRTUAL_ENV --disable-lzma --without-curses && make && make install && cd ..")
### Patch cstag's PDF import ###
patch_cstag_lazy_import()
### Clean up temporary files ###
run_command("rm -f samtools-1.16.1.tar.bz2")
run_command("rm -rf samtools-1.16.1")
### Verify installations ###
run_command(f"{env_bin}/samtools --version")
print("\nAll tools installed successfully!")
if __name__ == "__main__":
main()