-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_python_compatibility.py
More file actions
324 lines (259 loc) · 11.5 KB
/
check_python_compatibility.py
File metadata and controls
324 lines (259 loc) · 11.5 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
"""
Python Package Compatibility Checker
=====================================
Checks if your installed Python packages support a specific Python version
by querying PyPI metadata.
Usage:
python check_python_compatibility.py [--version 3.14] [--full] [--save results.json]
Examples:
# Check for Python 3.14 support (sample of 50 packages)
python check_python_compatibility.py --version 3.14
# Full check of all packages (takes longer)
python check_python_compatibility.py --version 3.13 --full
# Save results to JSON file
python check_python_compatibility.py --version 3.14 --save results.json
Author: Generated via Warp AI
License: MIT
"""
import json
import subprocess
import sys
import urllib.request
import urllib.error
import argparse
from datetime import datetime
from typing import Dict, List, Tuple
class CompatibilityChecker:
"""Check Python package compatibility with specified Python version."""
def __init__(self, target_version: str = "3.14"):
self.target_version = target_version
self.target_major_minor = '.'.join(target_version.split('.')[:2])
def get_installed_packages(self) -> List[Dict[str, str]]:
"""Get list of all installed packages."""
result = subprocess.run(
[sys.executable, '-m', 'pip', 'list', '--format=json'],
capture_output=True,
text=True,
check=False
)
if result.returncode != 0:
print("Error: Could not get package list from pip")
return []
return json.loads(result.stdout)
def check_package_compatibility(self, package_name: str) -> Tuple[str, List[str]]:
"""
Check if a package supports the target Python version.
Returns:
Tuple of (status, python_versions)
status: "compatible", "likely", "incompatible", "unknown"
"""
try:
url = f'https://pypi.org/pypi/{package_name}/json'
req = urllib.request.Request(
url,
headers={'User-Agent': 'Python-Compatibility-Checker/1.0'}
)
with urllib.request.urlopen(req, timeout=5) as response:
data = json.loads(response.read())
# Get Python version classifiers
classifiers = data.get('info', {}).get('classifiers', [])
py_versions = [
c.split('::')[-1].strip()
for c in classifiers
if 'Programming Language :: Python ::' in c
and not 'Implementation' in c
]
# Check for target version support
has_target = any(self.target_major_minor in v for v in py_versions)
# Check for previous version
prev_minor = int(self.target_major_minor.split('.')[1]) - 1
prev_version = f"3.{prev_minor}"
has_previous = any(prev_version in v for v in py_versions)
if has_target:
return "compatible", py_versions
elif has_previous or not py_versions:
# If it supports previous version or no info, it might work
return "likely", py_versions
else:
return "incompatible", py_versions
except urllib.error.HTTPError as e:
if e.code == 404:
return "unknown", ["Package not found on PyPI"]
return "unknown", [f"HTTP Error: {e.code}"]
except Exception as e:
return "unknown", [f"Error: {str(e)}"]
def analyze_packages(self, packages: List[Dict[str, str]], full_check: bool = False) -> Dict:
"""
Analyze package compatibility.
Args:
packages: List of package dictionaries
full_check: If True, check all packages. If False, sample.
"""
total = len(packages)
check_count = total if full_check else min(50, total)
print(f"\n{'='*70}")
print(f"Python {self.target_version} Compatibility Check")
print(f"{'='*70}")
print(f"Total packages installed: {total}")
print(f"Checking: {check_count} packages")
print(f"Mode: {'FULL SCAN' if full_check else 'SAMPLE'}")
print(f"Started: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"{'='*70}\n")
results = {
'compatible': [],
'likely': [],
'incompatible': [],
'unknown': []
}
packages_to_check = packages if full_check else packages[:check_count]
for idx, pkg in enumerate(packages_to_check, 1):
name = pkg['name']
version = pkg['version']
if idx % 10 == 0 or idx == 1:
print(f"Progress: {idx}/{check_count} packages checked...")
status, py_versions = self.check_package_compatibility(name)
pkg_info = {
'name': name,
'version': version,
'python_versions': py_versions
}
results[status].append(pkg_info)
print(f"\nCompleted: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
return results
def print_results(self, results: Dict):
"""Print formatted results."""
total = sum(len(results[k]) for k in results)
print(f"\n{'='*70}")
print(f"RESULTS SUMMARY")
print(f"{'='*70}\n")
# Compatible packages
compatible = results['compatible']
print(f"✅ COMPATIBLE with Python {self.target_version}: {len(compatible)} packages")
if compatible:
print(f" ({len(compatible)/total*100:.1f}% of checked packages)")
for pkg in compatible[:10]:
print(f" • {pkg['name']:30} (v{pkg['version']})")
if len(compatible) > 10:
print(f" ... and {len(compatible)-10} more")
print()
# Likely compatible
likely = results['likely']
print(f"🟡 LIKELY COMPATIBLE: {len(likely)} packages")
if likely:
print(f" ({len(likely)/total*100:.1f}% of checked packages)")
print(f" (No explicit {self.target_version} support, but may work)")
for pkg in likely[:10]:
max_py = max(pkg['python_versions']) if pkg['python_versions'] else 'Unknown'
print(f" • {pkg['name']:30} (max confirmed: {max_py})")
if len(likely) > 10:
print(f" ... and {len(likely)-10} more")
print()
# Incompatible
incompatible = results['incompatible']
print(f"🔴 POTENTIALLY INCOMPATIBLE: {len(incompatible)} packages")
if incompatible:
print(f" ({len(incompatible)/total*100:.1f}% of checked packages)")
print(f" (May need updates before upgrading Python)")
for pkg in incompatible[:15]:
max_py = max(pkg['python_versions']) if pkg['python_versions'] else 'Unknown'
print(f" • {pkg['name']:30} (max: {max_py})")
if len(incompatible) > 15:
print(f" ... and {len(incompatible)-15} more")
print()
# Unknown
unknown = results['unknown']
if unknown:
print(f"❓ COULD NOT VERIFY: {len(unknown)} packages")
print(f" ({len(unknown)/total*100:.1f}% of checked packages)")
for pkg in unknown[:5]:
print(f" • {pkg['name']:30}")
if len(unknown) > 5:
print(f" ... and {len(unknown)-5} more")
print()
print(f"{'='*70}")
# Risk assessment
compat_percent = len(compatible) / total * 100
incompat_percent = len(incompatible) / total * 100
print(f"\n📊 RISK ASSESSMENT for Python {self.target_version} upgrade:\n")
if compat_percent >= 80:
print(" ✅ LOW RISK - Most packages support this Python version")
elif compat_percent >= 50:
print(" 🟡 MEDIUM RISK - Partial support, test thoroughly")
elif incompat_percent >= 30:
print(" 🔴 HIGH RISK - Many packages may not work")
else:
print(" ⚠️ UNCERTAIN - Limited compatibility data available")
print(f"\n Compatible: {compat_percent:.1f}%")
print(f" Incompatible: {incompat_percent:.1f}%")
print(f" Likely/Unknown: {(len(likely) + len(unknown))/total*100:.1f}%")
print(f"\n{'='*70}\n")
# Recommendations
print("💡 RECOMMENDATIONS:\n")
if incompat_percent > 20:
print(" 1. DO NOT upgrade Python yet")
print(" 2. Wait for package updates")
print(" 3. Check incompatible packages for updates")
elif incompat_percent > 10:
print(" 1. Test in isolated environment first")
print(" 2. Check critical packages manually")
print(" 3. Have rollback plan ready")
else:
print(" 1. Create test environment to verify")
print(" 2. Check for known issues with critical packages")
print(" 3. Upgrade non-critical environments first")
print(f"\n{'='*70}\n")
def save_results(self, results: Dict, filename: str):
"""Save results to JSON file."""
output = {
'timestamp': datetime.now().isoformat(),
'target_python_version': self.target_version,
'current_python_version': f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
'total_checked': sum(len(results[k]) for k in results),
'results': results,
'summary': {
'compatible': len(results['compatible']),
'likely': len(results['likely']),
'incompatible': len(results['incompatible']),
'unknown': len(results['unknown'])
}
}
with open(filename, 'w', encoding='utf-8') as f:
json.dump(output, f, indent=2)
print(f"✅ Results saved to: {filename}\n")
def main():
parser = argparse.ArgumentParser(
description='Check Python package compatibility with target Python version'
)
parser.add_argument(
'--version',
default='3.14',
help='Target Python version to check (e.g., 3.14, 3.13)'
)
parser.add_argument(
'--full',
action='store_true',
help='Check all installed packages (slower)'
)
parser.add_argument(
'--save',
metavar='FILE',
help='Save results to JSON file'
)
args = parser.parse_args()
# Initialize checker
checker = CompatibilityChecker(target_version=args.version)
# Get packages
packages = checker.get_installed_packages()
if not packages:
print("Error: No packages found")
sys.exit(1)
# Analyze
results = checker.analyze_packages(packages, full_check=args.full)
# Print results
checker.print_results(results)
# Save if requested
if args.save:
checker.save_results(results, args.save)
if __name__ == '__main__':
main()