144 lines
4.9 KiB
Python
144 lines
4.9 KiB
Python
"Range-read exact registry ZIP directory and selected necessary text members."
|
|
|
|
import datetime
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import urllib.request
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
REF = Path(__file__).resolve().parent.parent / "zimage-hd-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():
|
|
doc = json.loads((REF / "sources.json").read_bytes())
|
|
for key, wanted in [
|
|
("kjpkg", ["__init__.py", "nodes/nodes.py", "LICENSE", "pyproject.toml"]),
|
|
(
|
|
"rgpkg",
|
|
[
|
|
"__init__.py",
|
|
"py/image_comparer.py",
|
|
"py/constants.py",
|
|
"web/comfyui/image_comparer.js",
|
|
"LICENSE",
|
|
"pyproject.toml",
|
|
"__commit__.py",
|
|
],
|
|
),
|
|
("encryptpkg", ["__init__.py", "LICENSE", "pyproject.toml"]),
|
|
]:
|
|
m = json.loads((REF / (key + "-registry.json")).read_bytes())
|
|
r = Remote(m["downloadUrl"])
|
|
with zipfile.ZipFile(r) as z:
|
|
infos = [
|
|
dict(
|
|
name=i.filename,
|
|
crc=i.CRC,
|
|
size=i.file_size,
|
|
compressed=i.compress_size,
|
|
offset=i.header_offset,
|
|
)
|
|
for i in z.infolist()
|
|
]
|
|
(REF / (key + "-members.json")).write_text(json.dumps(infos, indent=2) + "\n")
|
|
for f in wanted:
|
|
if f not in z.namelist():
|
|
print("MISSING", key, f)
|
|
continue
|
|
raw = z.read(f)
|
|
raw.decode()
|
|
name = key + "__" + f.replace("/", "__")
|
|
(REF / name).write_bytes(raw)
|
|
doc["sources"].append(
|
|
dict(
|
|
id=key,
|
|
file=f,
|
|
local_file=name,
|
|
url=m["downloadUrl"],
|
|
commit=f"{m['node_id']}@{m['version']};Git绑定未知",
|
|
tag=m["version"],
|
|
tree="发布包中央目录;Git tree未知",
|
|
repo=m["node_id"],
|
|
git_commit_binding="unknown",
|
|
package_sha256="unknown: Range仅必要文本,未获取完整包",
|
|
sha256=hashlib.sha256(raw).hexdigest(),
|
|
member_crc32=z.getinfo(f).CRC,
|
|
acquisition_time=datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
|
license_file=key + "__LICENSE",
|
|
identity_root=".",
|
|
identity_key=key,
|
|
transport=(
|
|
"HTTP206 Range selected text ZIP members; CRC checked by zipfile"
|
|
),
|
|
applicability="本图准确cnr_id/ver;不证明目标安装或Label身份",
|
|
)
|
|
)
|
|
(REF / (key + "-range-proof.json")).write_text(
|
|
json.dumps(
|
|
dict(
|
|
url=r.url,
|
|
size=r.size,
|
|
headers=r.headers,
|
|
ranges=r.log,
|
|
package_sha256=("unknown; full archive intentionally not fetched"),
|
|
git_binding="unknown",
|
|
),
|
|
indent=2,
|
|
)
|
|
+ "\n"
|
|
)
|
|
print(
|
|
key,
|
|
"size",
|
|
r.size,
|
|
"ranges",
|
|
len(r.log),
|
|
"bytes",
|
|
sum(x["end"] - x["start"] + 1 for x in r.log),
|
|
)
|
|
(REF / "sources.json").write_text(json.dumps(doc, ensure_ascii=False, indent=2) + "\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|