"""Audit small pinned official documents without weights or model 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):
    req = urllib.request.Request(url, headers={"User-Agent": "CloutSourceAudit/1.0"})
    with urllib.request.urlopen(req, 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 = "76bec2bb3818863f470de7e867c2dc7f1d0bfd83"
    spec = [
        {"name": n, "url": f"https://raw.githubusercontent.com/microsoft/Mage/{rev}/{n}"}
        for n in [
            "mage_flow/README.md",
            "LICENSE",
            "mage_flow/pipeline.py",
            "mage_flow/inference.py",
            "mage_flow/models/modules/text_encoder.py",
        ]
    ]
result = []
for x in spec:
    h = hashlib.sha256(get(x["url"])).hexdigest()
    if a.baseline and h != x["sha256"]:
        raise ValueError("Source changed " + x["name"])
    result.append({"name": x["name"], "url": x["url"], "sha256": h})
print(
    json.dumps({"scope": "Read only document audit no model test", "documents": result}, indent=2)
)
