Skip to content

Commit 4573248

Browse files
Format with black
1 parent 2607058 commit 4573248

File tree

1 file changed

+110
-107
lines changed

1 file changed

+110
-107
lines changed

make_wheels.py

Lines changed: 110 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@
1212
from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED
1313

1414

15-
ZIG_VERSION_INFO_URL = 'https://ziglang.org/download/index.json'
15+
ZIG_VERSION_INFO_URL = "https://ziglang.org/download/index.json"
1616
ZIG_PYTHON_PLATFORMS = {
17-
'x86_64-windows': 'win_amd64',
18-
'x86-windows': 'win32',
19-
'x86_64-macos': 'macosx_12_0_x86_64',
20-
'aarch64-macos': 'macosx_12_0_arm64',
21-
'i386-linux': 'manylinux_2_12_i686.manylinux2010_i686.musllinux_1_1_i686',
17+
"x86_64-windows": "win_amd64",
18+
"x86-windows": "win32",
19+
"x86_64-macos": "macosx_12_0_x86_64",
20+
"aarch64-macos": "macosx_12_0_arm64",
21+
"i386-linux": "manylinux_2_12_i686.manylinux2010_i686.musllinux_1_1_i686",
2222
# renamed i386 to x86 since v0.11.0, i386 was last supported in v0.10.1
23-
'x86-linux': 'manylinux_2_12_i686.manylinux2010_i686.musllinux_1_1_i686',
24-
'x86_64-linux': 'manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64',
25-
'aarch64-linux':
26-
'manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64',
27-
'armv7a-linux': 'manylinux_2_17_armv7l.manylinux2014_armv7l.musllinux_1_1_armv7l',
28-
'powerpc64le-linux': 'manylinux_2_17_ppc64le.manylinux2014_ppc64le.musllinux_1_1_ppc64le',
23+
"x86-linux": "manylinux_2_12_i686.manylinux2010_i686.musllinux_1_1_i686",
24+
"x86_64-linux": "manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64",
25+
"aarch64-linux": "manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64",
26+
"armv7a-linux": "manylinux_2_17_armv7l.manylinux2014_armv7l.musllinux_1_1_armv7l",
27+
"powerpc64le-linux": "manylinux_2_17_ppc64le.manylinux2014_ppc64le.musllinux_1_1_ppc64le",
2928
}
3029

3130

@@ -42,7 +41,7 @@ def writestr(self, zinfo_or_arcname, data, *args, **kwargs):
4241
zinfo.external_attr = 0o0664 << 16
4342

4443
zinfo.compress_type = ZIP_DEFLATED
45-
zinfo.date_time = (1980,1,1,0,0,0)
44+
zinfo.date_time = (1980, 1, 1, 0, 0, 0)
4645
zinfo.create_system = 3
4746
super().writestr(zinfo, data, *args, **kwargs)
4847

@@ -61,34 +60,42 @@ def make_message(headers, payload=None):
6160

6261

6362
def write_wheel_file(filename, contents):
64-
with ReproducibleWheelFile(filename, 'w') as wheel:
63+
with ReproducibleWheelFile(filename, "w") as wheel:
6564
for member_info, member_source in contents.items():
6665
wheel.writestr(member_info, bytes(member_source))
6766
return filename
6867

6968

7069
def write_wheel(out_dir, *, name, version, tag, metadata, description, contents):
71-
wheel_name = f'{name}-{version}-{tag}.whl'
72-
dist_info = f'{name}-{version}.dist-info'
70+
wheel_name = f"{name}-{version}-{tag}.whl"
71+
dist_info = f"{name}-{version}.dist-info"
7372
filtered_metadata = []
7473
for header, value in metadata:
7574
filtered_metadata.append((header, value))
7675

77-
return write_wheel_file(os.path.join(out_dir, wheel_name), {
78-
**contents,
79-
f'{dist_info}/METADATA': make_message([
80-
('Metadata-Version', '2.4'),
81-
('Name', name),
82-
('Version', version),
83-
*filtered_metadata,
84-
], description),
85-
f'{dist_info}/WHEEL': make_message([
86-
('Wheel-Version', '1.0'),
87-
('Generator', 'ziglang make_wheels.py'),
88-
('Root-Is-Purelib', 'false'),
89-
('Tag', tag),
90-
]),
91-
})
76+
return write_wheel_file(
77+
os.path.join(out_dir, wheel_name),
78+
{
79+
**contents,
80+
f"{dist_info}/METADATA": make_message(
81+
[
82+
("Metadata-Version", "2.4"),
83+
("Name", name),
84+
("Version", version),
85+
*filtered_metadata,
86+
],
87+
description,
88+
),
89+
f"{dist_info}/WHEEL": make_message(
90+
[
91+
("Wheel-Version", "1.0"),
92+
("Generator", "ziglang make_wheels.py"),
93+
("Root-Is-Purelib", "false"),
94+
("Tag", tag),
95+
]
96+
),
97+
},
98+
)
9299

93100

94101
def iter_archive_contents(archive):
@@ -109,37 +116,37 @@ def iter_archive_contents(archive):
109116

110117
def write_ziglang_wheel(out_dir, *, version, platform, archive):
111118
contents = {}
112-
contents['ziglang/__init__.py'] = b''
119+
contents["ziglang/__init__.py"] = b""
113120

114121
license_files = {}
115122

116123
# The paths to these licenses MUST match both the actual files
117124
# in the Zig source tarballs and the License-File entries listed
118125
# below in the metadata.
119126
license_paths = [
120-
'LICENSE',
121-
'lib/libc/glibc/LICENSES',
122-
'lib/libc/mingw/COPYING',
123-
'lib/libc/musl/COPYRIGHT',
124-
'lib/libc/wasi/LICENSE',
125-
'lib/libc/wasi/LICENSE-APACHE',
126-
'lib/libc/wasi/LICENSE-APACHE-LLVM',
127-
'lib/libc/wasi/LICENSE-MIT',
128-
'lib/libcxx/LICENSE.TXT',
129-
'lib/libcxxabi/LICENSE.TXT',
130-
'lib/libunwind/LICENSE.TXT'
127+
"LICENSE",
128+
"lib/libc/glibc/LICENSES",
129+
"lib/libc/mingw/COPYING",
130+
"lib/libc/musl/COPYRIGHT",
131+
"lib/libc/wasi/LICENSE",
132+
"lib/libc/wasi/LICENSE-APACHE",
133+
"lib/libc/wasi/LICENSE-APACHE-LLVM",
134+
"lib/libc/wasi/LICENSE-MIT",
135+
"lib/libcxx/LICENSE.TXT",
136+
"lib/libcxxabi/LICENSE.TXT",
137+
"lib/libunwind/LICENSE.TXT",
131138
]
132139

133140
found_license_files = set()
134141

135142
for entry_name, entry_mode, entry_data in iter_archive_contents(archive):
136-
entry_name = '/'.join(entry_name.split('/')[1:])
143+
entry_name = "/".join(entry_name.split("/")[1:])
137144
if not entry_name:
138145
continue
139-
if entry_name.startswith('doc/'):
146+
if entry_name.startswith("doc/"):
140147
continue
141148

142-
zip_info = ZipInfo(f'ziglang/{entry_name}')
149+
zip_info = ZipInfo(f"ziglang/{entry_name}")
143150
zip_info.external_attr = (entry_mode & 0xFFFF) << 16
144151
contents[zip_info] = entry_data
145152

@@ -149,15 +156,19 @@ def write_ziglang_wheel(out_dir, *, version, platform, archive):
149156
license_files[entry_name] = entry_data # adding to license files
150157
found_license_files.add(entry_name) # tracking found licenses
151158

152-
if entry_name.startswith('zig'):
153-
contents['ziglang/__main__.py'] = f'''\
159+
if entry_name.startswith("zig"):
160+
contents["ziglang/__main__.py"] = (
161+
f"""\
154162
import os, sys
155163
argv = [os.path.join(os.path.dirname(__file__), "{entry_name}"), *sys.argv[1:]]
156164
if os.name == 'posix':
157165
os.execv(argv[0], argv)
158166
else:
159167
import subprocess; sys.exit(subprocess.call(argv))
160-
'''.encode('ascii')
168+
""".encode(
169+
"ascii"
170+
)
171+
)
161172

162173
# Check if any license files are missing and warn. This
163174
# is useful to detect changes in the Zig archive structure
@@ -167,46 +178,47 @@ def write_ziglang_wheel(out_dir, *, version, platform, archive):
167178
print(f"Warning: The following license files were not found in the Zig archive: {', '.join(sorted(missing_licenses))}")
168179
print("This may indicate a change in Zig's license file structure or an error in the list of license files and/or paths.")
169180

170-
with open('README.pypi.md') as f:
181+
with open("README.pypi.md") as f:
171182
description = f.read()
172183

173-
dist_info = f'ziglang-{version}.dist-info'
184+
dist_info = f"ziglang-{version}.dist-info"
174185
for license_path, license_data in license_files.items():
175-
contents[f'{dist_info}/licenses/{license_path}'] = license_data
186+
contents[f"{dist_info}/licenses/{license_path}"] = license_data
176187

177-
return write_wheel(out_dir,
178-
name='ziglang',
188+
return write_wheel(
189+
out_dir,
190+
name="ziglang",
179191
version=version,
180-
tag=f'py3-none-{platform}',
192+
tag=f"py3-none-{platform}",
181193
metadata=[
182-
('Summary', 'Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.'),
183-
('Description-Content-Type', "'text/markdown'; charset=UTF-8; variant=GFM"),
194+
("Summary", "Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software."),
195+
("Description-Content-Type", "'text/markdown'; charset=UTF-8; variant=GFM"),
184196
# The license expression and the file paths MUST remain in sync
185197
# with the paths in the official Zig tarballs and with the ones
186198
# defined above in the contents.
187-
('License-Expression', 'MIT'),
188-
('License-File', 'LICENSE'),
189-
('License-File', 'lib/libc/glibc/LICENSES'),
190-
('License-File', 'lib/libc/mingw/COPYING'),
191-
('License-File', 'lib/libc/musl/COPYRIGHT'),
192-
('License-File', 'lib/libc/wasi/LICENSE'),
193-
('License-File', 'lib/libc/wasi/LICENSE-APACHE'),
194-
('License-File', 'lib/libc/wasi/LICENSE-APACHE-LLVM'),
195-
('License-File', 'lib/libc/wasi/LICENSE-MIT'),
196-
('License-File', 'lib/libcxx/LICENSE.TXT'),
197-
('License-File', 'lib/libcxxabi/LICENSE.TXT'),
198-
('License-File', 'lib/libunwind/LICENSE.TXT'),
199-
('Classifier', 'Development Status :: 4 - Beta'),
200-
('Classifier', 'Intended Audience :: Developers'),
201-
('Classifier', 'Topic :: Software Development :: Compilers'),
202-
('Classifier', 'Topic :: Software Development :: Code Generators'),
203-
('Classifier', 'Topic :: Software Development :: Build Tools'),
204-
('Classifier', 'Programming Language :: Other'),
205-
('Classifier', 'Programming Language :: Other Scripting Engines'),
206-
('Project-URL', 'Homepage, https://ziglang.org'),
207-
('Project-URL', 'Source Code, https://github.com/ziglang/zig-pypi'),
208-
('Project-URL', 'Bug Tracker, https://github.com/ziglang/zig-pypi/issues'),
209-
('Requires-Python', '~=3.5'),
199+
("License-Expression", "MIT"),
200+
("License-File", "LICENSE"),
201+
("License-File", "lib/libc/glibc/LICENSES"),
202+
("License-File", "lib/libc/mingw/COPYING"),
203+
("License-File", "lib/libc/musl/COPYRIGHT"),
204+
("License-File", "lib/libc/wasi/LICENSE"),
205+
("License-File", "lib/libc/wasi/LICENSE-APACHE"),
206+
("License-File", "lib/libc/wasi/LICENSE-APACHE-LLVM"),
207+
("License-File", "lib/libc/wasi/LICENSE-MIT"),
208+
("License-File", "lib/libcxx/LICENSE.TXT"),
209+
("License-File", "lib/libcxxabi/LICENSE.TXT"),
210+
("License-File", "lib/libunwind/LICENSE.TXT"),
211+
("Classifier", "Development Status :: 4 - Beta"),
212+
("Classifier", "Intended Audience :: Developers"),
213+
("Classifier", "Topic :: Software Development :: Compilers"),
214+
("Classifier", "Topic :: Software Development :: Code Generators"),
215+
("Classifier", "Topic :: Software Development :: Build Tools"),
216+
("Classifier", "Programming Language :: Other"),
217+
("Classifier", "Programming Language :: Other Scripting Engines"),
218+
("Project-URL", "Homepage, https://ziglang.org"),
219+
("Project-URL", "Source Code, https://github.com/ziglang/zig-pypi"),
220+
("Project-URL", "Bug Tracker, https://github.com/ziglang/zig-pypi/issues"),
221+
("Requires-Python", "~=3.5"),
210222
],
211223
description=description,
212224
contents=contents,
@@ -218,69 +230,60 @@ def fetch_zig_version_info():
218230
return json.loads(request.read())
219231

220232

221-
def fetch_and_write_ziglang_wheels(
222-
outdir='dist/', zig_version='master', wheel_version_suffix='', platforms=tuple()
223-
):
233+
def fetch_and_write_ziglang_wheels(outdir="dist/", zig_version="master", wheel_version_suffix="", platforms=tuple()):
224234
Path(outdir).mkdir(exist_ok=True)
225235
if not platforms:
226236
platforms = list(ZIG_PYTHON_PLATFORMS)
227237
zig_versions_info = fetch_zig_version_info()
228238

229-
if zig_version == 'latest':
230-
zig_version = [version for version in zig_versions_info if version != 'master'][0]
239+
if zig_version == "latest":
240+
zig_version = [version for version in zig_versions_info if version != "master"][0]
231241

232242
try:
233243
zig_version_info = zig_versions_info[zig_version]
234244
except KeyError:
235245
print(f"Invalid version, valid values: {list(zig_versions_info)}")
236246
raise
237247

238-
effective_zig_version = zig_version_info.get('version', zig_version)
248+
effective_zig_version = zig_version_info.get("version", zig_version)
239249

240250
for zig_platform in platforms:
241251
python_platform = ZIG_PYTHON_PLATFORMS[zig_platform]
242252
if zig_platform not in zig_version_info:
243-
print(f"{zig_platform} not present for "
244-
f"version {zig_version} / {effective_zig_version}")
253+
print(f"{zig_platform} not present for " f"version {zig_version} / {effective_zig_version}")
245254
continue
246255
zig_download = zig_version_info[zig_platform]
247-
zig_url = zig_download['tarball']
248-
expected_hash = zig_download['shasum']
256+
zig_url = zig_download["tarball"]
257+
expected_hash = zig_download["shasum"]
249258

250259
with urllib.request.urlopen(zig_url) as request:
251260
zig_archive = request.read()
252261
zig_archive_hash = hashlib.sha256(zig_archive).hexdigest()
253262
if zig_archive_hash != expected_hash:
254263
print(zig_download, "SHA256 hash mismatch!")
255264
raise AssertionError
256-
print(f'{hashlib.sha256(zig_archive).hexdigest()} {zig_url}')
265+
print(f"{hashlib.sha256(zig_archive).hexdigest()} {zig_url}")
257266

258-
wheel_version = effective_zig_version.split('+')[0].replace('-', '.')
259-
wheel_path = write_ziglang_wheel(outdir,
260-
version=wheel_version + wheel_version_suffix,
261-
platform=python_platform,
262-
archive=zig_archive)
263-
with open(wheel_path, 'rb') as wheel:
264-
print(f' {hashlib.sha256(wheel.read()).hexdigest()} {wheel_path}')
267+
wheel_version = effective_zig_version.split("+")[0].replace("-", ".")
268+
wheel_path = write_ziglang_wheel(outdir, version=wheel_version + wheel_version_suffix, platform=python_platform, archive=zig_archive)
269+
with open(wheel_path, "rb") as wheel:
270+
print(f" {hashlib.sha256(wheel.read()).hexdigest()} {wheel_path}")
265271

266272

267273
def get_argparser():
268274
parser = argparse.ArgumentParser(prog=__file__, description="Repackage official Zig downloads as Python wheels")
269-
parser.add_argument('--version', default='latest',
270-
help="version to package, use `latest` for latest release, `master` for nightly build")
271-
parser.add_argument('--suffix', default='', help="wheel version suffix")
272-
parser.add_argument('--outdir', default='dist/', help="target directory")
273-
parser.add_argument('--platform', action='append', choices=list(ZIG_PYTHON_PLATFORMS.keys()), default=[],
274-
help="platform to build for, can be repeated")
275+
parser.add_argument("--version", default="latest", help="version to package, use `latest` for latest release, `master` for nightly build")
276+
parser.add_argument("--suffix", default="", help="wheel version suffix")
277+
parser.add_argument("--outdir", default="dist/", help="target directory")
278+
parser.add_argument("--platform", action="append", choices=list(ZIG_PYTHON_PLATFORMS.keys()), default=[], help="platform to build for, can be repeated")
275279
return parser
276280

277281

278282
def main():
279283
args = get_argparser().parse_args()
280284
logging.getLogger("wheel").setLevel(logging.WARNING)
281-
fetch_and_write_ziglang_wheels(outdir=args.outdir, zig_version=args.version,
282-
wheel_version_suffix=args.suffix, platforms=args.platform)
285+
fetch_and_write_ziglang_wheels(outdir=args.outdir, zig_version=args.version, wheel_version_suffix=args.suffix, platforms=args.platform)
283286

284287

285-
if __name__ == '__main__':
288+
if __name__ == "__main__":
286289
main()

0 commit comments

Comments
 (0)