import hashlib
from pathlib import Path
import subprocess, zipfile
from concurrent.futures import ThreadPoolExecutor
base=Path(__file__).resolve().parent
gh="https://github.com/ggml-org/llama.cpp/releases/download/b12973/"
a="https://huggingface.co/unsloth/Qwen3.6-35B-A3B-GGUF/resolve/a483e9e6cbd595906af30beda3187c2663a1118c/"
b="https://huggingface.co/unsloth/Qwen4-35B-A3B-GGUF/resolve/a1d33fca9a3f7cab8a959f600ff3afb9a02d3aa3/UD-IQ2_M/"
files=[(gh,"llama-b12973-bin-win-vulkan-x64.zip","773341304d3cec928b746586ecc2d7d08a8a03e12dbaef13bc01b5353b1de87e"),(a,"Qwen3.6-35B-A3B-UD-IQ2_M.gguf","2be7ef1ed7e1af8b10d3829102cf9a6c2bd5ddb64d675b4ece23a60799403d43"),(b,"Qwen4-35B-A3B-UD-IQ2_M-00001-of-00002.gguf","01099c649b59bf60a5560403b198c0b95267fa631393d108e8ad2580b130be38"),(b,"Qwen4-35B-A3B-UD-IQ2_M-00002-of-00002.gguf","419b93856e79a6f2338160d98d3b6b75042c57786af92c3010405b7b808dc4b4")]
def fetch(row):
    url,name,digest=row
    path=base/name
    if not path.exists():
        partial=base/(name+".part")
        subprocess.run(["curl.exe","--fail","--location","--silent","--show-error","--connect-timeout","20","--retry","4","--retry-all-errors","-C","-","-o",str(partial),url+name],check=True)
    else: partial=path
    with partial.open("rb") as f: assert hashlib.file_digest(f,"sha256").hexdigest()==digest,name
    if partial!=path: partial.replace(path)
    print("Verified",name,path.stat().st_size,flush=True)
    if name.endswith(".zip"):
        with zipfile.ZipFile(path) as f:f.extractall(base/"runtime-b12973")
if __name__=="__main__":
    with ThreadPoolExecutor(max_workers=4) as pool:list(pool.map(fetch,files))
