#!/usr/bin/env python3 """Download an official Ultralytics checkpoint atomically and verify its digest.""" from __future__ import annotations import argparse import hashlib import shutil import sys import urllib.request from pathlib import Path OFFICIAL = { "yolo26s.pt": { "url": "https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo26s.pt", "sha256": "646f8bc3fe0a656803d95c294f7852321748cb29d13466a1af8862e2db384a1b", } } def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--model", default="yolo26s.pt", choices=sorted(OFFICIAL)) parser.add_argument("--output-dir", type=Path, default=Path("models")) args = parser.parse_args() item = OFFICIAL[args.model] args.output_dir.mkdir(parents=True, exist_ok=True) destination = args.output_dir / args.model expected = item["sha256"] if destination.is_file() and sha256(destination) == expected: print(f"Already verified: {destination}") return temporary = destination.with_suffix(destination.suffix + ".part") if temporary.exists(): temporary.unlink() request = urllib.request.Request(item["url"], headers={"User-Agent": "cuphead-detector-setup/1.0"}) print(f"Downloading {item['url']} -> {destination}") try: with urllib.request.urlopen(request, timeout=120) as response, temporary.open("wb") as output: shutil.copyfileobj(response, output, length=1024 * 1024) actual = sha256(temporary) if actual != expected: raise RuntimeError(f"SHA256 mismatch: expected {expected}, got {actual}") temporary.replace(destination) except Exception: temporary.unlink(missing_ok=True) raise print(f"Verified SHA256 {expected}") print(destination.resolve()) if __name__ == "__main__": try: main() except KeyboardInterrupt: sys.exit(130)