"""校验交付包:哈希、签名、外键、主键、行数、取值区间。 用法(在解压后的包目录里,或用绝对路径指定): python verify.py # 自动找 04-manifest.json / 01-数据集 / 03-复现/public.pem python verify.py --manifest 04-manifest.json --data 01-数据集 python verify.py --no-signature # 没有 cryptography 时先跳过签名 退出码:0 全部通过;1 有检查失败;2 包结构不全,无法校验。 只依赖 Python 标准库(签名校验可选依赖 cryptography)。 """ import argparse import csv import hashlib import io import json import sys from pathlib import Path OK, FAIL = "\u901a\u8fc7", "\u5931\u8d25" # 中文输出用转义,避免个别控制台编码问题 def sha256_bytes(data: bytes) -> str: return hashlib.sha256(data).hexdigest() def sha256_file(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as fh: for block in iter(lambda: fh.read(1024 * 1024), b""): digest.update(block) return digest.hexdigest() def read_csv(path: Path): with io.open(path, encoding="utf-8-sig", newline="") as fh: return list(csv.DictReader(fh)) class Report: def __init__(self): self.items = [] def check(self, name, ok, expect="", actual="", note=""): self.items.append((name, bool(ok), expect, actual, note)) mark = "PASS" if ok else "FAIL" line = f"[{mark}] {name}" if expect or actual: line += f"\uff1a\u671f\u671b {expect}\uff0c\u5b9e\u9645 {actual}" if note: line += f" ({note})" print(line) return ok def passed(self): return all(i[1] for i in self.items) def summary(self): total = len(self.items) bad = [i[0] for i in self.items if not i[1]] print(f"\n\u5171 {total} \u9879\uff0c\u901a\u8fc7 {total - len(bad)}\uff0c\u5931\u8d25 {len(bad)}") if bad: print("\u5931\u8d25\u9879\uff1a" + "\u3001".join(bad)) print("ALL CHECKS FAILED") return 1 print("ALL CHECKS PASSED") return 0 def find_default(root: Path): manifest = root / "04-manifest.json" data = root / "01-数据集" sig = root / "04-manifest.json.sig" pub = root / "03-复现" / "public.pem" return manifest, data, sig, pub def main(): ap = argparse.ArgumentParser() ap.add_argument("--manifest", default="") ap.add_argument("--data", default="") ap.add_argument("--sig", default="") ap.add_argument("--pub", default="") ap.add_argument("--root", default=".") ap.add_argument("--no-signature", action="store_true") a = ap.parse_args() root = Path(a.root).resolve() d_manifest, d_data, d_sig, d_pub = find_default(root) manifest_path = Path(a.manifest) if a.manifest else d_manifest data_dir = Path(a.data) if a.data else d_data sig_path = Path(a.sig) if a.sig else d_sig pub_path = Path(a.pub) if a.pub else d_pub if not manifest_path.exists() or not data_dir.is_dir(): print("\u627e\u4e0d\u5230 04-manifest.json \u6216 01-\u6570\u636e\u96c6\u76ee\u5f55\uff0c\u8bf7\u786e\u8ba4\u662f\u5426\u5728\u5305\u76ee\u5f55\u91cc\u8fd0\u884c") return 2 manifest = json.loads(manifest_path.read_text(encoding="utf-8")) rep = Report() # 1. 逐文件:字节数 + sha256 files = manifest.get("files") if files: for item in files: p = data_dir / item["path"] if not p.exists(): rep.check(f"\u6587\u4ef6\u5b58\u5728 {item['path']}", False, "\u5b58\u5728", "\u7f3a\u5931") continue size = p.stat().st_size digest = sha256_file(p) rep.check( f"\u5b57\u8282\u6570 {item['path']}", size == item["bytes"], item["bytes"], size) rep.check( f"SHA256 {item['path']}", digest == item["sha256"], item["sha256"][:16] + "\u2026", digest[:16] + "\u2026") else: # 兼容老包:用 file_sha256 映射 for name, expect in (manifest.get("file_sha256") or {}).items(): p = data_dir / f"{name}.csv" if p.exists(): rep.check(f"SHA256 {name}.csv", sha256_file(p) == expect, expect[:16] + "\u2026", sha256_file(p)[:16] + "\u2026") # 2. combined / bundle 复算 actual_hashes = {} for p in sorted(data_dir.glob("*.csv")): actual_hashes[p.stem] = sha256_file(p) combined_expect = manifest.get("combined_sha256") if combined_expect and actual_hashes: combined_actual = sha256_bytes( "".join(f"{n}:{h}" for n, h in sorted(actual_hashes.items())).encode()) rep.check("combined_sha256(多表 combined 口径)", combined_actual == combined_expect, combined_expect[:16] + "\u2026", combined_actual[:16] + "\u2026") dataset_expect = manifest.get("dataset_sha256") if dataset_expect: main_file = manifest.get("dataset_file") or "dataset.csv" p = data_dir / main_file if p.exists(): rep.check(f"dataset_sha256(单文件 {main_file})", sha256_file(p) == dataset_expect, dataset_expect[:16] + "\u2026", sha256_file(p)[:16] + "\u2026") else: rep.check(f"dataset_sha256 主文件存在 {main_file}", False, "存在", "缺失") bundle_expect = manifest.get("bundle_sha256") if bundle_expect: entries = sorted( (p.relative_to(data_dir).as_posix(), sha256_file(p)) for p in data_dir.rglob("*") if p.is_file()) bundle_actual = sha256_bytes( "".join(f"{path}:{h}\n" for path, h in entries).encode()) rep.check("bundle_sha256", bundle_actual == bundle_expect, bundle_expect[:16] + "\u2026", bundle_actual[:16] + "\u2026", "\u53e3\u5f84\uff1a\u6587\u4ef6\u76f8\u5bf9\u8def\u5f84\u6392\u5e8f\u540e 'path:sha256' \u9010\u884c\u62fc\u63a5") # 3. 签名 signer = manifest.get("signer") or {} if a.no_signature: print("[SKIP] \u7b7e\u540d\u6821\u9a8c\uff08\u6309\u53c2\u6570\u8df3\u8fc7\uff09") elif not (sig_path.exists() and pub_path.exists()): rep.check("\u7b7e\u540d\u6587\u4ef6\u5c31\u4f4d", False, "04-manifest.json.sig + 03-\u590d\u73b0/public.pem", "\u7f3a\u5931") else: try: from cryptography.exceptions import InvalidSignature from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey except Exception: print("[SKIP] \u7b7e\u540d\u6821\u9a8c\u9700\u8981 cryptography\uff1apip install cryptography") else: pub = serialization.load_pem_public_key(pub_path.read_bytes()) if not isinstance(pub, Ed25519PublicKey): rep.check("\u516c\u94a5\u7c7b\u578b", False, "Ed25519", type(pub).__name__) else: der = pub.public_bytes( encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo) fp = sha256_bytes(der) expect_fp = (signer.get("pubkey_sha256") or "").strip() if expect_fp: rep.check("\u516c\u94a5\u6307\u7eb9\u4e0e\u6e05\u5355\u4e00\u81f4", fp == expect_fp, expect_fp[:16] + "\u2026", fp[:16] + "\u2026") try: pub.verify(sig_path.read_bytes(), manifest_path.read_bytes()) rep.check("\u6e05\u5355 Ed25519 \u7b7e\u540d", True, "\u9a8c\u7b7e\u901a\u8fc7", "\u9a8c\u7b7e\u901a\u8fc7") except InvalidSignature: rep.check("\u6e05\u5355 Ed25519 \u7b7e\u540d", False, "\u9a8c\u7b7e\u901a\u8fc7", "\u7b7e\u540d\u4e0d\u5339\u914d\uff08\u6e05\u5355\u88ab\u6539\u6216\u516c\u94a5\u4e0d\u5bf9\uff09") # 4. 数据检查:行数 / 外键 / 主键唯一 / 非空 / 区间 checks = manifest.get("checks") or {} tables = {} def table(name): if name not in tables: path = data_dir / f"{name}.csv" tables[name] = read_csv(path) if path.exists() else None return tables[name] for name, expect in (checks.get("row_counts") or manifest.get("tables") or {}).items(): rows = table(name) if rows is None: rep.check(f"\u884c\u6570 {name}", False, expect, "\u8868\u6587\u4ef6\u7f3a\u5931") else: exp = expect["rows"] if isinstance(expect, dict) else expect rep.check(f"\u884c\u6570 {name}", len(rows) == exp, exp, len(rows)) for rel in checks.get("relations") or []: child, cfield = rel["from_table"], rel["from_field"] parent, pfield = rel["to_table"], rel["to_field"] crows, prows = table(child), table(parent) if crows is None or prows is None: rep.check(f"\u5916\u952e {child}.{cfield} -> {parent}.{pfield}", False, "0 \u5b64\u513f", "\u8868\u7f3a\u5931") continue keys = {r.get(pfield, "") for r in prows} orphans = [r for r in crows if r.get(cfield, "") not in keys] rep.check(f"\u5916\u952e\u5b64\u513f {child}.{cfield} -> {parent}.{pfield}", len(orphans) == 0, 0, len(orphans), "\u9996\u4e2a\u5b64\u513f\uff1a" + (str(orphans[0]) if orphans else "\u65e0")) for item in checks.get("unique") or []: name, col = item["table"], item["column"] rows = table(name) if rows is None: continue vals = [r.get(col, "") for r in rows] dup = len(vals) - len(set(vals)) rep.check(f"\u4e3b\u952e\u552f\u4e00 {name}.{col}", dup == 0, 0, dup) for item in checks.get("not_null") or []: name, col = item["table"], item["column"] rows = table(name) or [] empties = sum(1 for r in rows if not str(r.get(col, "")).strip()) rep.check(f"\u975e\u7a7a {name}.{col}", empties == 0, 0, empties) for item in checks.get("ranges") or []: name, col = item["table"], item["column"] lo, hi = item["min"], item["max"] rows = table(name) or [] out = 0 for r in rows: try: v = float(str(r.get(col, "")).replace(",", "")) except ValueError: out += 1 continue if v < lo or v > hi: out += 1 rep.check(f"\u53d6\u503c\u533a\u95f4 {name}.{col} [{lo}, {hi}]", out == 0, 0, out) return rep.summary() if __name__ == "__main__": sys.exit(main())