#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Fetch the pinned documents into reference/ (git-ignored; nothing fetched is committed). ``scripts/pinned_files.json`` lists one file per entry: its path under the repository, its URL, its SHA-256, its licence or terms and a note. A file whose SHA-256 does not match is not kept. Nothing fetched is executed. python3 -I scripts/fetch_references.py # fetch what is missing python3 -I scripts/fetch_references.py --verify # hash what is present, fetch nothing python3 -I scripts/fetch_references.py --pin # fetch entries whose sha256 is empty # and write their hashes into the pin file Before an entry is added, the site's robots.txt is read (the family's rule of 2026-11-26): a site that turns away AI crawlers is not fetched from, and is recorded in docs/provenance.md as "set aside (site's wishes)". """ 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] PINS = ROOT / "scripts" / "pinned_files.json" # bitsavers answers 403 to curl's own user agent (tms34010-python docs/validation.md). 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 download(url: str, target: Path) -> None: result = subprocess.run( ["curl", "-sSfL", "--max-time", "1800", "-A", USER_AGENT, "-o", str(target), url], capture_output=True, check=False, ) if result.returncode: raise OSError(result.stderr.decode("utf-8", "replace").strip()) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) group = parser.add_mutually_exclusive_group() group.add_argument("--verify", action="store_true") group.add_argument("--pin", action="store_true") args = parser.parse_args() entries = json.loads(PINS.read_text(encoding="utf-8")) problems = 0 changed = False for entry in entries: path = ROOT / entry["path"] pinned = entry["sha256"] if path.exists(): digest = sha256(path) if pinned and digest != pinned: print(f"ERROR: {entry['path']} does not match its pin") problems += 1 elif not pinned and args.pin: entry["sha256"] = digest changed = True print(f"pinned {entry['path']} {digest}") else: print(f"ok {entry['path']}") continue if args.verify or (not pinned and not args.pin): print(f"missing: {entry['path']}" + ("" if pinned else " (unpinned: use --pin)")) 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) try: download(entry["url"], partial) except OSError as error: partial.unlink(missing_ok=True) print(f"ERROR: {entry['url']}: {error}") problems += 1 continue digest = sha256(partial) if pinned and digest != pinned: partial.unlink() print(f"ERROR: {entry['url']} has SHA-256 {digest}, pinned {pinned}; not kept") problems += 1 continue partial.replace(path) if not pinned: entry["sha256"] = digest changed = True print(f"fetched and pinned {entry['path']} {digest}") else: print(f"fetched {entry['path']}") if changed: PINS.write_text(json.dumps(entries, indent=1) + "\n", encoding="utf-8") return 1 if problems else 0 if __name__ == "__main__": sys.exit(main())