Files
aigc-home/docs/hel-892/birefnet-document-evidence/fetch_sources.py
T

52 lines
1.8 KiB
Python

"""Optional bounded textual-source replay; never follow model/media links or import plugins."""
from __future__ import annotations
import hashlib
import json
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1] / "birefnet-reference"
def read(url):
with urllib.request.urlopen(url, timeout=30) as response:
return response.read()
def main():
entries = json.loads((ROOT / "sources.json").read_bytes())["sources"]
for entry in entries:
assert len(entry["commit"]) == 40 and entry["commit"] in entry["url"]
raw = read(entry["url"])
assert hashlib.sha256(raw).hexdigest() == entry["sha256"]
(ROOT / entry["local_file"]).write_bytes(raw)
ledger = json.loads((ROOT / "core-bounded-search.json").read_bytes())
matches = {key: [] for key in ledger["matches"]}
commit = ledger["commit"]
assert commit == "3086026401180c9216bcb6ace442a4e3587d2c66"
for entry in ledger["files"]:
path = entry["path"]
local = ROOT / ("core__" + path.replace("/", "__"))
raw = (
local.read_bytes()
if local.exists()
else read(f"https://raw.githubusercontent.com/Comfy-Org/ComfyUI/{commit}/{path}")
)
assert hashlib.sha256(raw).hexdigest() == entry["sha256"]
assert (
hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
== entry["git_blob_sha1"]
)
for key in matches:
hits = [i for i, line in enumerate(raw.decode().splitlines(), 1) if key in line]
if hits:
matches[key].append(dict(path=path, lines=hits))
assert matches == ledger["matches"]
print("PASS exact textual originals and bounded 101-file search replay; no upstream execution")
if __name__ == "__main__":
main()