verify_release.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import hashlib
  2. import json
  3. from pathlib import Path
  4. import subprocess
  5. import sys
  6. import tempfile
  7. import zipfile
  8. root = Path(__file__).resolve().parents[2]
  9. sys.path.insert(0, str(root / "scripts"))
  10. import sync_package
  11. version = (root / "VERSION").read_text().strip()
  12. document = (root / "references" / "doc-snapshot.txt").read_text(encoding="utf-8")
  13. fingerprint = hashlib.sha1(sync_package.rule_check.normalize(document).encode("utf-8")).hexdigest()[:12]
  14. output = root.parent / "dist"
  15. archive = Path(sync_package.make_zip(str(root), str(output), version))
  16. with zipfile.ZipFile(archive) as bundle:
  17. count = len(bundle.infolist())
  18. sync_package.update_install_note(str(root), version, count, fingerprint)
  19. archive = Path(sync_package.make_zip(str(root), str(output), version))
  20. extracted = Path(tempfile.mkdtemp(prefix="sku-audit-v114-"))
  21. with zipfile.ZipFile(archive) as bundle:
  22. names = bundle.namelist()
  23. assert len(names) == count
  24. assert bundle.testzip() is None
  25. assert not any(name.endswith("credentials.json") or "__pycache__" in name or "/.git/" in name for name in names)
  26. assert bundle.read("sku_audit/VERSION").decode().strip() == version
  27. for name in names:
  28. parts = Path(name).parts
  29. assert not Path(name).is_absolute() and ".." not in parts
  30. bundle.extractall(extracted)
  31. workspace = extracted / root.name
  32. coverage = subprocess.run([sys.executable, "-B", "scripts/audit.py", "--coverage"], cwd=workspace,
  33. check=True, capture_output=True, text=True, encoding="utf-8")
  34. print("\n".join(coverage.stdout.splitlines()[:3]))
  35. check = (
  36. "from pathlib import Path;import sys;sys.path.insert(0,'scripts');import rule_check,audit;"
  37. "r=rule_check.cmd_verify(Path('references/doc-snapshot.txt').read_text(encoding='utf-8'),audit.pick_workbook(),quiet=True);"
  38. "print({k:r[k] for k in ('ok','warn','bad')});raise SystemExit(bool(r['bad']))"
  39. )
  40. verified = subprocess.run([sys.executable, "-B", "-c", check], cwd=workspace, check=True,
  41. capture_output=True, text=True, encoding="utf-8")
  42. print("extracted rule verification", verified.stdout.strip())
  43. digest = hashlib.sha256(archive.read_bytes()).hexdigest()
  44. archive.with_suffix(".zip.sha256").write_text(digest + " " + archive.name + "\n", encoding="utf-8")
  45. print(json.dumps({"version": version, "archive": str(archive), "files": count,
  46. "size_bytes": archive.stat().st_size, "zip_crc": "pass", "credentials_included": False,
  47. "extracted_checks": "pass", "sha256": digest}, ensure_ascii=False))