1+ import os
2+ import subprocess
3+ import sys
4+
5+ def run_command (command , check = True ):
6+ """
7+ Runs a shell command and handles errors gracefully.
8+ """
9+ try :
10+ print (f"Running: { command } " )
11+ subprocess .run (command , shell = True , check = check , text = True )
12+ except subprocess .CalledProcessError as e :
13+ print (f"Error: Command '{ command } ' failed with exit code { e .returncode } " )
14+ sys .exit (1 )
15+
16+ def patch_cstag_lazy_import ():
17+ """
18+ Patch cstag's to_pdf.py to lazy-import weasyprint only when needed.
19+ """
20+ to_pdf_path = os .path .join (sys .prefix , "lib" , "python3.9" , "site-packages" , "cstag" , "to_pdf.py" )
21+
22+ if not os .path .exists (to_pdf_path ):
23+ print (f"⚠️ cstag's to_pdf.py not found at { to_pdf_path } . Skipping patch." )
24+ return
25+
26+ with open (to_pdf_path , "r" ) as f :
27+ lines = f .readlines ()
28+
29+ new_lines = []
30+ import_removed = False
31+ for line in lines :
32+ if "from weasyprint import HTML" in line and not import_removed :
33+ print ("🔧 Removing top-level weasyprint import..." )
34+ import_removed = True
35+ continue
36+ new_lines .append (line )
37+
38+ for i , line in enumerate (new_lines ):
39+ if line .strip ().startswith ("def to_pdf(" ):
40+ indent = ' ' * (len (line ) - len (line .lstrip ()) + 4 )
41+ new_lines .insert (i + 1 , f"{ indent } from weasyprint import HTML # lazy import\n " )
42+ print ("✅ Injected lazy import inside `to_pdf()`." )
43+ break
44+ else :
45+ print ("❌ Couldn't find `def to_pdf()` in cstag. No changes made." )
46+ return
47+
48+ with open (to_pdf_path , "w" ) as f :
49+ f .writelines (new_lines )
50+
51+ print ("🎉 Successfully patched cstag to lazy-load weasyprint." )
52+
53+ def main ():
54+ ### ensure script is running in a virtual environment ###
55+ if not hasattr (sys , 'real_prefix' ) and not (hasattr (sys , 'base_prefix' ) and sys .base_prefix != sys .prefix ):
56+ print ("Please activate your virtual environment before running this script." )
57+ sys .exit (1 )
58+
59+ ### Define installation path ###
60+ env_bin = os .path .join (sys .prefix , "bin" )
61+
62+ print (f"Virtual environment detected: { sys .prefix } " )
63+ print (f"Installing tools into: { env_bin } " )
64+
65+ ### Upgrade pip and install Python dependencies ###
66+ run_command ("pip install --upgrade pip" )
67+ run_command ("pip install -r requirements.txt" )
68+
69+ ### Install Samtools ###
70+ run_command ("wget https://github.com/samtools/samtools/releases/download/1.16.1/samtools-1.16.1.tar.bz2" )
71+ run_command ("tar -xvjf samtools-1.16.1.tar.bz2" )
72+ run_command ("cd samtools-1.16.1 && ./configure --prefix=$VIRTUAL_ENV --disable-lzma --without-curses && make && make install && cd .." )
73+
74+ ### Patch cstag's PDF import ###
75+ patch_cstag_lazy_import ()
76+
77+ ### Clean up temporary files ###
78+ run_command ("rm -f samtools-1.16.1.tar.bz2" )
79+ run_command ("rm -rf samtools-1.16.1" )
80+
81+ ### Verify installations ###
82+ run_command (f"{ env_bin } /samtools --version" )
83+
84+ print ("\n All tools installed successfully!" )
85+
86+ if __name__ == "__main__" :
87+ main ()
0 commit comments