"""Baixa os pesos listados no models.lock.json para VOICE_MODELS_DIR e confere o sha256. Uso: python scripts/fetch_models.py [--group v1 --group stt-base] [--dest /models] Reprodutível: arquivo já presente com o hash certo não é baixado de novo; hash errado = falha. """ from __future__ import annotations import argparse import hashlib import json import os import sys import urllib.request from pathlib import Path ROOT = Path(__file__).resolve().parent.parent def sha256_file(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for block in iter(lambda: handle.read(1 << 20), b""): digest.update(block) return digest.hexdigest() def fetch(entry: dict, dest: Path) -> None: target = dest / entry["path"] if target.is_file() and sha256_file(target) == entry["sha256"]: print(f"ok (já presente) {entry['path']}") return if not entry["url"].startswith("https://"): raise SystemExit(f"URL não-HTTPS recusada: {entry['path']}") target.parent.mkdir(parents=True, exist_ok=True) tmp = target.with_suffix(target.suffix + ".part") print(f"baixando {entry['path']} ...") with urllib.request.urlopen(entry["url"], timeout=120) as response, tmp.open("wb") as out: # noqa: S310 - https checado acima while block := response.read(1 << 20): out.write(block) actual = sha256_file(tmp) if actual != entry["sha256"]: tmp.unlink(missing_ok=True) raise SystemExit(f"checksum divergente em {entry['path']}: {actual}") os.replace(tmp, target) print(f"ok {entry['path']}") def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--group", action="append", help="repita para baixar vários grupos (padrão: v1)") parser.add_argument("--dest", default=os.environ.get("VOICE_MODELS_DIR", str(ROOT.parent / "data" / "voice" / "models"))) parser.add_argument("--lock", default=str(ROOT / "models.lock.json")) args = parser.parse_args() lock = json.loads(Path(args.lock).read_text(encoding="utf-8")) groups = set(args.group or ["v1"]) entries = [e for e in lock["files"] if e.get("group", "v1") in groups] if not entries: print(f"nenhum arquivo nos grupos {sorted(groups)}", file=sys.stderr) return 1 for entry in entries: fetch(entry, Path(args.dest)) return 0 if __name__ == "__main__": raise SystemExit(main())