#!/usr/bin/env python3 """Download and SHA-256 verify one component from the fixed dependency lock.""" from __future__ import annotations import argparse import csv import hashlib import urllib.parse import urllib.request from pathlib import Path def rows(path: Path) -> list[dict[str, str]]: with path.open("r", encoding="utf-8", newline="") as handle: return list(csv.DictReader(handle, delimiter="\t")) def download(row: dict[str, str], output: Path) -> None: output.mkdir(parents=True, exist_ok=True) target = output / Path(row["repository_path"]).name partial = target.with_suffix(target.suffix + ".partial") url = ( f"https://huggingface.co/datasets/{row['source_repo']}/resolve/" f"{row['source_revision']}/{urllib.parse.quote(row['repository_path'])}?download=true" ) digest = hashlib.sha256() size = 0 with urllib.request.urlopen(url) as response, partial.open("wb") as handle: while True: chunk = response.read(8 * 1024 * 1024) if not chunk: break handle.write(chunk) digest.update(chunk) size += len(chunk) actual = digest.hexdigest() if size != int(row["bytes"]) or actual != row["sha256"]: partial.unlink(missing_ok=True) raise ValueError(f"dependency mismatch for {row['repository_path']}") partial.replace(target) print(f"PASS: {target} ({size:,} bytes; SHA-256 {actual})") def main() -> int: root = Path(__file__).resolve().parents[1] parser = argparse.ArgumentParser() parser.add_argument("component") parser.add_argument("--dependencies", type=Path, default=root / "dependencies" / "DEPENDENCIES.tsv") parser.add_argument("--output", type=Path, default=root / "dependencies" / "downloads") args = parser.parse_args() selected = [row for row in rows(args.dependencies) if row["component"] == args.component] if not selected: raise SystemExit(f"unknown component: {args.component}") for row in selected: download(row, args.output) if any(row["assembly_method"] != "none" for row in selected): print("This component has multiple transport parts. Run tools/reassemble_s4.py explicitly.") return 0 if __name__ == "__main__": raise SystemExit(main())