"""Downloads every engine in engines.json, refuses any whose SHA-256 isn't the published one, and
unpacks it, so a test runs exactly the build its result names.
python3 fetch_engines.py <engines.json> <folder>
prints NAME=<folder>/<name>/<binary> for each engine, ready for a test's --engine. An engine with
a "path" instead of a "url" is one the kit builds itself (tests/timing/build_itgmania.sh); its
binary's SHA-256 goes to standard error for the record.
"""
from __future__ import annotations
import hashlib
import json
import sys
import tarfile
import urllib.request
from pathlib import Path
# Named, since stepmaniay.com's downloads refuse a bare Python client.
USER_AGENT = "stepmaniay-verification (+https://stepmaniay.com/verification/)"
def sha256(path: Path) -> str:
digest = hashlib.sha256()
with open(path, "rb") as f:
for block in iter(lambda: f.read(1 << 20), b""):
digest.update(block)
return digest.hexdigest()
def main() -> None:
engines = json.loads(Path(sys.argv[1]).read_text())
root = Path(sys.argv[2])
root.mkdir(parents=True, exist_ok=True)
for name, engine in engines.items():
if "path" in engine:
binary = Path(engine["path"])
if not binary.exists():
raise SystemExit(f"{name}: {binary} isn't built yet ({engine.get('build', 'see its test')})")
print(f"{name}: {binary} sha256 {sha256(binary)}", file=sys.stderr)
print(f"{name}={binary}")
continue
archive = root / f"{name}.tar.gz"
if not archive.exists() or sha256(archive) != engine["sha256"]:
print(f"downloading {name}", file=sys.stderr)
request = urllib.request.Request(engine["url"], headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(request) as response, open(archive, "wb") as out:
while block := response.read(1 << 20):
out.write(block)
got = sha256(archive)
if got != engine["sha256"]:
archive.unlink()
raise SystemExit(f"{name}: SHA-256 {got} isn't the published {engine['sha256']}")
folder = root / name
binary = folder / engine["binary"]
if not binary.exists():
folder.mkdir(exist_ok=True)
with tarfile.open(archive) as tar:
tar.extractall(folder)
print(f"{name}={binary}")
if __name__ == "__main__":
main()