#!/usr/bin/env python3 """Verify and extract benchmark data archives.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path HF_DIR = Path(__file__).resolve().parents[1] ARCHIVES_MANIFEST = HF_DIR / "archives_manifest.json" def sha256_file(path: Path, chunk_size: int = 1024 * 1024 * 16) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(chunk_size), b""): digest.update(chunk) return digest.hexdigest() def main() -> None: parser = argparse.ArgumentParser() parser.add_argument( "--community", action="append", help="community id to extract, e.g. community_0. Can be repeated. Defaults to all.", ) parser.add_argument("--skip-existing", action="store_true") parser.add_argument("--no-verify", action="store_true") args = parser.parse_args() manifest = json.loads(ARCHIVES_MANIFEST.read_text(encoding="utf-8")) wanted = set(args.community or []) for record in manifest["archives"]: community_id = record["community_id"] if wanted and community_id not in wanted: continue archive_path = HF_DIR / record["archive_path"] extract_path = HF_DIR / record["extracts_to"] if args.skip_existing and extract_path.exists(): print(f"skip existing {community_id}") continue if not args.no_verify: digest = sha256_file(archive_path) if digest != record["sha256"]: raise SystemExit(f"sha256 mismatch for {archive_path}") print(f"extract {community_id}") subprocess.run( ["tar", "--use-compress-program", "zstd", "-xf", str(archive_path.relative_to(HF_DIR))], cwd=HF_DIR, check=True, ) if __name__ == "__main__": main()