# SPDX-License-Identifier: MIT """Download the pinned NGP references into reference/ (git-ignored), or verify them. python -I scripts/fetch_references.py # fetch what is missing python -I scripts/fetch_references.py --verify # check only Two pin lists: scripts/pinned_files.json (URL, size, SHA-256 per file) and scripts/pinned_repos.json (one commit per repository, a blobless clone with a sparse checkout of only the listed paths, so no firmware a repository carries is fetched). A file that fails its hash is moved to trash// (never deleted). Nothing fetched is run. PDFs get a `pdftotext -layout` copy in reference/text/. """ 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] def load(name: str) -> list[dict]: path = ROOT / "scripts" / name return json.loads(path.read_text(encoding="utf-8")) if path.exists() else [] def digest(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def good(entry: dict) -> bool: path = ROOT / entry["path"] return path.is_file() and path.stat().st_size == entry["size"] and digest(path) == entry["sha256"] def move_aside(path: Path) -> None: if path.exists(): dest = ROOT / "trash" / time.strftime("%Y%m%d-%H%M%S") dest.mkdir(parents=True, exist_ok=True) shutil.move(str(path), dest / path.name) def get_file(entry: dict) -> bool: path = ROOT / entry["path"] move_aside(path) path.parent.mkdir(parents=True, exist_ok=True) subprocess.run(["curl", "-sS", "-L", "--fail", "-m", "600", "-o", str(path), entry["url"]], check=True) if good(entry): return True print(f" {path.name}: size {path.stat().st_size}, sha256 {digest(path)} (moved aside)") move_aside(path) return False def head(entry: dict) -> str | None: path = ROOT / entry["path"] if not (path / ".git").exists(): return None out = subprocess.run(["git", "-C", str(path), "rev-parse", "HEAD"], capture_output=True, text=True) return out.stdout.strip() def get_repo(entry: dict) -> None: path = ROOT / entry["path"] path.mkdir(parents=True, exist_ok=True) git = ["git", "-C", str(path)] subprocess.run([*git, "init", "-q"], check=True) if entry.get("sparse"): subprocess.run([*git, "sparse-checkout", "set", "--no-cone", *entry["sparse"]], check=True) fetch = ["fetch", "-q", "--depth", "1", "--filter=blob:none", entry["url"], entry["rev"]] subprocess.run([*git, *fetch], check=True) subprocess.run([*git, "checkout", "-q", "FETCH_HEAD"], check=True) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--verify", action="store_true") args = parser.parse_args() problems = 0 repos, files = load("pinned_repos.json"), load("pinned_files.json") for entry in repos: if head(entry) is None and not args.verify: get_repo(entry) ok = head(entry) == entry["rev"] problems += not ok print(f"{'ok' if ok else 'BAD':8} {entry['path']} @ {entry['rev'][:12]}") for entry in files: ok = good(entry) or (not args.verify and get_file(entry)) problems += not ok if ok and entry["path"].lower().endswith(".pdf") and shutil.which("pdftotext"): text = ROOT / "reference" / "text" / (Path(entry["path"]).stem + ".txt") if not text.exists(): text.parent.mkdir(parents=True, exist_ok=True) subprocess.run(["pdftotext", "-layout", str(ROOT / entry["path"]), str(text)], check=False) print(f"{'ok' if ok else 'BAD':8} {entry['path']}") print(f"{problems} problem(s) among {len(files)} pinned files and {len(repos)} pinned repositories") return 1 if problems else 0 if __name__ == "__main__": sys.exit(main())