|
| 1 | +""" |
| 2 | +Rewrite wheel tags from cp312-cp312-PLATFORM to py3-none-PLATFORM. |
| 3 | +
|
| 4 | +This is appropriate when the wheel contains OS-specific non-Python binaries |
| 5 | +(e.g., Inform7) but no Python extension modules (ABI-independent). |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +import re |
| 11 | +import zipfile |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +WHEEL_TAG_RE = re.compile(r"^Tag:\s*(\S+)\s*$") |
| 16 | + |
| 17 | + |
| 18 | +def parse_platform_tag(filename: str) -> str: |
| 19 | + # {dist}-{ver}-{python tag}-{abi tag}-{platform tag}.whl |
| 20 | + m = re.match(r"^.+-[^-]+-[^-]+-(?P<plat>.+)\.whl$", filename) |
| 21 | + if not m: |
| 22 | + raise ValueError(f"Cannot parse wheel filename: {filename}") |
| 23 | + return m.group("plat") |
| 24 | + |
| 25 | + |
| 26 | +def retag(path: Path) -> Path: |
| 27 | + plat = parse_platform_tag(path.name) |
| 28 | + new_name = re.sub(r"-[^-]+-[^-]+-" + re.escape(plat) + r"\.whl$", f"-py3-none-{plat}.whl", path.name) |
| 29 | + # If the regex didn't match (unexpected format), fall back to explicit construction: |
| 30 | + if new_name == path.name: |
| 31 | + # Use a more explicit split |
| 32 | + parts = path.name[:-4].split("-") |
| 33 | + if len(parts) < 5: |
| 34 | + raise ValueError(f"Unexpected wheel name: {path.name}") |
| 35 | + new_name = "-".join(parts[:-3] + ["py3", "none", parts[-1]]) + ".whl" |
| 36 | + |
| 37 | + out = path.with_name(new_name) |
| 38 | + |
| 39 | + with zipfile.ZipFile(path, "r") as zin, zipfile.ZipFile(out, "w", compression=zipfile.ZIP_DEFLATED) as zout: |
| 40 | + wheel_files = [n for n in zin.namelist() if n.endswith(".dist-info/WHEEL")] |
| 41 | + if len(wheel_files) != 1: |
| 42 | + raise RuntimeError(f"Expected exactly one .dist-info/WHEEL, found: {wheel_files}") |
| 43 | + wheel_meta = wheel_files[0] |
| 44 | + |
| 45 | + for info in zin.infolist(): |
| 46 | + data = zin.read(info.filename) |
| 47 | + if info.filename == wheel_meta: |
| 48 | + text = data.decode("utf-8") |
| 49 | + lines = text.splitlines(True) |
| 50 | + |
| 51 | + kept = [] |
| 52 | + for line in lines: |
| 53 | + if WHEEL_TAG_RE.match(line.strip()): |
| 54 | + continue |
| 55 | + kept.append(line) |
| 56 | + |
| 57 | + kept.append(f"Tag: py3-none-{plat}\n") |
| 58 | + data = "".join(kept).encode("utf-8") |
| 59 | + |
| 60 | + zout.writestr(info, data) |
| 61 | + |
| 62 | + # Remove original so only retagged wheel remains |
| 63 | + os.remove(path) |
| 64 | + return out |
| 65 | + |
| 66 | + |
| 67 | +def main() -> None: |
| 68 | + ap = argparse.ArgumentParser() |
| 69 | + ap.add_argument("wheel", nargs="+") |
| 70 | + args = ap.parse_args() |
| 71 | + |
| 72 | + for w in args.wheel: |
| 73 | + out = retag(Path(w)) |
| 74 | + print(f"Retagged: {w} -> {out.name}") |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
0 commit comments