Files
aigc-home/docs/hel-892/batch180-exact-engine/acquire.py
T

133 lines
4.9 KiB
Python

"""Bounded exact-commit metadata and four source texts; never execute them."""
import base64
import datetime
import hashlib
import json
import urllib.error
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
DOC = Path(__file__).resolve().parent
REPO = "https://github.com/Comfy-Org/ComfyUI"
COMMIT = "8010b48d2c1be986613f8e8c761527d205bd756f"
API = "https://api.github.com/repos/Comfy-Org/ComfyUI"
PATHS = (
"execution.py",
"comfy_execution/graph.py",
"comfy_execution/graph_utils.py",
"comfy_execution/validation.py",
)
def sha(data):
return hashlib.sha256(data).hexdigest()
def utc():
return datetime.datetime.now(datetime.timezone.utc).isoformat()
def write(name, data):
with (DOC / name).open("xb") as stream:
stream.write(data)
def save(name, value):
write(name, (json.dumps(value, ensure_ascii=False, indent=2) + "\n").encode())
def request(key, suffix):
url = API + suffix
cap = 524288
receipt = dict(key=key, url=url, response_cap=cap, started_at_utc=utc())
req = urllib.request.Request(url, headers={"User-Agent": "HEL-892-static-audit"})
try:
try:
response = urllib.request.urlopen(req, timeout=30)
except urllib.error.HTTPError as exc:
response = exc
with response:
raw = response.read(cap + 1)
receipt.update(
status=response.status, headers=dict(response.headers), final_url=response.url
)
write("network/" + key + ".body", raw)
receipt.update(bytes=len(raw), sha256=sha(raw), cap_exceeded=len(raw) > cap)
assert len(raw) <= cap, "HTTP response exceeds cap; bounded prefix preserved"
assert receipt["status"] == 200, "HTTP failure; raw response preserved"
result = json.loads(raw)
except Exception as exc:
receipt["error"] = type(exc).__name__ + ": " + str(exc)
raise
finally:
receipt["completed_at_utc"] = utc()
save("network/" + key + ".json", receipt)
return result, receipt
def main():
(DOC / "network").mkdir()
(DOC / "source-text").mkdir()
snapshot_path = "docs/hel-892/batch179-target-snapshots/incoming/ssh-snapshot.stdout.json"
snapshot_bytes = (ROOT / snapshot_path).read_bytes()
core = json.loads(snapshot_bytes)["installations"][0]["core"]
assert core["path"] == "/media/ComfyUI" and core["head"] == COMMIT
commit, _ = request("commit", "/git/commits/" + COMMIT)
assert commit["sha"] == COMMIT and commit["tree"]["sha"] == core["tree"]
tree, _ = request("root-tree", "/git/trees/" + core["tree"])
assert tree["sha"] == core["tree"] and not tree["truncated"]
entries = {x["path"]: x for x in tree["tree"]}
subtree, _ = request("execution-tree", "/git/trees/" + entries["comfy_execution"]["sha"])
assert subtree["sha"] == entries["comfy_execution"]["sha"] and not subtree["truncated"]
children = {x["path"]: x for x in subtree["tree"]}
rows = []
for index, path in enumerate((*PATHS, "LICENSE")):
key = "text-" + str(index)
item, receipt = request(key, "/contents/" + path + "?ref=" + COMMIT)
entry = children[path.split("/")[1]] if "/" in path else entries[path]
assert item["path"] == path and item["type"] == "file" and item["encoding"] == "base64"
assert entry["type"] == "blob" and item["sha"] == entry["sha"]
data = base64.b64decode(item["content"], validate=False)
assert len(data) <= 262144 and len(data) == item["size"] == entry["size"]
assert (
hashlib.sha1(b"blob " + str(len(data)).encode() + b"\0" + data).hexdigest()
== item["sha"]
)
data.decode("utf-8")
row = dict(
repo=REPO,
commit=COMMIT,
tree=core["tree"],
path=path,
git_blob_sha1=item["sha"],
bytes=len(data),
sha256=sha(data),
request_key=key,
retrieved_at_utc=receipt["completed_at_utc"],
file="source-text/" + path.replace("/", "__") + ".txt",
)
if path in PATHS:
original = next(x for x in core["files"] if x["relative_path"] == path)
for field in ("bytes", "sha256", "git_blob_sha1"):
assert row[field] == original[field], (path, field, "snapshot mismatch; stop")
row.update(
snapshot=original,
snapshot_file=snapshot_path,
snapshot_sha256=sha(snapshot_bytes),
sampling_time_only=True,
current_process_loaded="unknown",
)
else:
row["license_only_not_working_file"] = True
write(row["file"], data)
rows.append(row)
save("identity-" + str(index) + ".json", row)
print(path, len(data), sha(data), "snapshot match" if path in PATHS else "license only")
save("identities.json", rows)
if __name__ == "__main__":
main()