#!/usr/bin/env python3 """Fetch the 20 hash-locked PLOS/CRS inputs used by clean_source_pack.py.""" from __future__ import annotations import hashlib import json import os from pathlib import Path import urllib.request SCRIPT_ROOT = Path(__file__).resolve().parent ROOT = Path(os.environ.get("FRESH10_SOURCE_PACK", SCRIPT_ROOT / "source_pack")) DESCRIPTOR = SCRIPT_ROOT / "source_pack.json" USER_AGENT = "fresh10-builder/1.0 (+https://huggingface.co/datasets/quintic/fresh10)" def digest(data: bytes) -> str: return hashlib.sha256(data).hexdigest() def contained(root: Path, relative: str) -> Path: candidate = (root / relative).resolve() resolved_root = root.resolve() if candidate != resolved_root and resolved_root not in candidate.parents: raise RuntimeError(f"unsafe source-pack path: {relative!r}") return candidate def main() -> None: payload = json.loads(DESCRIPTOR.read_text(encoding="utf-8")) documents = payload.get("documents") if not isinstance(documents, list) or len(documents) != 20: raise RuntimeError("source_pack.json must describe exactly 20 documents") for document in documents: target = contained(ROOT, document["raw_file"]) expected = document["raw_sha256"] if target.is_file() and digest(target.read_bytes()) == expected: continue request = urllib.request.Request(document["source_url"], headers={"User-Agent": USER_AGENT}) with urllib.request.urlopen(request, timeout=120) as response: data = response.read() actual = digest(data) if actual != expected: raise RuntimeError( f"{document['id']}: upstream bytes changed " f"(expected {expected}, got {actual}); refusing an unreviewed rebuild" ) target.parent.mkdir(parents=True, exist_ok=True) temporary = target.with_suffix(target.suffix + ".tmp") temporary.write_bytes(data) temporary.replace(target) print(f"verified {len(documents)} raw source-pack files under {ROOT}") if __name__ == "__main__": main()