Files
aigc-home/docs/hel-892/krea-auto-document-evidence/fetch_release.py
T

109 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Range-read exact release necessary text only; never install or import upstream."""
import datetime
import hashlib
import io
import json
import urllib.request
import zipfile
from pathlib import Path
REF = Path(__file__).resolve().parent.parent / "krea-auto-reference"
class Remote(io.RawIOBase):
def __init__(self, url):
self.url = url
self.pos = 0
self.log = []
with urllib.request.urlopen(urllib.request.Request(url, method="HEAD"), timeout=30) as r:
self.size = int(r.headers["Content-Length"])
self.headers = dict(r.headers)
def seekable(self):
return True
def readable(self):
return True
def tell(self):
return self.pos
def seek(self, off, whence=0):
self.pos = off if whence == 0 else self.pos + off if whence == 1 else self.size + off
return self.pos
def read(self, n=-1):
n = min(self.size - self.pos, n if n >= 0 else self.size - self.pos)
if n <= 0:
return b""
a = self.pos
b = a + n - 1
req = urllib.request.Request(self.url, headers={"Range": f"bytes={a}-{b}"})
with urllib.request.urlopen(req, timeout=40) as r:
assert r.status == 206 and r.headers["Content-Range"].startswith(f"bytes {a}-{b}/")
raw = r.read(n + 1)
assert len(raw) == n
self.pos += n
self.log.append(dict(start=a, end=b, sha256=hashlib.sha256(raw).hexdigest()))
return raw
def main():
url = "https://api.comfy.org/nodes/comfyui-custom-scripts/versions/1.2.5"
with urllib.request.urlopen(url, timeout=30) as r:
raw = r.read(500000)
(REF / "showtext-registry.json").write_bytes(raw)
meta = json.loads(raw)
archive = Remote(meta.get("downloadUrl", meta.get("download_url")))
rows = []
members = []
now = datetime.datetime.now(datetime.timezone.utc).isoformat()
with zipfile.ZipFile(archive) as z:
for info in z.infolist():
members.append(dict(name=info.filename, size=info.file_size, crc=info.CRC))
f = info.filename
if f not in [
"py/show_text.py",
"web/js/showText.js",
"__init__.py",
"LICENSE",
"pyproject.toml",
]:
continue
data = z.read(f)
name = "showtext__" + f.replace("/", "__")
(REF / name).write_bytes(data)
rows.append(
dict(
id="showtext",
file=f,
local_file=name,
tag="1.2.5",
commit="发布成员到Git绑定未知",
tree="未知",
url=archive.url,
sha256=hashlib.sha256(data).hexdigest(),
package_sha256="unknown-not-downloaded-whole-archive",
member_crc32=info.CRC,
acquisition_time=now,
reverified_at=now,
license_file="showtext__LICENSE",
identity_root=".",
identity_key="showtext",
applicability=(
"本图ShowText准确1.2.5;Range必要文本,不借旧S" + "how31/Git609f身份"
),
)
)
(REF / "showtext-members.json").write_text(json.dumps(members, indent=2) + "\n")
(REF / "showtext-range-proof.json").write_text(json.dumps(archive.log, indent=2) + "\n")
(REF / "showtext-sources.json").write_text(
json.dumps(dict(sources=rows), ensure_ascii=False, indent=2) + "\n"
)
print("PASS Range necessary release texts", len(rows), "whole package not acquired")
if __name__ == "__main__":
main()