"""Package a clean pushed branch; byte/SHA/CRC verification of an independent ZIP.""" import hashlib import json import socket import subprocess import urllib.request import zipfile from build_definition import OUT, PATHS, REPO from rebuild import BASE def git(*args): return subprocess.check_output(["git", *args], cwd=REPO).decode().strip() def sha(path): return hashlib.sha256(path.read_bytes()).hexdigest() def main(): target = REPO / ".local-evidence/kapi-delivery" target.mkdir(exist_ok=True) assert not git("status", "--porcelain") head, branch = git("rev-parse", "HEAD"), git("branch", "--show-current") remote = git("ls-remote", "origin", "refs/heads/" + branch).split()[0] assert head == remote and branch == "agent/agent/a64a969a6a44" with urllib.request.urlopen( "https://gitea.xbay.cc/api/v1/repos/leefer/aigc-home/pulls/25", timeout=45 ) as response: pr = json.load(response) assert pr["head"]["sha"] == head and pr["base"]["ref"] == "main" and pr["state"] == "open" index = json.loads((OUT / "file-purpose-sha256.json").read_bytes()) for row in index["files"]: assert sha(REPO / row["path"]) == row["sha256"], row["path"] changed = git("diff", "--name-only", "-z", BASE, head).strip("\0").split("\0") assert set(changed) == {r["path"] for r in index["files"]} | { str((OUT / "file-purpose-sha256.json").relative_to(REPO)) } files = {n: REPO / n for n in changed} files[PATHS[0]] = REPO / PATHS[0] browser = REPO / ".local-evidence/krea-api-document/browser" for p in browser.glob("*.png"): files["screenshots/" + p.name] = p assert len(list(browser.glob("*.png"))) == 40 for port in (8247, 5173): with socket.socket() as sock: sock.settimeout(1) assert sock.connect_ex(("127.0.0.1", port)) != 0 with socket.socket() as sock: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(("127.0.0.1", port)) (target / "complete.patch").write_bytes( subprocess.check_output(["git", "diff", "--binary", BASE, head], cwd=REPO) ) subprocess.run( ["git", "bundle", "create", str(target / "batch165.bundle"), branch, "^" + BASE], cwd=REPO, check=True, ) verified = subprocess.run( ["git", "bundle", "verify", str(target / "batch165.bundle")], cwd=REPO, capture_output=True, check=True, ) (target / "bundle-verify.txt").write_bytes(verified.stdout + verified.stderr) final = dict( base=BASE, head=head, remote_head=remote, branch=branch, pr="https://gitea.xbay.cc/leefer/aigc-home/pulls/25", pr_head=pr["head"]["sha"], pr_base="main", clean=True, pins=json.loads((OUT / "pins.json").read_bytes()), reference_documents=146, saved=10257, notes=287, remaining=9, full_documents=0, new_reliable_conversions=0, real_executions=0, independent_review="待HEL-893;自测不等于审核通过", services_stopped=True, ) (target / "final-head.json").write_text(json.dumps(final, ensure_ascii=False, indent=2) + "\n") (target / "pr-verification.json").write_text( json.dumps(pr, ensure_ascii=False, indent=2) + "\n" ) for n in [ "complete.patch", "batch165.bundle", "bundle-verify.txt", "final-head.json", "pr-verification.json", ]: files[n] = target / n for p in (REPO / ".local-evidence").glob("kapi-*push*.txt"): files["delivery-records/" + p.name] = p files["delivery-records/kapi-commit.txt"] = REPO / ".local-evidence/kapi-commit.txt" rows = [ dict( path=n, bytes=p.stat().st_size, sha256=sha(p), purpose="最终截图" if n.startswith("screenshots/") else "本份代码/原件/文档/验证或提交证据;详见file-purpose-sha256.json", ) for n, p in sorted(files.items()) ] (target / "bundle-purpose-sha256.json").write_text( json.dumps( dict(self_excluded=True, base=BASE, head=head, files=rows), ensure_ascii=False, indent=2 ) + "\n" ) files["bundle-purpose-sha256.json"] = target / "bundle-purpose-sha256.json" archive = target / "HEL-892-batch165-Krea2-API-evidence.zip" with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as z: for n, p in sorted(files.items()): z.write(p, n) with zipfile.ZipFile(archive) as z: assert z.testzip() is None for n, p in files.items(): assert z.read(n) == p.read_bytes() assert hashlib.sha256(z.read(n)).hexdigest() == sha(p) (target / "archive-sha256.txt").write_text(sha(archive) + "\n") print( "PASS", head, len(files), "members byte/SHA/CRC; ZIP", archive.stat().st_size, sha(archive) ) if __name__ == "__main__": main()