File size: 2,321 Bytes
c8beaf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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())