From 945d4232cc161001a0a371e904b03497e2fa6f25 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sat, 21 Nov 2026 18:24:33 +0100 Subject: [PATCH 1/2] Verify core distribution artifacts against the reviewed Git tree --- .gitea/workflows/ci.yml | 7 ++ tools/check_core_release.py | 184 ++++++++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 tools/check_core_release.py diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 86d0ab4..1d20263 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -52,3 +52,10 @@ jobs: - name: Run tests run: python manage.py test coulobre + + - name: Verify package artifacts + if: matrix.python-version == '3.14' + run: | + python -m pip install build + python -m build + python tools/check_core_release.py dist/*.tar.gz dist/*.whl diff --git a/tools/check_core_release.py b/tools/check_core_release.py new file mode 100644 index 0000000..b422446 --- /dev/null +++ b/tools/check_core_release.py @@ -0,0 +1,184 @@ +import argparse +import io +import re +import stat +import subprocess +import tarfile +import tomllib +import zipfile +from email.parser import BytesParser +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +PACKAGE = "coulobre-panel" +PREMIUM = re.compile(r"\bcoulobre[-_.]+premium\b", re.IGNORECASE) +SOURCE_ROOT = { + "CONTRIBUTING.md", + "LICENSE", + "MANIFEST.in", + "README.md", + "manage.py", + "pyproject.toml", +} +SOURCE_DIRECTORIES = ("deploy/", "docs/", "src/coulobre/", "tests/") + + +def archive_files(archive, prefix=""): + files = {} + for member in archive: + if member.isdir(): + continue + name = member.name.removeprefix(prefix) + if ( + not member.isfile() + or not member.name.startswith(prefix) + or name in files + ): + raise ValueError(f"Unexpected archive entry: {member.name}") + with archive.extractfile(member) as stream: + files[name] = stream.read() + return files + + +def git_files(ref): + tree = subprocess.check_output( + [ + "git", + "rev-parse", + "--verify", + "--end-of-options", + f"{ref}^{{tree}}", + ], + cwd=ROOT, + text=True, + ).strip() + content = subprocess.check_output(["git", "archive", tree], cwd=ROOT) + with tarfile.open(fileobj=io.BytesIO(content)) as archive: + return archive_files(archive) + + +def wheel_files(path): + files = {} + with zipfile.ZipFile(path) as archive: + for entry in archive.infolist(): + if entry.is_dir(): + continue + name = entry.filename + if name in files or stat.S_ISLNK(entry.external_attr >> 16): + raise ValueError(f"Unexpected wheel entry: {name}") + files[name] = archive.read(entry) + return files + + +def verify_contents(actual, expected, generated): + contents = actual.keys() - generated + missing = expected.keys() - contents + extra = contents - expected.keys() + if missing or extra: + differences = [f"missing {name}" for name in sorted(missing)] + differences += [f"unexpected {name}" for name in sorted(extra)] + raise ValueError("; ".join(differences)) + for name, data in expected.items(): + if actual[name] != data: + raise ValueError( + f"File differs from the reviewed Git tree: {name}" + ) + + +def verify_metadata(data, version): + metadata = BytesParser().parsebytes(data, headersonly=True) + if metadata["Name"] != PACKAGE or metadata["Version"] != version: + raise ValueError( + "Distribution metadata does not match the core release." + ) + if any( + PREMIUM.search(item) for item in metadata.get_all("Requires-Dist", []) + ): + raise ValueError("The core release declares a premium dependency.") + + +def verify_release(source_path, wheel_path, ref): + tracked = git_files(ref) + if any(PREMIUM.search(name) for name in tracked): + raise ValueError("The core Git tree contains a premium package path.") + project = tomllib.loads(tracked["pyproject.toml"].decode())["project"] + version = project["version"] + distribution = f"coulobre_panel-{version}" + if project["name"] != PACKAGE or ( + source_path.name != f"{distribution}.tar.gz" + or wheel_path.name != f"{distribution}-py3-none-any.whl" + ): + raise ValueError("Artifact names do not match the core release.") + + source_expected = { + name: data + for name, data in tracked.items() + if name in SOURCE_ROOT or name.startswith(SOURCE_DIRECTORIES) + } + egg_info = { + "PKG-INFO", + "SOURCES.txt", + "dependency_links.txt", + "entry_points.txt", + "requires.txt", + "top_level.txt", + } + with tarfile.open(source_path, "r:gz") as archive: + source = archive_files(archive, prefix=f"{distribution}/") + verify_contents( + source, + source_expected, + {"PKG-INFO", "setup.cfg"} + | {f"src/coulobre_panel.egg-info/{name}" for name in egg_info}, + ) + verify_metadata(source["PKG-INFO"], version) + + info = f"{distribution}.dist-info" + runtime_expected = { + name.removeprefix("src/"): data + for name, data in tracked.items() + if name.startswith("src/coulobre/") + } + runtime_expected[f"{info}/licenses/LICENSE"] = tracked["LICENSE"] + wheel = wheel_files(wheel_path) + verify_contents( + wheel, + runtime_expected, + { + f"{info}/{name}" + for name in ( + "METADATA", + "RECORD", + "WHEEL", + "entry_points.txt", + "top_level.txt", + ) + }, + ) + verify_metadata(wheel[f"{info}/METADATA"], version) + + +def main(): + parser = argparse.ArgumentParser( + description="Verify Coulobre release files." + ) + parser.add_argument("source", type=Path) + parser.add_argument("wheel", type=Path) + parser.add_argument("--ref", default="HEAD", help="Reviewed Git revision") + args = parser.parse_args() + try: + verify_release(args.source, args.wheel, args.ref) + except ( + KeyError, + OSError, + ValueError, + subprocess.CalledProcessError, + tarfile.TarError, + zipfile.BadZipFile, + ) as error: + parser.exit(1, f"Release verification failed. {error}\n") + print(f"Verified source and wheel against {args.ref}.") + + +if __name__ == "__main__": + main() From 24ddc7a4af9b32d0569bd0c46a662383b599ae46 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sat, 21 Nov 2026 18:24:33 +0100 Subject: [PATCH 2/2] Run artifact verification before pushing release tags --- CONTRIBUTING.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2c1f718..9ad41de 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -34,13 +34,16 @@ use a Python version such as `0.1.0rc1` and a matching Git tag such as `v0.1.0-rc.1`. [Python version specifiers][versions] 2. From a clean checkout, use `python -m build` to build the source - distribution and wheel. Verify that the source archive contains the - deployment examples and guides. Install the built wheel in a fresh - virtual environment and run the deployment checks for the release. + distribution and wheel. Run + `python tools/check_core_release.py dist/*.tar.gz dist/*.whl` to compare + both packages with the reviewed Git tree. Use `--ref ` + to verify the files against a specific revision. Install the built wheel + in a fresh virtual environment and run the deployment checks. [Build documentation][build] ยท [Deployment checks][deployment-checks] -3. Create an annotated tag for the tested commit on `main` and publish the - corresponding [Gitea release][releases] with the source distribution - and wheel. Include user-visible changes, installation or upgrade +3. Create an annotated tag for the tested commit on `main`. Verify the + artifacts against that local tag before pushing it. Publish the + corresponding [Gitea release][releases] with the verified source + distribution and wheel. Include user-visible changes, installation or upgrade instructions, and known limitations. Mark a release candidate as a prerelease. Close the milestone when its target version is published.