# 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 Every entry of scripts/pinned_files.json names a URL, a SHA-256 and a size (and, for archive.org files, the SHA-1 archive.org publishes, checked too). A file is kept only if its hashes 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 host was pinned its robots.txt was read (docs/sources.md); a host that refuses automated or AI clients is not pinned. """ 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" def digest(path: Path, name: str) -> str: hasher = hashlib.new(name) with path.open("rb") as handle: for block in iter(lambda: handle.read(1 << 20), b""): hasher.update(block) return hasher.hexdigest() def check(entry: dict) -> str: path = ROOT / entry["path"] if not path.exists(): return "missing" if path.stat().st_size != entry["size"] or digest(path, "sha256") != entry["sha256"]: return "mismatch" if "sha1" in entry and digest(path, "sha1") != entry["sha1"]: return "mismatch" return "ok" def set_aside(path: Path) -> None: trash = ROOT / "trash" / time.strftime("%Y%m%d-%H%M%S") trash.mkdir(parents=True, exist_ok=True) shutil.move(str(path), trash / path.name) def fetch(entry: dict) -> str: path = ROOT / entry["path"] path.parent.mkdir(parents=True, exist_ok=True) part = path.with_suffix(path.suffix + ".part") subprocess.run( ["curl", "-sS", "-L", "--fail", "-m", "900", "-o", str(part), entry["url"]], check=True, ) part.replace(path) state = check(entry) if state != "ok": set_aside(path) return state def to_text(entry: dict) -> None: path = ROOT / entry["path"] 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(["pdftotext", "-layout", str(path), str(text)], check=False) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--verify", action="store_true", help="check only, download nothing") args = parser.parse_args() entries = json.loads(PINS.read_text(encoding="utf-8")) bad = 0 for entry in entries: state = check(entry) if state == "mismatch" and not args.verify: set_aside(ROOT / entry["path"]) state = "missing" if state == "missing" and not args.verify: state = fetch(entry) time.sleep(entry.get("delay", 1)) # the host's crawl delay, where it sets one if state == "ok": to_text(entry) else: bad += 1 print(f"{state:8} {entry['path']}") print(f"{bad} problem(s) among {len(entries)} pinned files") return 1 if bad else 0 if __name__ == "__main__": sys.exit(main())