release_checks.py
python
sha256:5c8cb67657bbffc367d7e4b74e9fd7dea32dada487ae1ef893d2d5abfa0447e1
feat(release): gate report bound to the exact artifact, gat…
Opus 5.5
patch
13 days ago
| 1 | #!/usr/bin/env python3 |
| 2 | """Packaging gates for a muse release candidate (musehub#114 Phase 6). Exit 0 = pass. |
| 3 | |
| 4 | release_checks.py reproducible ARTIFACT MUSE_REPO |
| 5 | release_checks.py version ARTIFACT MUSE_REPO |
| 6 | release_checks.py dependency-metadata ARTIFACT |
| 7 | release_checks.py manifest-contract ARTIFACT MUSEHUB_REPO |
| 8 | |
| 9 | stdlib + ``packaging`` only. Everything is built/extracted under a temporary |
| 10 | directory; neither repository nor any installed Muse is modified. |
| 11 | """ |
| 12 | from __future__ import annotations |
| 13 | |
| 14 | import email.parser |
| 15 | import hashlib |
| 16 | import re |
| 17 | import subprocess |
| 18 | import sys |
| 19 | import tarfile |
| 20 | import tempfile |
| 21 | import tomllib |
| 22 | from pathlib import Path |
| 23 | |
| 24 | from packaging.requirements import InvalidRequirement, Requirement |
| 25 | from packaging.version import Version |
| 26 | |
| 27 | |
| 28 | class CheckFailed(Exception): |
| 29 | pass |
| 30 | |
| 31 | |
| 32 | def _version_of(artifact: Path) -> str: |
| 33 | m = re.fullmatch(r"muse-(.+)\.tar\.gz", artifact.name) |
| 34 | if not m: |
| 35 | raise CheckFailed(f"artifact name {artifact.name!r} is not muse-<version>.tar.gz") |
| 36 | return m.group(1) |
| 37 | |
| 38 | |
| 39 | def _members(tarball: Path) -> dict[str, str]: |
| 40 | """name -> sha256 of content, ignoring mtimes/uid/gid and the gzip header.""" |
| 41 | out: dict[str, str] = {} |
| 42 | with tarfile.open(tarball) as tf: |
| 43 | for m in tf.getmembers(): |
| 44 | if m.isfile(): |
| 45 | f = tf.extractfile(m) |
| 46 | assert f is not None |
| 47 | out[m.name] = hashlib.sha256(f.read()).hexdigest() |
| 48 | return out |
| 49 | |
| 50 | |
| 51 | def _pkg_info(artifact: Path) -> email.message.Message: |
| 52 | version = _version_of(artifact) |
| 53 | with tarfile.open(artifact) as tf: |
| 54 | f = tf.extractfile(f"muse-{version}/PKG-INFO") |
| 55 | if f is None: |
| 56 | raise CheckFailed("sdist has no PKG-INFO") |
| 57 | return email.parser.BytesParser().parsebytes(f.read()) |
| 58 | |
| 59 | |
| 60 | def _read_member(artifact: Path, relative: str) -> str: |
| 61 | with tarfile.open(artifact) as tf: |
| 62 | f = tf.extractfile(f"muse-{_version_of(artifact)}/{relative}") |
| 63 | if f is None: |
| 64 | raise CheckFailed(f"sdist is missing {relative}") |
| 65 | return f.read().decode() |
| 66 | |
| 67 | |
| 68 | def reproducible(artifact: Path, repo: Path) -> None: |
| 69 | """A fresh sdist build of the checkout has the same file contents as the artifact.""" |
| 70 | with tempfile.TemporaryDirectory() as tmp: |
| 71 | subprocess.run([sys.executable, "-m", "build", "--sdist", "--outdir", tmp, str(repo)], check=True, capture_output=True, text=True) |
| 72 | rebuilt = Path(tmp) / artifact.name |
| 73 | if not rebuilt.is_file(): |
| 74 | raise CheckFailed(f"rebuild produced {[p.name for p in Path(tmp).iterdir()]}, not {artifact.name}") |
| 75 | a, b = _members(artifact), _members(rebuilt) |
| 76 | if a != b: |
| 77 | diff = sorted(set(a) ^ set(b)) or sorted(k for k in a if a[k] != b[k]) |
| 78 | raise CheckFailed(f"artifact is not reproducible from the checkout; differing members: {diff[:10]}") |
| 79 | |
| 80 | |
| 81 | def version(artifact: Path, repo: Path) -> None: |
| 82 | """Filename, PKG-INFO and the checkout's pyproject agree on one canonical version.""" |
| 83 | claimed = _version_of(artifact) |
| 84 | if str(Version(claimed)) != claimed: |
| 85 | raise CheckFailed(f"{claimed!r} is not canonical PEP 440") |
| 86 | info = _pkg_info(artifact)["Version"] |
| 87 | pyproject = tomllib.loads((repo / "pyproject.toml").read_text())["project"]["version"] |
| 88 | if not (claimed == info == pyproject): |
| 89 | raise CheckFailed(f"version disagreement: filename={claimed} PKG-INFO={info} pyproject={pyproject}") |
| 90 | |
| 91 | |
| 92 | def dependency_metadata(artifact: Path) -> None: |
| 93 | info = _pkg_info(artifact) |
| 94 | if not info["Requires-Python"]: |
| 95 | raise CheckFailed("PKG-INFO lacks Requires-Python") |
| 96 | requires = info.get_all("Requires-Dist") or [] |
| 97 | if not requires: |
| 98 | raise CheckFailed("PKG-INFO lists no Requires-Dist") |
| 99 | for line in requires: |
| 100 | try: |
| 101 | Requirement(line) |
| 102 | except InvalidRequirement as exc: |
| 103 | raise CheckFailed(f"invalid Requires-Dist {line!r}: {exc}") from exc |
| 104 | extras = set(info.get_all("Provides-Extra") or []) |
| 105 | if "all" not in extras: |
| 106 | raise CheckFailed(f"the installer's full profile needs an 'all' extra; sdist provides {sorted(extras)}") |
| 107 | |
| 108 | |
| 109 | def manifest_contract(artifact: Path, musehub_repo: Path) -> None: |
| 110 | """The engine inside the sdist and the installer agree on installer_contract.""" |
| 111 | engine = re.search(r"^INSTALLER_CONTRACT\s*=\s*(\d+)", _read_member(artifact, "muse/core/install_manifest.py"), re.M) |
| 112 | installer = re.search(r"installer_contract=(\d+)", (musehub_repo / "musehub/api/routes/musehub/install.py").read_text()) |
| 113 | if not engine or not installer: |
| 114 | raise CheckFailed("could not read INSTALLER_CONTRACT from the engine or installer_contract from the installer") |
| 115 | if engine.group(1) != installer.group(1): |
| 116 | raise CheckFailed(f"installer_contract mismatch: engine={engine.group(1)} installer={installer.group(1)}") |
| 117 | |
| 118 | |
| 119 | def main(argv: list[str]) -> int: |
| 120 | commands = {"reproducible": (reproducible, 2), "version": (version, 2), "dependency-metadata": (dependency_metadata, 1), "manifest-contract": (manifest_contract, 2)} |
| 121 | if len(argv) < 2 or argv[0] not in commands: |
| 122 | print(__doc__, file=sys.stderr) |
| 123 | return 2 |
| 124 | fn, nargs = commands[argv[0]] |
| 125 | if len(argv) - 1 != nargs: |
| 126 | print(f"{argv[0]} takes {nargs} path argument(s)", file=sys.stderr) |
| 127 | return 2 |
| 128 | try: |
| 129 | fn(*[Path(a) for a in argv[1:]]) # type: ignore[arg-type] |
| 130 | except (CheckFailed, subprocess.CalledProcessError, KeyError, OSError, tarfile.TarError) as exc: |
| 131 | print(f"{argv[0]}: FAIL: {exc}", file=sys.stderr) |
| 132 | return 1 |
| 133 | print(f"{argv[0]}: ok") |
| 134 | return 0 |
| 135 | |
| 136 | |
| 137 | if __name__ == "__main__": |
| 138 | raise SystemExit(main(sys.argv[1:])) |
File History
1 commit
sha256:5c8cb67657bbffc367d7e4b74e9fd7dea32dada487ae1ef893d2d5abfa0447e1
feat(release): gate report bound to the exact artifact, gat…
Opus 5.5
patch
13 days ago