89 lines
3.0 KiB
Python
89 lines
3.0 KiB
Python
"""Exact-commit necessary text only, bounded HTTP, no upstream imports or execution."""
|
|
|
|
import base64
|
|
import datetime as dt
|
|
import hashlib
|
|
import json
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
COMMITS = {
|
|
"0.21.1": "26515acd23fa291a8f5ab53c5997258598de0701",
|
|
"0.17.0": "63d1bbdb407c69370d407ce5ced6ca3f917528a8",
|
|
}
|
|
|
|
|
|
def now():
|
|
return dt.datetime.now(dt.timezone.utc).isoformat()
|
|
|
|
|
|
def sha(raw):
|
|
return hashlib.sha256(raw).hexdigest()
|
|
|
|
|
|
def save(name, value):
|
|
with (HERE / name).open("x", encoding="utf-8") as stream:
|
|
json.dump(value, stream, ensure_ascii=False, indent=2)
|
|
stream.write("\n")
|
|
|
|
|
|
def fetch(version, path, phase):
|
|
commit = COMMITS[version]
|
|
key = phase + "-" + version + "-" + path.replace("/", "__")
|
|
url = "https://api.github.com/repos/Comfy-Org/ComfyUI/contents/" + path + "?ref=" + commit
|
|
record = dict(requested_url=url, started_at=now(), maximum_bytes=512000)
|
|
raw = b""
|
|
try:
|
|
request = urllib.request.Request(
|
|
url, headers={"User-Agent": "HEL-892-static-protocol", "Accept-Encoding": "identity"}
|
|
)
|
|
with urllib.request.urlopen(request, timeout=20) as response:
|
|
record.update(
|
|
status=response.status, headers=dict(response.headers), final_url=response.url
|
|
)
|
|
raw = response.read(512001)
|
|
assert len(raw) <= 512000
|
|
record["ok"] = True
|
|
except (OSError, AssertionError) as exc:
|
|
record.update(ok=False, error=type(exc).__name__ + ": " + str(exc))
|
|
if isinstance(exc, urllib.error.HTTPError):
|
|
record["status"] = exc.code
|
|
raw = exc.read(4096)
|
|
finally:
|
|
record.update(finished_at=now(), bytes=len(raw), sha256=sha(raw))
|
|
with (HERE / "network" / (key + ".body")).open("xb") as stream:
|
|
stream.write(raw)
|
|
save("network/" + key + ".json", record)
|
|
assert record["ok"], record
|
|
item = json.loads(raw)
|
|
assert item["path"] == path and item["type"] == "file" and item["encoding"] == "base64"
|
|
text = base64.b64decode(item["content"])
|
|
blob = hashlib.sha1(b"blob " + str(len(text)).encode() + b"\0" + text).hexdigest()
|
|
assert len(text) == item["size"] and blob == item["sha"]
|
|
text.decode("utf-8")
|
|
output = version + "__" + path.replace("/", "__") + ".txt"
|
|
with (HERE / "source-text" / output).open("xb") as stream:
|
|
stream.write(text)
|
|
return dict(
|
|
repository="https://github.com/Comfy-Org/ComfyUI",
|
|
version=version,
|
|
commit=commit,
|
|
path=path,
|
|
file="source-text/" + output,
|
|
sha256=sha(text),
|
|
git_blob_sha1=blob,
|
|
retrieved_at=record["finished_at"],
|
|
request_key=key,
|
|
scope="exact text only; target host engine version unknown",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
phase, *paths = sys.argv[1:]
|
|
proofs = [fetch(v, p, phase) for v in COMMITS for p in paths]
|
|
save(phase + "-sources.json", proofs)
|
|
print(len(proofs), "exact-commit necessary texts obtained")
|