Brokkr/tools/build-msgpack-wheel.py
Meta Vens 17584e40f9 Wheel METADATA is the published file's bytes (rebuild, new sha256); a real unlinkSockets removal pins one undo step
build-msgpack-wheel.py read METADATA through read_text, which folded CRLF and changed the hash PyPI's RECORD lists.
The test compares the wheel's METADATA hash with the installed distribution's RECORD and failed first; the unlink
test fails against a mutant that never reports a change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 21:31:30 +01:00

27 lines
1.7 KiB
Python

"""Repack the installed msgpack's pure-Python modules as one py3-none-any wheel (no download, no C extension).
usage: python tools/build-msgpack-wheel.py <out-dir>
Deterministic: the same installed msgpack gives the same bytes, so the sha256 in install.md is checkable.
"""
import base64, hashlib, importlib.metadata as md, pathlib, sys, zipfile
PURE = ("__init__.py", "exceptions.py", "ext.py", "fallback.py") # _cmsgpack is the C extension: left out
dist = md.distribution("msgpack")
version = dist.version
site = pathlib.Path(dist.locate_file(""))
files = {f"msgpack/{name}": (site / "msgpack" / name).read_bytes() for name in PURE}
info = f"msgpack-{version}.dist-info"
files[f"{info}/METADATA"] = (site / info / "METADATA").read_bytes() # bytes: read_text would fold CRLF and change the hash PyPI's RECORD lists
files[f"{info}/top_level.txt"] = b"msgpack\n"
files[f"{info}/licenses/COPYING"] = (site / info / "licenses" / "COPYING").read_bytes()
files[f"{info}/WHEEL"] = b"Wheel-Version: 1.0\nGenerator: brokkr tools/build-msgpack-wheel.py\nRoot-Is-Purelib: true\nTag: py3-none-any\n"
digest = lambda data: "sha256=" + base64.urlsafe_b64encode(hashlib.sha256(data).digest()).rstrip(b"=").decode()
files[f"{info}/RECORD"] = ("".join(f"{n},{digest(d)},{len(d)}\n" for n, d in files.items()) + f"{info}/RECORD,,\n").encode()
out = pathlib.Path(sys.argv[1]) / f"msgpack-{version}-py3-none-any.whl"
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as z:
for name, data in files.items():
entry = zipfile.ZipInfo(name, (1980, 1, 1, 0, 0, 0))
entry.compress_type, entry.external_attr = zipfile.ZIP_DEFLATED, 0o644 << 16
z.writestr(entry, data)
print(out, hashlib.sha256(out.read_bytes()).hexdigest())