Files
aigc-home/docs/hel-892/tts-clone-document-evidence/fetch_sources.py
T
2026-10-31 04:00:40 +08:00

130 lines
4.1 KiB
Python

"""Fetch only pinned upstream text; never import upstream code."""
import datetime
import hashlib
import json
import urllib.request
from pathlib import Path
OUT = Path(__file__).resolve().parents[1] / "tts-clone-reference"
SPECS = {
"qwen": (
"AICoderTudou/ComfyUI-TD-Qwen3TTS",
"f5c78cd3a7ef1ca030d64d36801adad7413aded6",
["__init__.py", "nodes.py", "LICENSE"],
),
"layer": (
"chflame163/ComfyUI_LayerStyle",
"89014d833d2ad0998d724090eff1456692692a1d",
["__init__.py", "py/purge_vram.py", "py/imagefunc.py", "LICENSE"],
),
}
def get(url):
with urllib.request.urlopen(
urllib.request.Request(url, headers={"User-Agent": "offline-source-review"}), timeout=40
) as r:
return r.read()
def fetch(spec):
key, (repo, rev, files) = spec
base = f"https://api.github.com/repos/{repo}/git/"
tree_path = OUT / (key + "-tree.json")
tree = (
tree_path.read_bytes()
if tree_path.exists()
else get(base + f"trees/{rev}?recursive=1&review=hel892clone")
)
t = json.loads(tree)
assert not t["truncated"]
(OUT / (key + "-tree.json")).write_bytes(tree)
commit_path = OUT / (key + "-commit.json")
commit = (
commit_path.read_bytes()
if commit_path.exists()
else get(base + f"commits/{rev}?review=hel892clone")
)
(OUT / (key + "-commit.json")).write_bytes(commit)
rows = []
previous = (
json.loads((OUT / "sources.json").read_bytes())["sources"]
if (OUT / "sources.json").exists()
else []
)
for f in files:
url = f"https://raw.githubusercontent.com/{repo}/{rev}/{f}"
prior = next((e for e in previous if e["id"] == key and e["file"] == f), None)
name = prior["local_file"] if prior else key + "__" + f.replace("/", "__")
cached = OUT / name
raw = cached.read_bytes() if cached.exists() else get(url)
blob = next(e["sha"] for e in t["tree"] if e["path"] == f)
assert hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest() == blob
if not cached.exists():
cached.write_bytes(raw)
rows.append(
dict(
id=key,
repo=repo,
file=f,
local_file=name,
commit=rev,
tree=json.loads(commit)["tree"]["sha"],
url=url,
sha256=hashlib.sha256(raw).hexdigest(),
git_blob_sha1=blob,
verified_at=datetime.datetime.now(datetime.timezone.utc).isoformat(),
acquisition_time="unknown for reused cache; verification time above",
cached_bytes_reverified=True,
license_file=next(
(
e["local_file"]
for e in previous
if e["id"] == key and e["file"] == "LICENSE"
),
key + "__LICENSE",
),
applicability="源声明准确提交;发布包/目标安装绑定未知",
transport="GitHub pinned raw text; blob verified",
)
)
return rows
def main():
OUT.mkdir(parents=True, exist_ok=True)
tag_path = OUT / "core0100-tag.json"
tag = (
tag_path.read_bytes()
if tag_path.exists()
else get(
"https://api.github.com/repos/Comfy-Org/ComfyUI/git/ref/tags/v0.10.0?review=hel892clone"
)
)
tag_path.write_bytes(tag)
commit = json.loads(tag)["object"]["sha"]
assert json.loads(tag)["object"]["type"] == "commit"
SPECS["core"] = (
"Comfy-Org/ComfyUI",
commit,
[
"comfy_extras/nodes_audio.py",
"comfy_api/latest/_ui.py",
"comfy_api/latest/_io.py",
"nodes.py",
"folder_paths.py",
"LICENSE",
],
)
rows = []
for spec in SPECS.items():
rows += fetch(spec)
(OUT / "sources.json").write_text(
json.dumps(dict(sources=rows), ensure_ascii=False, indent=2) + "\n"
)
if __name__ == "__main__":
main()