#!/usr/bin/env python3 """Build a reproducible source ZIP from an explicit publication allowlist. The archive contains code, documentation, configurations, tests, and the tokenizer. Model weights are distributed as separate verified checkpoints. """ from __future__ import annotations import argparse import hashlib import zipfile from pathlib import Path ROOT_FILES = ("README.md", "pyproject.toml", ".gitignore", "THIRD_PARTY_NOTICES.md") PATTERNS = ( "src/pat/*.py", "src/pat/kernels/*.py", "src/pat/assets/tokenizer.json", "src/pat/assets/tokenizer_contract.json", "src/pat/assets/tokenizer_license.txt", "CHECKPOINTS.json", "configs/*.json", "docs/*.md", "docs/verification.json", "scripts/train_*.sh", "scripts/build_supplement.py", "tests/test_*.py", "THIRD_PARTY_NOTICES/tokenizer/LICENSE", ) def source_files(root: Path) -> list[Path]: paths = set() for name in ROOT_FILES: path = root / name if not path.is_file(): raise FileNotFoundError(f"Required supplement file is missing: {name}") paths.add(path) for pattern in PATTERNS: paths.update(root.glob(pattern)) for path in paths: if path.is_symlink() or not path.is_file(): raise ValueError(f"Supplement sources must be regular files: {path.relative_to(root)}") if not path.resolve().is_relative_to(root.resolve()): raise ValueError("Supplement source resolves outside the repository") return sorted(paths, key=lambda path: path.relative_to(root).as_posix()) def build(root: Path, output: Path) -> dict: paths = source_files(root) if output.exists(): raise FileExistsError(output) output.parent.mkdir(parents=True, exist_ok=True) sums = [] with zipfile.ZipFile(output, "x", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as archive: for path in paths: relative = path.relative_to(root).as_posix() data = path.read_bytes() sums.append(f"{hashlib.sha256(data).hexdigest()} {relative}\n") info = zipfile.ZipInfo(f"PAT/{relative}", date_time=(1980, 1, 1, 0, 0, 0)) info.compress_type = zipfile.ZIP_DEFLATED info.create_system = 3 executable = path.suffix == ".sh" or relative == "scripts/build_supplement.py" info.external_attr = (0o100755 if executable else 0o100644) << 16 archive.writestr(info, data) info = zipfile.ZipInfo("PAT/SHA256SUMS", date_time=(1980, 1, 1, 0, 0, 0)) info.compress_type = zipfile.ZIP_DEFLATED info.create_system = 3 info.external_attr = 0o100644 << 16 archive.writestr(info, "".join(sums)) with zipfile.ZipFile(output) as archive: if archive.testzip() is not None: raise RuntimeError("ZIP integrity check failed") digest = hashlib.sha256(output.read_bytes()).hexdigest() output.with_suffix(output.suffix + ".sha256").write_text(f"{digest} {output.name}\n") return {"archive": str(output), "sha256": digest, "source_files": len(paths)} def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output", type=Path, help="New ZIP path; existing archives are never overwritten") parser.add_argument("--list", action="store_true", help="Inspect the allowlist without writing an archive") args = parser.parse_args() root = Path(__file__).resolve().parents[1] if args.list: for path in source_files(root): print(path.relative_to(root).as_posix()) elif args.output: import json print(json.dumps(build(root, args.output), indent=2)) else: parser.error("provide --output or --list") if __name__ == "__main__": main()