Verify core distribution artifacts against the reviewed Git tree
This commit is contained in:
1 parent
847337bfc3
commit
945d4232cc
2 files changed
+191
No files matched your search
@@ -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
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user