"""Read small pinned source documents only without model weights or runtime execution."""

import argparse
import hashlib
import json
import urllib.request
from pathlib import Path

p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--baseline", type=Path)
a = p.parse_args()


def get(url):
    with urllib.request.urlopen(
        urllib.request.Request(url, headers={"User-Agent": "CloutSourceAudit/1.0"}), timeout=40
    ) as r:
        b = r.read(1000001)
        if len(b) > 1000000:
            raise ValueError("Document limit exceeded")
        return b


if a.baseline:
    spec = json.loads(a.baseline.read_text())["documents"]
else:
    rev = "2c2d29ff729e48f33e41f49edfdbd81d5ac103b4"
    spec = [
        {
            "name": name,
            "url": f"https://raw.githubusercontent.com/HiDream-ai/HiDream-O1-Image/{rev}/{name}",
        }
        for name in [
            "README.md",
            "inference.py",
            "models/pipeline.py",
            "models/utils.py",
            "LICENSE",
            "prompt_agent.py",
        ]
    ]
    for name in ["HiDream-O1-Image", "HiDream-O1-Image-Dev"]:
        m = json.loads(get("https://huggingface.co/api/models/HiDream-ai/" + name))
        spec.append(
            {
                "name": name + "/README.md",
                "url": f"https://huggingface.co/HiDream-ai/{name}/resolve/{m['sha']}/README.md",
            }
        )
docs = []
for item in spec:
    digest = hashlib.sha256(get(item["url"])).hexdigest()
    if a.baseline and digest != item["sha256"]:
        raise ValueError("Source hash changed for " + item["name"])
    docs.append({"name": item["name"], "url": item["url"], "sha256": digest})
print(
    json.dumps(
        {
            "scope": "Read only document audit not a model test",
            "repositoryRevision": "2c2d29ff729e48f33e41f49edfdbd81d5ac103b4",
            "documents": docs,
        },
        indent=2,
    )
)
