1+ #!/usr/bin/env python3
2+ """
3+ Script to verify Python version compatibility with project requirements.
4+ This helps identify packages that might not be compatible with specific Python versions.
5+ """
6+
7+ import sys
8+ import subprocess
9+ import tempfile
10+ import os
11+ import platform
12+ from pathlib import Path
13+
14+ def check_requirements (requirements_file ):
15+ """Test if all packages in the requirements file can be installed"""
16+ print (f"Checking compatibility of { requirements_file } with Python { platform .python_version ()} " )
17+
18+ with tempfile .TemporaryDirectory () as tmpdir :
19+ # Create a virtual environment in the temp directory
20+ venv_dir = os .path .join (tmpdir , "venv" )
21+ subprocess .run ([sys .executable , "-m" , "venv" , venv_dir ], check = True )
22+
23+ # Determine pip path
24+ if sys .platform .startswith ('win' ):
25+ pip_path = os .path .join (venv_dir , "Scripts" , "pip" )
26+ else :
27+ pip_path = os .path .join (venv_dir , "bin" , "pip" )
28+
29+ # Upgrade pip
30+ subprocess .run ([pip_path , "install" , "--upgrade" , "pip" ], check = True )
31+
32+ # Test installing the requirements
33+ try :
34+ subprocess .run (
35+ [pip_path , "install" , "-r" , requirements_file ],
36+ check = True ,
37+ capture_output = True ,
38+ text = True
39+ )
40+ print (f"✅ All packages in { requirements_file } are compatible with Python { platform .python_version ()} " )
41+ return True
42+ except subprocess .CalledProcessError as e :
43+ print (f"❌ Some packages in { requirements_file } are NOT compatible with Python { platform .python_version ()} " )
44+ print ("Error details:" )
45+ print (e .stdout )
46+ print (e .stderr )
47+ return False
48+
49+ def main ():
50+ """Main function"""
51+ proj_root = Path (__file__ ).parent .parent
52+
53+ # Check both requirements files
54+ req_files = [
55+ proj_root / "requirements.txt" ,
56+ proj_root / "requirements-dev.txt"
57+ ]
58+
59+ success = True
60+ for req_file in req_files :
61+ if req_file .exists ():
62+ if not check_requirements (req_file ):
63+ success = False
64+
65+ return 0 if success else 1
66+
67+ if __name__ == "__main__" :
68+ sys .exit (main ())
0 commit comments