File size: 1,566 Bytes
ee3d849
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Optionally acquire pinned additional source bytes and verify every hash."""
import argparse
import hashlib
import json
from pathlib import Path
import urllib.request

def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--output",type=Path,default=Path("external_sources/additional"))
    args=parser.parse_args()
    manifest=Path(__file__).resolve().parents[1]/"provenance/v2_additional_source_files.json"
    records=json.loads(manifest.read_text())["files"]
    root=args.output.resolve()
    root.mkdir(parents=True,exist_ok=True)
    for r in records:
        relative=Path(r["repository"].split("/")[-1])/r["path"]
        dest=(root/relative).resolve()
        if not dest.is_relative_to(root):
            raise ValueError("Source path escapes requested destination")
        if dest.exists():
            data=dest.read_bytes()
        else:
            request=urllib.request.Request(r["url"],headers={"User-Agent":"Neixarua-source-reproduction/2"})
            with urllib.request.urlopen(request,timeout=60) as response:
                data=response.read(r["bytes"]+1)
        if len(data)!=r["bytes"] or hashlib.sha256(data).hexdigest()!=r["sha256"]:
            raise ValueError("Source byte/hash mismatch: "+r["repository"]+"/"+r["path"])
        if not dest.exists():
            dest.parent.mkdir(parents=True,exist_ok=True)
            dest.write_bytes(data)
        print("Verified:",relative)
    print("All pinned additional source files verified:",len(records))

if __name__=="__main__":
    main()