File size: 2,195 Bytes
7ca47a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Fetch one original public GitHub archive and verify its frozen SHA-256."""

import argparse
import hashlib
import json
from pathlib import Path
import re
from tempfile import TemporaryDirectory
from urllib.request import urlopen


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("repo", help="GitHub owner/repo listed in study_scores.json")
    parser.add_argument("revision", help="Full Git commit listed in study_scores.json")
    parser.add_argument("output", type=Path, help="New archive file")
    args = parser.parse_args()
    manifest = Path(__file__).with_name("study_scores.json")
    entries = json.loads(manifest.read_text(encoding="utf-8"))
    if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", args.repo) or not re.fullmatch(
        r"[0-9a-f]{40}", args.revision
    ):
        parser.error("Use a GitHub owner/repo and a full commit SHA.")
    match = next((row for group in ("human", "agent") for row in entries[group]
                  if row.get("repo") == args.repo and row["sha"] == args.revision
                  and row.get("archive_sha256")), None)
    if match is None:
        parser.error("This archive is absent from the public source manifest.")
    url = f"https://codeload.github.com/{args.repo}/tar.gz/{args.revision}"
    with TemporaryDirectory(dir=args.output.resolve().parent) as temporary:
        download = Path(temporary) / "source.tar.gz"
        sha = hashlib.sha256()
        total = 0
        with urlopen(url, timeout=30) as inactive_archive, download.open("wb") as stream:
            while block := inactive_archive.read(1024 * 1024):
                total += len(block)
                if total > 2 * 1024**3:
                    raise SystemExit("The archive exceeds the two GiB download limit.")
                stream.write(block)
                sha.update(block)
        if sha.hexdigest() != match["archive_sha256"]:
            raise SystemExit("Archive mismatch. Check the upstream snapshot and gzip bytes.")
        # A hard link publishes the verified bytes without overwriting an existing output.
        args.output.hardlink_to(download)


if __name__ == "__main__":
    main()