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

150 lines
5.4 KiB
Python

"Acquire necessary pinned text only; no upstream imports or execution."
import datetime
import hashlib
import json
import shutil
import subprocess
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
OUT = Path(__file__).resolve().parent
REF = OUT.parent / "flux2-img-reference"
def main():
old = REF.parent / "flux2-reference"
rows = []
for e in json.loads((old / "sources.json").read_bytes())["sources"]:
if e["id"] not in ["core", "tt"]:
continue
shutil.copyfile(old / e["local_file"], REF / e["local_file"])
rows.append(dict(e, applicability="本图独立声明core0.8.2或TT完整提交;目标安装/恢复未知"))
for key in ["core", "tt"]:
for p in old.glob(key + "-*"):
shutil.copyfile(p, REF / p.name)
specs = [
(
"lora",
"Comfy-Org/ComfyUI",
"v0.21.1",
["nodes.py", "folder_paths.py", "comfy/sd.py", "LICENSE"],
),
(
"frontend",
"Comfy-Org/ComfyUI_frontend",
"v1.36.13",
[
"package.json",
"pnpm-lock.yaml",
"src/scripts/widgets.ts",
"src/scripts/app.ts",
"src/extensions/core/widgetInputs.ts",
"src/extensions/core/rerouteNode.ts",
"src/extensions/core/noteNode.ts",
"src/extensions/core/uploadImage.ts",
"src/lib/litegraph/src/LGraphNode.ts",
"src/lib/litegraph/src/Reroute.ts",
"src/utils/migration/migrateReroute.ts",
"src/utils/executionUtil.ts",
"src/lib/litegraph/LICENSE",
"LICENSE",
],
),
]
for key, repo, tag, files in specs:
cache = ROOT / ".local-evidence" / ("flux2-img-" + key + "-git")
cache.mkdir(parents=True, exist_ok=True)
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(*args):
return subprocess.check_output(["git", "-C", str(cache), *args])
rev = git("rev-parse", "FETCH_HEAD^{commit}").decode().strip()
tree = git("rev-parse", rev + "^{tree}").decode().strip()
(REF / (key + "-commit.txt")).write_bytes(git("cat-file", "commit", rev))
(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=rev,
object=git("rev-parse", "FETCH_HEAD").decode().strip(),
),
indent=2,
)
+ "\n"
)
entries = []
for line in git("ls-tree", "-r", rev).decode().splitlines():
head, path = line.split("\t", 1)
mode, kind, sha = head.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"
)
missing = []
for f in files:
ent = next((x for x in entries if x["path"] == f), None)
if ent is None:
missing.append(f)
continue
raw = git("show", rev + ":" + f)
name = key + "__" + f.replace("/", "__")
(REF / name).write_bytes(raw)
rows.append(
dict(
id=key,
repo=repo,
file=f,
local_file=name,
commit=rev,
tree=tree,
tag=tag,
url=f"https://raw.githubusercontent.com/{repo}/{rev}/{f}",
sha256=hashlib.sha256(raw).hexdigest(),
git_blob_sha1=ent["sha"],
acquisition_time=datetime.datetime.now(datetime.timezone.utc).isoformat(),
license_file=key + "__LICENSE",
transport="filtered git; selected text blobs only",
applicability="本图独立声明版本;UE/tab/宿主恢复未知",
)
)
(REF / (key + "-absent.json")).write_text(json.dumps(missing, indent=2) + "\n")
for key, node, version in [
("kj", "comfyui-kjnodes", "1.3.9"),
("rgthree", "rgthree-comfy", "1.0.2604070017"),
]:
url = f"https://api.comfy.org/nodes/{node}/versions/{version}"
try:
with urllib.request.urlopen(url, timeout=30) as r:
raw = r.read(200001)
assert len(raw) <= 200000
(REF / (key + "-registry.json")).write_bytes(raw)
print(key, raw[:1000])
except Exception as e:
(REF / (key + "-registry-unavailable.json")).write_text(
json.dumps(dict(url=url, error=str(e)))
)
(REF / "sources.json").write_text(
json.dumps(dict(sources=rows), ensure_ascii=False, indent=2) + "\n"
)
if __name__ == "__main__":
main()