File size: 1,922 Bytes
406a8a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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()