Files
aigc-home/docs/hel-892/flux2-restore-document-evidence/bind_sources.py
T

159 lines
6.4 KiB
Python

"""Rebind exact local text; acquire only new Primitive and subgraph source text."""
import hashlib
import json
import os
import subprocess
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
REF = ROOT / "docs/hel-892/flux2-restore-reference"
def main():
rows = []
for stem, key in [
("flux2-img", "core"),
("flux2-img", "lora"),
("flux2-img", "frontend"),
("flux2-img", "rgthree"),
("resize-compare", "layer"),
]:
origin = REF.parent / (stem + "-reference")
for old in json.loads((origin / "sources.json").read_bytes())["sources"]:
if old["id"] != key:
continue
if key == "rgthree" and old["file"] == "web/comfyui/label.js":
continue
e = dict(old)
e["local_file"] = os.path.relpath((origin / e["local_file"]).resolve(), REF)
e["license_file"] = os.path.relpath(
(origin / old.get("license_file", key + "__LICENSE")).resolve(), REF
)
e["identity_root"] = os.path.relpath(origin, REF)
e["identity_key"] = key
e["applicability"] = (
"仅本图静态参考;core0.8.2/LoRA0.21.1/frontend1.36.13/比较器准确发布;Layer无身份,89014d仅假设;UE/tab/代理恢复及安装未知"
)
if key == "layer":
# The API index SHA is a requested ref, not the real Git tree.
e["tree_index_ref"] = json.loads((origin / "layer-tree.json").read_bytes())["sha"]
meta = REF / "layer-commit-meta.json"
if not meta.exists():
with urllib.request.urlopen(
"https://api.github.com/repos/chflame163/ComfyUI_LayerStyle/commits/89014d833d2ad0998d724090eff1456692692a1d",
timeout=30,
) as response:
meta.write_bytes(response.read())
e["tree"] = json.loads(meta.read_bytes())["commit"]["tree"]["sha"]
e["repo"] = "chflame163/ComfyUI_LayerStyle"
e["tag"] = "无源版本,仅显式89014d参考假设"
assert hashlib.sha256((REF / e["local_file"]).read_bytes()).hexdigest() == e["sha256"]
rows.append(e)
# Acquire exact core0.28.3 commit and selected Git objects, without executing upstream.
key, repo, tag = "primitive", "Comfy-Org/ComfyUI", "v0.28.3"
cache = ROOT / ".local-evidence/flux2-restore-primitive-git"
if not cache.exists():
subprocess.run(["git", "init", "--bare", str(cache)], check=True, stdout=subprocess.DEVNULL)
subprocess.run(
[
"git",
"-C",
str(cache),
"fetch",
"--filter=blob:none",
"--depth=1",
"https://github.com/" + repo + ".git",
"refs/tags/" + tag,
],
check=True,
)
def git(*a):
return subprocess.check_output(["git", "-C", str(cache), *a])
commit = git("rev-parse", "FETCH_HEAD^{commit}").decode().strip()
tree = git("rev-parse", commit + "^{tree}").decode().strip()
(REF / (key + "-commit.txt")).write_bytes(git("cat-file", "commit", commit))
(REF / (key + "-tag.txt")).write_bytes(git("cat-file", "-p", "FETCH_HEAD"))
(REF / (key + "-tag.json")).write_text(
json.dumps(
dict(
ref="refs/tags/" + tag,
commit=commit,
object=git("rev-parse", "FETCH_HEAD").decode().strip(),
),
indent=2,
)
+ "\n"
)
entries = []
for line in git("ls-tree", "-r", commit).decode().splitlines():
meta, path = line.split("\t", 1)
mode, kind, sha = meta.split()
entries.append(dict(path=path, mode=mode, type=kind, sha=sha))
(REF / (key + "-tree.json")).write_text(
json.dumps(dict(sha=tree, truncated=False, tree=entries), indent=2) + "\n"
)
for file in ["comfy_extras/nodes_primitive.py", "nodes.py", "LICENSE"]:
raw = git("show", commit + ":" + file)
local = key + "__" + file.replace("/", "__")
(REF / local).write_bytes(raw)
rows.append(
dict(
id=key,
repo=repo,
file=file,
local_file=local,
commit=commit,
tree=tree,
tag=tag,
url=f"https://raw.githubusercontent.com/{repo}/{commit}/{file}",
sha256=hashlib.sha256(raw).hexdigest(),
git_blob_sha1=hashlib.sha1(
b"blob " + str(len(raw)).encode() + b"\0" + raw
).hexdigest(),
identity_root=".",
identity_key=key,
license_file=key + "__LICENSE",
applicability="仅根152 Primitive和宿主150声明0.28.3;不赋予内部core新身份",
)
)
base = next(e for e in rows if e["id"] == "frontend")
tree_index = json.loads((REF / base["identity_root"] / "frontend-tree.json").read_bytes())
for file in [
"src/lib/litegraph/src/subgraph/SubgraphNode.ts",
"src/lib/litegraph/src/subgraph/Subgraph.ts",
"src/lib/litegraph/src/subgraph/SubgraphInput.ts",
"src/lib/litegraph/src/subgraph/SubgraphOutput.ts",
"src/lib/litegraph/src/subgraph/SubgraphInputNode.ts",
"src/lib/litegraph/src/subgraph/SubgraphOutputNode.ts",
]:
e = dict(base)
local = "frontend__" + file.replace("/", "__")
path = REF / local
url = f"https://raw.githubusercontent.com/{e['repo']}/{e['commit']}/{file}"
if not path.exists():
with urllib.request.urlopen(url, timeout=30) as response:
path.write_bytes(response.read())
raw = path.read_bytes()
blob = hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
assert next(t["sha"] for t in tree_index["tree"] if t["path"] == file) == blob
e.update(
file=file,
local_file=local,
url=url,
sha256=hashlib.sha256(raw).hexdigest(),
git_blob_sha1=blob,
)
rows.append(e)
(REF / "sources.json").write_text(
json.dumps(dict(sources=rows, upstream_executed=False), ensure_ascii=False, indent=2) + "\n"
)
print("Bound", len(rows), "necessary texts")
if __name__ == "__main__":
main()