|
| 1 | +"""Verify built wheels in dist/ have Requires-Dist matching setup.cfg. |
| 2 | +
|
| 3 | +Runs after `pants package ::` in CI. Walks dist/*.whl, parses METADATA, |
| 4 | +locates the package's setup.cfg, and asserts: |
| 5 | +
|
| 6 | + set(Requires-Dist without `; extra == ...`) |
| 7 | + == set(install_requires from setup.cfg) |
| 8 | +
|
| 9 | +Exits non-zero with a per-package diff on mismatch. |
| 10 | +""" |
| 11 | + |
| 12 | +import glob |
| 13 | +import pathlib |
| 14 | +import re |
| 15 | +import sys |
| 16 | +import zipfile |
| 17 | +from collections import defaultdict |
| 18 | +from email.parser import Parser |
| 19 | + |
| 20 | +from packaging.requirements import Requirement |
| 21 | +from packaging.utils import canonicalize_name |
| 22 | + |
| 23 | + |
| 24 | +def _norm(req_str: str) -> tuple[str, str]: |
| 25 | + r = Requirement(req_str) |
| 26 | + return canonicalize_name(r.name), str(r.specifier) |
| 27 | + |
| 28 | + |
| 29 | +def _setup_cfg_install_requires(path: pathlib.Path) -> list[str]: |
| 30 | + reqs, in_options, in_ir = [], False, False |
| 31 | + for raw in path.read_text().splitlines(): |
| 32 | + s = raw.strip() |
| 33 | + if s.startswith("[") and s.endswith("]"): |
| 34 | + in_options = s == "[options]" |
| 35 | + in_ir = False |
| 36 | + continue |
| 37 | + if not in_options: |
| 38 | + continue |
| 39 | + if s.startswith("install_requires"): |
| 40 | + in_ir = True |
| 41 | + if "=" in raw: |
| 42 | + v = raw.split("=", 1)[1].strip() |
| 43 | + if v: |
| 44 | + reqs.append(v) |
| 45 | + continue |
| 46 | + if in_ir: |
| 47 | + if raw and not raw[0].isspace(): |
| 48 | + in_ir = False |
| 49 | + continue |
| 50 | + if s and not s.startswith("#"): |
| 51 | + reqs.append(s) |
| 52 | + return reqs |
| 53 | + |
| 54 | + |
| 55 | +def _wheel_runtime_requires(whl: pathlib.Path) -> list[str]: |
| 56 | + with zipfile.ZipFile(whl) as zf: |
| 57 | + meta = next(n for n in zf.namelist() if n.endswith(".dist-info/METADATA")) |
| 58 | + msg = Parser().parsestr(zf.read(meta).decode()) |
| 59 | + out = [] |
| 60 | + for rd in msg.get_all("Requires-Dist") or []: |
| 61 | + if ";" in rd and "extra" in rd.split(";", 1)[1]: |
| 62 | + continue |
| 63 | + out.append(rd.split(";", 1)[0].strip()) |
| 64 | + return out |
| 65 | + |
| 66 | + |
| 67 | +def _pkg_name_from_wheel(whl: pathlib.Path) -> str: |
| 68 | + return whl.name.split("-")[0] |
| 69 | + |
| 70 | + |
| 71 | +def _setup_cfg_for(dist_name: str) -> pathlib.Path | None: |
| 72 | + for cfg in pathlib.Path("py").glob("*/setup.cfg"): |
| 73 | + for line in cfg.read_text().splitlines(): |
| 74 | + m = re.match(r"\s*name\s*=\s*(\S+)", line) |
| 75 | + if m and canonicalize_name(m.group(1)) == canonicalize_name(dist_name): |
| 76 | + return cfg |
| 77 | + return None |
| 78 | + |
| 79 | + |
| 80 | +def main() -> int: |
| 81 | + failures: dict[str, list[str]] = defaultdict(list) |
| 82 | + wheels = sorted(pathlib.Path(p) for p in glob.glob("dist/*.whl")) |
| 83 | + if not wheels: |
| 84 | + print("ERROR: no wheels found in dist/", file=sys.stderr) |
| 85 | + return 2 |
| 86 | + |
| 87 | + for whl in wheels: |
| 88 | + dist_name = _pkg_name_from_wheel(whl) |
| 89 | + cfg = _setup_cfg_for(dist_name) |
| 90 | + if cfg is None: |
| 91 | + failures[whl.name].append( |
| 92 | + f"no matching py/*/setup.cfg for dist name {dist_name!r}" |
| 93 | + ) |
| 94 | + continue |
| 95 | + expected = {_norm(r) for r in _setup_cfg_install_requires(cfg)} |
| 96 | + actual = {_norm(r) for r in _wheel_runtime_requires(whl)} |
| 97 | + if expected == actual: |
| 98 | + print(f"OK {whl.name}") |
| 99 | + continue |
| 100 | + extra = actual - expected |
| 101 | + missing = expected - actual |
| 102 | + if extra: |
| 103 | + failures[whl.name].append( |
| 104 | + f"unexpected Requires-Dist (leaked from pants/deps?): {sorted(extra)}" |
| 105 | + ) |
| 106 | + if missing: |
| 107 | + failures[whl.name].append( |
| 108 | + f"missing Requires-Dist (in setup.cfg but not in wheel): {sorted(missing)}" |
| 109 | + ) |
| 110 | + |
| 111 | + if failures: |
| 112 | + print("\nFAIL: wheel METADATA does not match setup.cfg", file=sys.stderr) |
| 113 | + for whl, errs in failures.items(): |
| 114 | + print(f" {whl}", file=sys.stderr) |
| 115 | + for e in errs: |
| 116 | + print(f" - {e}", file=sys.stderr) |
| 117 | + return 1 |
| 118 | + print(f"\nAll {len(wheels)} wheels match their setup.cfg install_requires.") |
| 119 | + return 0 |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + raise SystemExit(main()) |
0 commit comments