#!/usr/bin/env python3 """Fetch the pinned documents and source trees into reference/ (git-ignored). Two pin files drive it: - ``scripts/pinned_files.json``: one file per entry, with URL, size and SHA-256. A download whose hash differs is an error and is not kept. - ``scripts/pinned_repos.json``: one repository per entry at a full commit hash, cloned shallow and sparse (gitignore-style patterns). Nothing fetched is committed and nothing fetched is executed. python -I scripts/fetch_references.py # fetch what is missing python -I scripts/fetch_references.py --verify # check only, fetch nothing bitsavers.org answers 403 to scripted clients; the pins use its mirrorservice.org mirror, which serves the same files. """ from __future__ import annotations import argparse import hashlib import json import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] FILES = ROOT / "scripts" / "pinned_files.json" REPOS = ROOT / "scripts" / "pinned_repos.json" USER_AGENT = "Mozilla/5.0 (reference fetch)" def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for block in iter(lambda: handle.read(1 << 20), b""): digest.update(block) return digest.hexdigest() def git_head(path: Path) -> str: result = subprocess.run( ["git", "-C", str(path), "rev-parse", "HEAD"], capture_output=True, text=True, check=False ) return result.stdout.strip() def fetch_files(verify_only: bool) -> int: problems = 0 for entry in json.loads(FILES.read_text(encoding="utf-8")): path = ROOT / entry["path"] if path.exists(): if sha256(path) != entry["sha256"]: print(f"ERROR: {entry['path']} does not match its pin") problems += 1 else: print(f"ok: {entry['path']}") continue if verify_only: print(f"missing: {entry['path']}") problems += 1 continue path.parent.mkdir(parents=True, exist_ok=True) with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as handle: partial = Path(handle.name) result = subprocess.run( ["curl", "-sSfL", "--max-time", "900", "-A", USER_AGENT, "-o", str(partial), entry["url"]], capture_output=True, check=False, ) if result.returncode: partial.unlink(missing_ok=True) print(f"ERROR: {entry['url']}: {result.stderr.decode('utf-8', 'replace').strip()}") problems += 1 continue digest = sha256(partial) if digest != entry["sha256"]: partial.unlink() print(f"ERROR: {entry['url']} has SHA-256 {digest}, pinned {entry['sha256']}; not kept") problems += 1 continue partial.replace(path) print(f"fetched {entry['path']}") return problems def fetch_repos(verify_only: bool) -> int: problems = 0 for entry in json.loads(REPOS.read_text(encoding="utf-8")): path = ROOT / entry["path"] if path.exists(): head = git_head(path) if head != entry["rev"]: print(f"ERROR: {entry['path']} is at {head}, pinned {entry['rev']}") problems += 1 else: print(f"ok: {entry['path']} at {head[:12]}") continue if verify_only: print(f"missing: {entry['path']}") problems += 1 continue path.mkdir(parents=True) git = ["git", "-C", str(path)] subprocess.run([*git, "init", "-q"], check=True) subprocess.run([*git, "remote", "add", "origin", entry["url"]], check=True) subprocess.run([*git, "sparse-checkout", "set", "--no-cone", *entry["sparse"]], check=True) subprocess.run( [*git, "fetch", "-q", "--depth", "1", "--filter=blob:none", "origin", entry["rev"]], check=True, ) subprocess.run([*git, "checkout", "-q", "FETCH_HEAD"], check=True) print(f"cloned {entry['path']} at {entry['rev'][:12]}") return problems def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--verify", action="store_true", help="check only; fetch nothing") args = parser.parse_args() problems = fetch_files(args.verify) + fetch_repos(args.verify) print("all pins satisfied" if not problems else f"{problems} problem(s)") return 1 if problems else 0 if __name__ == "__main__": sys.exit(main())