# 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 # download unpinned entries, record their hashes Every entry of scripts/pinned_files.json names a URL, a SHA-256 and a size. A file is kept only if its hash matches; a mismatch is moved to trash/ (never deleted) and reported. Nothing fetched is executed or built. After a download the PDFs are turned into text (`pdftotext -layout`) under reference/text/ for grepping. MAME files (kind "mame-bsd") are fetched at one commit and kept only when their first lines carry `license:BSD-3-Clause`: MAME's COPYING makes a file with no licence line GPL-2.0+, and an MIT repository never keeps GPL source (charter, licences point 3). The check runs again on --verify, so a pin can never smuggle a GPL file in. """ from __future__ import annotations import argparse import hashlib import json import re import shutil import subprocess import sys import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PINS = ROOT / "scripts" / "pinned_files.json" BSD = re.compile(rb"license:\s*BSD-3-Clause") 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 licence_ok(entry: dict, path: Path) -> bool: if entry.get("kind") != "mame-bsd": return True with path.open("rb") as handle: return bool(BSD.search(handle.read(400))) def check(entry: dict) -> str: path = ROOT / entry["path"] if not path.exists(): return "missing" if not licence_ok(entry, path): return "licence" if entry.get("sha256") is None: return "unpinned" if path.stat().st_size != entry["size"] or sha256(path) != entry["sha256"]: 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 download(entry: dict) -> None: 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) def to_text(entry: dict) -> None: path = ROOT / entry["path"] if path.suffix.lower() != ".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]) mode = parser.add_mutually_exclusive_group() mode.add_argument("--verify", action="store_true", help="check only, download nothing") mode.add_argument("--pin", action="store_true", help="fetch unpinned entries and pin them") args = parser.parse_args() entries = json.loads(PINS.read_text(encoding="utf-8")) bad = 0 for entry in entries: path = ROOT / entry["path"] state = check(entry) if state in ("mismatch", "licence") and not args.verify: set_aside(path) state = "missing" if state == "mismatch" else state if state == "missing" and not args.verify and (args.pin or entry.get("sha256")): download(entry) state = check(entry) if state == "licence": set_aside(path) if state == "unpinned" and args.pin: entry["sha256"], entry["size"] = sha256(path), path.stat().st_size state = "pinned" if state in ("ok", "pinned"): to_text(entry) else: bad += 1 print(f"{state:8} {entry['path']}") if args.pin: PINS.write_text(json.dumps(entries, indent=1) + "\n", encoding="utf-8") print(f"{bad} problem(s) among {len(entries)} pinned files") return 1 if bad else 0 if __name__ == "__main__": sys.exit(main())