#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Fetch the pinned documents into reference/, verified by SHA-256. Nothing fetched here is committed (reference/ is git-ignored) and nothing fetched is executed. One pin file drives it, ``scripts/pinned_files.json``: one entry per file with its URL, SHA-256, licence and note. A mismatch is an error and the file is not kept. There are no pinned source trees: every emulator and HDL implementation of these chips is a closed box or set aside (docs/sources.md). The User-Agent names a generic client, never a browser (2026-11-26 rule). Every site's robots.txt was read before its first fetch (docs/sources.md). python -I scripts/fetch_references.py # everything missing python -I scripts/fetch_references.py --verify # hash what is present, fetch nothing """ from __future__ import annotations import argparse import hashlib import json import subprocess import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] FILES = ROOT / "scripts" / "pinned_files.json" USER_AGENT = "emu-family-reference-fetch/1.0" 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: ap = argparse.ArgumentParser() ap.add_argument("--verify", action="store_true", help="hash what is present, fetch nothing") a = ap.parse_args() problems = ok = 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: ok += 1 print(f"ok {entry['path']}") continue if a.verify: 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) 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 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) ok += 1 print(f"fetched {entry['path']}") print(f"{ok} ok, {problems} problems") return 1 if problems else 0 if __name__ == "__main__": raise SystemExit(main())