Python · complete file
bootstrap_spsc.py
Complete source referenced by the documentation.
benchmarks/spsc/bootstrap_spsc.py
1#!/usr/bin/env python32"""Verify bundled SPSC headers and licenses, or restore missing pinned files; never execute downloads."""3import argparse4import base645import hashlib6import json7from pathlib import Path8import urllib.request91011def digest(path):12 return hashlib.sha256(path.read_bytes()).hexdigest()131415def fetch(dep, entry):16 headers = {"User-Agent": "TwoBufQueue-SPSC-benchmark"}17 try:18 request = urllib.request.Request(entry["url"], headers=headers)19 with urllib.request.urlopen(request, timeout=30) as response:20 return response.read()21 except OSError:22 url = "https://api.github.com/repos/{}/contents/{}?ref={}".format(23 dep["repository"], entry["path"], dep["commit"])24 with urllib.request.urlopen(urllib.request.Request(url, headers=headers), timeout=30) as response:25 return base64.b64decode(json.load(response)["content"])262728def main():29 parser = argparse.ArgumentParser(description=__doc__)30 parser.add_argument("--output", type=Path, default=Path(__file__).resolve().parents[2] / "third_party/spsc")31 parser.add_argument("--check-only", action="store_true")32 args = parser.parse_args()33 manifest = json.loads(Path(__file__).with_name("spsc_dependencies.json").read_text())34 for name, dep in manifest.items():35 for entry in dep["files"]:36 target = args.output / name / entry["path"]37 if target.exists():38 if digest(target) != entry["sha256"]:39 raise RuntimeError("SHA256 mismatch: {}".format(target))40 continue41 if args.check_only:42 raise RuntimeError("Missing dependency: {}".format(target))43 data = fetch(dep, entry)44 if hashlib.sha256(data).hexdigest() != entry["sha256"]:45 raise RuntimeError("Downloaded SHA256 mismatch: {}".format(target))46 target.parent.mkdir(parents=True, exist_ok=True)47 temporary = target.with_name(target.name + ".download")48 temporary.write_bytes(data)49 temporary.replace(target)50 print("Fetched", name, entry["path"], flush=True)51 print("Pinned SPSC dependencies verified:", args.output.resolve())525354if __name__ == "__main__":55 main()