-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_wheels_install.py
More file actions
285 lines (228 loc) · 9.85 KB
/
test_wheels_install.py
File metadata and controls
285 lines (228 loc) · 9.85 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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
"""
Test wheel installation script for CI workflows.
This script finds and installs wheels compatible with the current Python version,
verifying that wheel files are valid and platform-compatible.
It also checks wheels against exclude_list.yaml and removes incompatible ones.
Wheels are ZIP archives (PEP 427). pip opens them with the zipfile module; a
BadZipFile / "Bad magic number" error means the bytes on disk are not a valid
ZIP (truncated, corrupted, or not a wheel), not that ".whl" was mistaken for ".zip".
"""
from __future__ import annotations
import re
import subprocess
import sys
from pathlib import Path
from colorama import Fore
from _helper_functions import EXCLUDE_LIST_PATH
from _helper_functions import get_current_platform
from _helper_functions import print_color
from _helper_functions import should_exclude_wheel
from _helper_functions import wheel_archive_is_readable
from yaml_list_adapter import YAMLListAdapter
WHEELS_DIR = Path("./downloaded_wheels")
def get_python_version_tag() -> str:
"""Get the Python version tag (e.g., '311' for Python 3.11)."""
return f"{sys.version_info.major}{sys.version_info.minor}"
def get_platform_patterns() -> list[str]:
"""Get regex patterns for wheels compatible with current platform."""
platform = sys.platform
if platform == "win32":
return [r"-win_amd64\.whl$", r"-win32\.whl$", r"-any\.whl$"]
elif platform == "darwin":
return [r"-macosx_.*\.whl$", r"-any\.whl$"]
elif platform == "linux":
return [r"-manylinux.*\.whl$", r"-linux.*\.whl$", r"-any\.whl$"]
else:
# Unknown platform, only match universal wheels
return [r"-any\.whl$"]
def is_wheel_compatible(wheel_name: str, python_version: str) -> bool:
"""
Check if a wheel is compatible with the given Python version AND current platform.
Python version compatibility:
- cpXY-cpXY: exact Python version match (e.g., cp311-cp311 for Python 3.11 only)
- cpXY-abi3: stable ABI wheels (compatible with Python >= XY)
- py3: universal Python 3 wheels
- py2.py3: universal Python 2/3 wheels
Platform compatibility:
- Windows: win32, win_amd64, any
- macOS: macosx_*, any
- Linux: manylinux*, linux*, any
"""
current_version = int(python_version) # e.g., 311 for Python 3.11
# Check for abi3 wheels first - they have a minimum Python version requirement
abi3_match = re.search(r"-cp(\d+)-abi3-", wheel_name)
if abi3_match:
base_version = int(abi3_match.group(1)) # e.g., 38 or 311 (not 3.8 or 3.11)
# abi3 wheels work on Python >= base_version (using these integer tags)
if current_version >= base_version:
# Check platform compatibility
platform_patterns = get_platform_patterns()
return any(re.search(pattern, wheel_name) for pattern in platform_patterns)
return False
# Check Python version compatibility for non-abi3 wheels
python_patterns = [
rf"-cp{python_version}-cp{python_version}-", # Exact version match (cpXY-cpXY)
rf"-cp{python_version}-", # Fallback for other cpXY patterns
r"-py3-", # Universal Python 3
r"-py2\.py3-", # Universal Python 2/3
]
if not any(re.search(pattern, wheel_name) for pattern in python_patterns):
return False
# Check platform compatibility
platform_patterns = get_platform_patterns()
return any(re.search(pattern, wheel_name) for pattern in platform_patterns)
def find_compatible_wheels(python_version: str) -> list[Path]:
"""Find all wheel files compatible with the given Python version."""
if not WHEELS_DIR.exists():
return []
wheels = []
for wheel_path in WHEELS_DIR.glob("*.whl"):
if is_wheel_compatible(wheel_path.name, python_version):
wheels.append(wheel_path)
return sorted(wheels)
def install_wheel(wheel_path: Path) -> tuple[bool, str]:
"""
Install a wheel with --no-deps to verify wheel validity.
Returns:
tuple: (success: bool, error_message: str)
"""
cmd = [
sys.executable,
"-m",
"pip",
"install",
"--no-deps",
"--no-index",
"--find-links",
str(WHEELS_DIR),
str(wheel_path),
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
return True, ""
return False, (result.stderr or result.stdout).strip()
def is_compatibility_error(error_message: str) -> bool:
"""Check if the error is due to Python version or platform constraints."""
compatibility_errors = [
"requires a different Python",
"not a supported wheel on this platform",
"is not a supported wheel",
]
return any(err in error_message for err in compatibility_errors)
def is_corrupt_wheel_archive_error(error_message: str) -> bool:
"""True if pip failed because the file is not a readable ZIP / wheel archive."""
if not error_message:
return False
# pip.exceptions.InvalidWheel -> "Wheel 'pkg' located at <path> is invalid."
if "Wheel '" in error_message and " is invalid." in error_message:
return True
markers = (
"BadZipFile",
"Bad magic number for file header",
"Bad magic number for central directory",
"has an invalid wheel",
"zipfile.BadZipFile",
)
return any(m in error_message for m in markers)
def discard_corrupt_wheel(wheel_path: Path, note: str) -> None:
"""Remove wheel from the test tree and print a single-line warning."""
wheel_path.unlink(missing_ok=True)
print_color(f"-- {wheel_path.name} ({note})", Fore.YELLOW)
def main() -> int:
python_version_tag = get_python_version_tag()
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
print_color(f"---------- TEST WHEELS INSTALL (Python {python_version}) ----------")
print(f"Platform: {sys.platform}\n")
# Load exclude list for current platform (exclude=True for runtime filtering)
exclude_requirements = YAMLListAdapter(
EXCLUDE_LIST_PATH, exclude=True, current_platform=get_current_platform()
).requirements
print(f"Loaded {len(exclude_requirements)} exclude requirements from {EXCLUDE_LIST_PATH}\n")
# Find compatible wheels
wheels = find_compatible_wheels(python_version_tag)
print(f"Found {len(wheels)} compatible wheels to test\n")
if not wheels:
print_color("No compatible wheels found!", Fore.RED)
return 1
# First pass: Check wheels against exclude_list and remove excluded ones
excluded = 0
excluded_wheels = []
print_color("---------- EXCLUDE LIST CHECK ----------")
wheels_to_install = []
for wheel_path in wheels:
should_exclude, reason = should_exclude_wheel(wheel_path.name, exclude_requirements)
if should_exclude:
excluded += 1
excluded_wheels.append((wheel_path.name, reason))
wheel_path.unlink()
print_color(f"-- {wheel_path.name}", Fore.RED)
print(f" Reason: {reason}")
else:
wheels_to_install.append(wheel_path)
print_color("---------- END EXCLUDE LIST CHECK ----------")
print(f"Excluded {excluded} wheels\n")
# Second pass: Install remaining wheels
installed = 0
failed = 0
deleted = 0
discarded_corrupt = 0
failed_wheels = []
deleted_wheels = []
print_color("---------- INSTALL WHEELS ----------")
for wheel_path in wheels_to_install:
if not wheel_archive_is_readable(wheel_path):
discarded_corrupt += 1
discard_corrupt_wheel(
wheel_path,
"unreadable / corrupt zip — not a valid wheel archive (PEP 427)",
)
continue
success, error_message = install_wheel(wheel_path)
if success:
installed += 1
elif is_compatibility_error(error_message):
# Wheel is valid but has Python version or platform constraints
# Delete it as it's incompatible with this environment
deleted += 1
deleted_wheels.append(wheel_path.name)
wheel_path.unlink()
print_color(f"-- {wheel_path.name} (compatibility constraint)", Fore.YELLOW)
elif is_corrupt_wheel_archive_error(error_message):
# Truncated/corrupt artifact or bad repair output; drop from this test artifact
# so CI can continue (see module docstring).
discarded_corrupt += 1
discard_corrupt_wheel(wheel_path, "invalid / corrupt zip (pip could not read wheel)")
else:
failed += 1
failed_wheels.append((wheel_path.name, error_message))
print_color(f"-- {wheel_path.name}", Fore.RED)
if error_message:
for line in error_message.split("\n")[:3]:
print(f" {line}")
print_color("---------- END INSTALL WHEELS ----------")
# Print statistics
print_color("---------- STATISTICS ----------")
print_color(f"Installed {installed} wheels", Fore.GREEN)
if excluded > 0:
print_color(f"Excluded {excluded} wheels (exclude_list.yaml)", Fore.YELLOW)
if deleted > 0:
print_color(f"Deleted {deleted} wheels (compatibility constraint)", Fore.YELLOW)
if discarded_corrupt > 0:
print_color(
f"Discarded {discarded_corrupt} wheels (invalid or corrupt zip archive)",
Fore.YELLOW,
)
if failed > 0:
print_color(f"Failed {failed} wheels", Fore.RED)
if failed_wheels:
print_color("\nFailed wheels:", Fore.RED)
for wheel_name, _ in failed_wheels:
print(f" - {wheel_name}")
return 1
print_color("\nAll compatible wheels processed successfully!", Fore.GREEN)
return 0
if __name__ == "__main__":
sys.exit(main())