#!/usr/bin/env python3 """Download the command-line tools LinTunes.app carries, and verify them. python3 -I packaging/mac/fetch_tools.py --arch arm64 --out build/tools Reads ``tools.json`` beside this file. Each archive is downloaded to a fresh temporary directory, its SHA-256 compared with the pinned one, and only then is the one named member pulled out to ``/`` (mode 755). A mismatch stops everything with a non-zero exit: a build never ships a tool nobody checked. Downloads are cached by hash in ``/.cache`` so a rebuild doesn't fetch again. Stdlib only, and runs anywhere (the tests check the manifest on Linux). """ import argparse import hashlib import json import platform import shutil import sys import tarfile import tempfile import urllib.request import zipfile from pathlib import Path HERE = Path(__file__).resolve().parent MANIFEST = HERE / "tools.json" ARCHES = ("arm64", "x86_64") USER_AGENT = "LinTunes-build" class VerifyError(Exception): pass def load_manifest(path: Path = MANIFEST) -> dict: return json.loads(path.read_text())["tools"] def sha256_of(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 verify(path: Path, expected: str) -> None: actual = sha256_of(path) if actual != expected.lower(): raise VerifyError(f"{path.name}: SHA-256 {actual}, expected {expected}") def extract_member(archive: Path, member: str, dest: Path) -> None: """Copy one named file out of a .zip or .tar.gz — never extractall, so nothing in the archive can land anywhere but ``dest``.""" if archive.suffix == ".zip" or zipfile.is_zipfile(archive): with zipfile.ZipFile(archive) as z, z.open(member) as src, \ open(dest, "wb") as out: shutil.copyfileobj(src, out) else: with tarfile.open(archive) as t: info = t.getmember(member) if not info.isfile(): raise VerifyError(f"{member} is not a regular file") with t.extractfile(info) as src, open(dest, "wb") as out: shutil.copyfileobj(src, out) dest.chmod(0o755) def download(url: str, dest: Path) -> None: if not url.startswith("https://"): raise VerifyError(f"refusing a non-https download: {url}") request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT}) with urllib.request.urlopen(request, timeout=300) as response, \ open(dest, "wb") as out: shutil.copyfileobj(response, out) def fetch(arch: str, out: Path, manifest: dict | None = None) -> list[Path]: manifest = load_manifest() if manifest is None else manifest out.mkdir(parents=True, exist_ok=True) cache = out / ".cache" cache.mkdir(exist_ok=True) made = [] for name, tool in manifest.items(): entry = tool["arch"][arch] suffix = ".zip" if entry["url"].endswith(".zip") else ".tar.gz" cached = cache / (entry["sha256"] + suffix) if not (cached.is_file() and sha256_of(cached) == entry["sha256"]): with tempfile.TemporaryDirectory() as tmp: part = Path(tmp) / ("download" + suffix) print(f" {name} {tool['version']} ({arch}): {entry['url']}") download(entry["url"], part) verify(part, entry["sha256"]) shutil.move(str(part), cached) verify(cached, entry["sha256"]) dest = out / name extract_member(cached, entry["member"], dest) print(f" {name}: verified {entry['sha256'][:16]}…, {dest}") made.append(dest) return made def main() -> int: native = "arm64" if platform.machine() in ("arm64", "aarch64") else "x86_64" ap = argparse.ArgumentParser(description=__doc__.split("\n")[0]) ap.add_argument("--arch", choices=ARCHES, default=native) ap.add_argument("--out", type=Path, required=True) args = ap.parse_args() try: fetch(args.arch, args.out) except (VerifyError, OSError, KeyError) as e: print(f"fetch_tools: {e}", file=sys.stderr) return 1 return 0 if __name__ == "__main__": sys.exit(main())