-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-futzed-wheels.py
More file actions
194 lines (141 loc) · 5.88 KB
/
generate-futzed-wheels.py
File metadata and controls
194 lines (141 loc) · 5.88 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
# /// script
# requires-python = ">=3.14"
# dependencies = []
# ///
"""
Generates a series of futzed wheels for testing purposes.
Futzes include:
* Wheels that have unusual internal compression methods (e.g., BZIP2, LZMA)
"""
import io
import subprocess
import tempfile
import zipfile
from pathlib import Path
_HERE = Path(__file__).parent
_OUTDIR = _HERE / "dist"
_OUTDIR.mkdir(exist_ok=True)
def _make_package(name: str) -> Path:
"""
Create a new package with `uv init` and `uv build` and return the path to the built wheel.
"""
tempdir = Path(tempfile.mkdtemp())
pkgdir = tempdir / name
subprocess.run(["uv", "init", name], cwd=tempdir, check=True)
subprocess.run(["uv", "build"], cwd=pkgdir, check=True)
dists = list((pkgdir / "dist").glob("*.whl"))
assert len(dists) == 1, (
f"Expected exactly one wheel in {pkgdir / 'dist'}, but found {len(dists)}"
)
return dists[0]
def _recompress_wheel(wheel_path: Path, compression: int) -> tuple[str, bytes]:
"""
Recompress all entries in a wheel with the specified compression method.
Returns a tuple of (wheel_name, wheel_bytes).
"""
original_data = wheel_path.read_bytes()
output_buffer = io.BytesIO()
with (
zipfile.ZipFile(io.BytesIO(original_data), "r") as original_zip,
zipfile.ZipFile(output_buffer, "w", compression=compression) as new_zip,
):
# Copy all entries with the new compression method
for item in original_zip.infolist():
data = original_zip.read(item.filename)
new_zip.writestr(item.filename, data, compress_type=compression)
return (wheel_path.name, output_buffer.getvalue())
def futzed_bz2() -> tuple[str, bytes]:
"""
Create a wheel with one or more files compressed with BZIP2.
"""
wheel_path = _make_package("futzed_bz2")
return _recompress_wheel(wheel_path, zipfile.ZIP_BZIP2)
def futzed_lzma() -> tuple[str, bytes]:
"""
Create a wheel with one or more files compressed with LZMA.
"""
wheel_path = _make_package("futzed_lzma")
return _recompress_wheel(wheel_path, zipfile.ZIP_LZMA)
def futzed_dist_info_wheel_tag_invalid() -> tuple[str, bytes]:
"""
Create a wheel with a `.dist-info/WHEEL` file with an invalid tag (`Tag: py3-none-futzed` instead of `Tag: py3-none-any`).
"""
wheel_path = _make_package("futzed_dist_info_wheel_tag_invalid")
original_data = wheel_path.read_bytes()
output_buffer = io.BytesIO()
with (
zipfile.ZipFile(io.BytesIO(original_data), "r") as original_zip,
zipfile.ZipFile(
output_buffer, "w", compression=zipfile.ZIP_DEFLATED
) as new_zip,
):
for item in original_zip.infolist():
data = original_zip.read(item.filename)
if item.filename.endswith(".dist-info/WHEEL"):
if b"Tag: py3-none-any" not in data:
raise ValueError(
f"Expected 'Tag: py3-none-any' in {item.filename}, but it was not found"
)
# Modify the WHEEL file to have incorrect tags
data = data.replace(b"Tag: py3-none-any", b"Tag: py3-none-futzed")
new_zip.writestr(item.filename, data)
return (wheel_path.name, output_buffer.getvalue())
def futzed_dist_info_wheel_tag_missing() -> tuple[str, bytes]:
"""
Create a wheel with a `.dist-info/WHEEL` file missing the `Tag:` field.
"""
wheel_path = _make_package("futzed_dist_info_wheel_tag_missing")
original_data = wheel_path.read_bytes()
output_buffer = io.BytesIO()
with (
zipfile.ZipFile(io.BytesIO(original_data), "r") as original_zip,
zipfile.ZipFile(
output_buffer, "w", compression=zipfile.ZIP_DEFLATED
) as new_zip,
):
for item in original_zip.infolist():
data = original_zip.read(item.filename)
if item.filename.endswith(".dist-info/WHEEL"):
# Modify the WHEEL file to remove the Tag field
lines = data.splitlines()
lines = [line for line in lines if not line.startswith(b"Tag:")]
data = b"\n".join(lines)
new_zip.writestr(item.filename, data)
return (wheel_path.name, output_buffer.getvalue())
def futzed_dist_info_wheel_version_invalid() -> tuple[str, bytes]:
"""
Create a wheel with a `.dist-info/WHEEL` file with an invalid (non-PEP 440) version in the `Wheel-Version` field.
"""
wheel_path = _make_package("futzed_dist_info_wheel_version_invalid")
original_data = wheel_path.read_bytes()
output_buffer = io.BytesIO()
with (
zipfile.ZipFile(io.BytesIO(original_data), "r") as original_zip,
zipfile.ZipFile(
output_buffer, "w", compression=zipfile.ZIP_DEFLATED
) as new_zip,
):
for item in original_zip.infolist():
data = original_zip.read(item.filename)
if item.filename.endswith(".dist-info/WHEEL"):
if b"Wheel-Version: 1.0" not in data:
raise ValueError(
f"Expected 'Wheel-Version: 1.0' in {item.filename}, but it was not found"
)
# Modify the WHEEL file to have an invalid version
data = data.replace(b"Wheel-Version: 1.0", b"Wheel-Version: invalid")
new_zip.writestr(item.filename, data)
return (wheel_path.name, output_buffer.getvalue())
def main() -> None:
name, wheel = futzed_bz2()
(_OUTDIR / name).write_bytes(wheel)
name, wheel = futzed_lzma()
(_OUTDIR / name).write_bytes(wheel)
name, wheel = futzed_dist_info_wheel_tag_invalid()
(_OUTDIR / name).write_bytes(wheel)
name, wheel = futzed_dist_info_wheel_tag_missing()
(_OUTDIR / name).write_bytes(wheel)
name, wheel = futzed_dist_info_wheel_version_invalid()
(_OUTDIR / name).write_bytes(wheel)
if __name__ == "__main__":
main()