#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Fetch the pinned references into the git-ignored reference/ directory. Files come from scripts/pinned_files.json (each pinned by SHA-256 and size), repositories from scripts/pinned_repos.json (each pinned by a full commit SHA, sparse where only a part is wanted). Nothing fetched is executed or committed. A file whose bytes do not match its pin is kept beside the target as `.mismatch` and reported, never used. python -I scripts/fetch_references.py # fetch what is missing python -I scripts/fetch_references.py --verify # check what is present, fetch nothing python -I scripts/fetch_references.py --skip 3do-devkit Needs `curl` and `git`. """ from __future__ import annotations import argparse import hashlib import json import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] 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 fetch_file(entry: dict, verify: bool) -> bool: target = ROOT / entry["path"] if target.exists(): if sha256(target) == entry["sha256"]: return True print(f"MISMATCH {entry['path']}", file=sys.stderr) return False if verify: print(f"MISSING {entry['path']}", file=sys.stderr) return False target.parent.mkdir(parents=True, exist_ok=True) partial = target.with_name(target.name + ".part") subprocess.run(["curl", "-sS", "-L", "--fail", "-m", "600", "--retry", "5", "--retry-delay", "15", "--retry-all-errors", "-A", "Mozilla/5.0", "-o", str(partial), entry["url"]], check=True) if sha256(partial) != entry["sha256"]: partial.rename(target.with_name(target.name + ".mismatch")) print(f"MISMATCH {entry['path']} (kept as .mismatch)", file=sys.stderr) return False partial.rename(target) return True def git(*args: str, cwd: Path | None = None) -> str: result = subprocess.run(["git", *args], cwd=cwd, check=True, capture_output=True, text=True) return result.stdout.strip() def fetch_repo(entry: dict, verify: bool) -> bool: target = ROOT / entry["path"] if (target / ".git").exists(): head = git("rev-parse", "HEAD", cwd=target) if head == entry["commit"]: return True print(f"WRONG COMMIT {entry['path']}: {head}", file=sys.stderr) return False if verify: print(f"MISSING {entry['path']}", file=sys.stderr) return False target.parent.mkdir(parents=True, exist_ok=True) git("clone", "-q", "--filter=blob:none", "--no-checkout", entry["url"], str(target)) if entry.get("sparse"): git("sparse-checkout", "set", "--no-cone", *entry["sparse"], cwd=target) git("checkout", "-q", entry["commit"], cwd=target) return True def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--verify", action="store_true", help="check only, fetch nothing") parser.add_argument("--skip", action="append", default=[], help="skip entries whose path contains this") args = parser.parse_args() files = json.loads((ROOT / "scripts" / "pinned_files.json").read_text(encoding="utf-8")) repos = json.loads((ROOT / "scripts" / "pinned_repos.json").read_text(encoding="utf-8")) good = bad = 0 for entry in files: if any(s in entry["path"] for s in args.skip): continue ok = fetch_file(entry, args.verify) good, bad = good + ok, bad + (not ok) for entry in repos: if any(s in entry["path"] for s in args.skip): continue ok = fetch_repo(entry, args.verify) good, bad = good + ok, bad + (not ok) print(f"{good} ok, {bad} not ok ({len(files)} files, {len(repos)} repositories pinned)") return 1 if bad else 0 if __name__ == "__main__": sys.exit(main())