# SPDX-License-Identifier: MIT """Fetch the pinned references into reference/ (git-ignored), or check them. python -I scripts/fetch_references.py # download what is missing python -I scripts/fetch_references.py --verify # check sizes and SHA-256s only python -I scripts/fetch_references.py --pin URL PATH # print a new pin entry scripts/pinned_files.json lists each file's URL, path, size and SHA-256. A download is kept only when both match; a mismatch is moved to trash/ (never deleted) and reported. Nothing fetched is executed. PDFs are turned into text (`pdftotext -layout`) under reference/text/ for grepping. Before a site is pinned its robots.txt is read (the 2026-11-26 rule): a site that blocks AI crawlers or serves an anti-AI challenge is not fetched from. The User-Agent is a plain, honest client name; it is never a browser's. """ from __future__ import annotations import argparse import hashlib import json import shutil import subprocess import sys import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PINS = ROOT / "scripts" / "pinned_files.json" AGENT = "emu-phase0-fetch" def sha256(path: Path) -> str: h = hashlib.sha256() with path.open("rb") as f: for block in iter(lambda: f.read(1 << 20), b""): h.update(block) return h.hexdigest() def state(entry: dict) -> str: path = ROOT / entry["path"] if not path.exists(): return "missing" if path.stat().st_size != entry["size"] or sha256(path) != entry["sha256"]: return "mismatch" return "ok" def download(url: str, path: Path) -> None: path.parent.mkdir(parents=True, exist_ok=True) part = path.with_name(path.name + ".part") subprocess.run(["curl", "-sS", "-L", "--fail", "-m", "900", "-A", AGENT, "-o", str(part), url], check=True) part.replace(path) def set_aside(path: Path) -> None: if not path.exists(): return trash = ROOT / "trash" / time.strftime("%Y%m%d-%H%M%S") trash.mkdir(parents=True, exist_ok=True) shutil.move(str(path), str(trash / path.name)) def to_text(path: Path) -> None: if path.suffix != ".pdf" or shutil.which("pdftotext") is None: return text = ROOT / "reference" / "text" / (path.stem + ".txt") if not text.exists(): text.parent.mkdir(parents=True, exist_ok=True) subprocess.run(["nice", "pdftotext", "-layout", str(path), str(text)], check=False) def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--verify", action="store_true") ap.add_argument("--pin", nargs=2, metavar=("URL", "PATH")) a = ap.parse_args() if a.pin: url, rel = a.pin path = ROOT / rel download(url, path) print(json.dumps({"path": rel, "url": url, "size": path.stat().st_size, "sha256": sha256(path)}, indent=1)) return 0 entries = json.loads(PINS.read_text())["files"] bad = 0 for e in entries: s = state(e) if s != "ok" and not a.verify: if s == "mismatch": set_aside(ROOT / e["path"]) download(e["url"], ROOT / e["path"]) s = state(e) if s != "ok": set_aside(ROOT / e["path"]) if s == "ok": to_text(ROOT / e["path"]) bad += s != "ok" print(f"{s:8} {e['path']}") print(f"{len(entries) - bad}/{len(entries)} ok") return 1 if bad else 0 if __name__ == "__main__": sys.exit(main())