|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Materialize the last pre-cleanup PCEA research snapshot into ./migrated. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + python materialize_legacy.py |
| 6 | + python materialize_legacy.py --check |
| 7 | + python materialize_legacy.py --force |
| 8 | +
|
| 9 | +The source commit is immutable. Every file is verified using Git's blob SHA-1 |
| 10 | +before it is written. This script is research/provenance tooling, never PCEA |
| 11 | +runtime code. |
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import argparse |
| 16 | +import hashlib |
| 17 | +import pathlib |
| 18 | +import sys |
| 19 | +import urllib.parse |
| 20 | +import urllib.request |
| 21 | + |
| 22 | +SOURCE_REPO = "The-Interdependency/pcea" |
| 23 | +SOURCE_COMMIT = "ecf2ca0dec38bef29382e02121b0edde66763aa9" |
| 24 | +FILES = { |
| 25 | + "pcea-ucns/PLAN.md": "174018de5208397e191ed4b10aaa00c3cff43e84", |
| 26 | + "pcea-ucns/README.md": "fc1187dbaffd0a542287392e4e64a9553e3e42df", |
| 27 | + "pcea-ucns/asymmetric-paths-assessment.md": "708e68c4e00cc884c616333eae3df552981ac58f", |
| 28 | + "pcea-ucns/attack1_minkowski_break.py": "4378a28fdac9b59aa6bf2be1f11d0f2b5a359e84", |
| 29 | + "pcea-ucns/attack_harness.py": "79d70b4d3520a949bb75893fed7c30f41f972f38", |
| 30 | + "pcea-ucns/avenues.md": "d5ab74a84644b4e19095339ec5e5e8c2bdcf79bf", |
| 31 | + "pcea-ucns/candidate-ledger.json": "e0965b4b7fb24e83db5369dd16af29ff86c5bef6", |
| 32 | + "pcea-ucns/factor_count_sweep.py": "48ea5e802a660228733ec06f29cff6c99c437128", |
| 33 | + "pcea-ucns/feasibility-investigation.md": "2e2d7b6845990fd69e0f0989f9440cf6625dffbd", |
| 34 | + "pcea-ucns/gonal_architecture.py": "a24e31110521b30ca941bf151b99458a06c910af", |
| 35 | + "pcea-ucns/one_way_map_gate.py": "1cc2c3637dc93475a3f7edc19b7ecca68362d0ab", |
| 36 | + "pcea-ucns/option_d_ucns_map.py": "3bdc91e4975f3122cbfd3b6f5338049d4676d73c", |
| 37 | + "pcea-ucns/option_family_specs.py": "d5e7bcae866003c413119869d7be2ebd339892e3", |
| 38 | + "pcea-ucns/positional_attack.py": "30fce01b51bcb855105de7dc904a8ea7ed739919", |
| 39 | + "pcea-ucns/prefix_read_break.py": "2acd92b9fdd43f8b81662a154f76795664baf33c", |
| 40 | + "pcea-ucns/projection_action_candidate.py": "e9cdaa612c7c71b26456fcb9c2be908803e8f136", |
| 41 | + "pcea-ucns/pruning_scaling.py": "f820f87e4440a807739b8e948e629b13c1475671", |
| 42 | + "pcea-ucns/quotient_attack.py": "00d54912f14f0df52a16887671cf1759b155080d", |
| 43 | + "pcea-ucns/three_factor_attack.py": "e24a9618415316a1a9977d2cb9a9fdfe742e56e9", |
| 44 | + "pcea-ucns/ucns-crypto-domain-v0.md": "ffa79dc459e424a5f5654454b208bf1bc23a0b4d", |
| 45 | + "pcea-ucns/ucns_compat.py": "cdf8ee4996c390e5753003c1fc1b3b5e622a2fdb", |
| 46 | + "rec.md": "c2f576d7fb1077d78535d17672f05ac441114bce", |
| 47 | + "tests/test_attack1_minkowski_break.py": "291c392608f4acced36e31987b08aa1dd0cfc074", |
| 48 | + "tests/test_attack_harness.py": "06b776119d898ed7d0374ba3861a84a885310a9a", |
| 49 | + "tests/test_factor_count_sweep.py": "a6dd2fc8fb33cf179a57963b52075a8bc9472b58", |
| 50 | + "tests/test_gonal_architecture.py": "d622fc6e85543ad99ce3f374b4fbbc1971d82812", |
| 51 | + "tests/test_one_way_map_gate.py": "9e61ed890c53e4550100bade6f86b7f928b564c8", |
| 52 | + "tests/test_option_d_ucns_map.py": "641c08579ed34a769e43d4d199f331072b00ff7c", |
| 53 | + "tests/test_option_family_specs.py": "38922acd951468884b181d4f53ab0d42d4717374", |
| 54 | + "tests/test_positional_attack.py": "05b52ad0e6a791c70974d08e1ebc59d738d7ca11", |
| 55 | + "tests/test_prefix_read_break.py": "bcf4d57e32edf3669841948b92968372275b6e5c", |
| 56 | + "tests/test_projection_action_candidate.py": "1f841c57aff58b1a3ea0df5dd277add8d6dec677", |
| 57 | + "tests/test_pruning_scaling.py": "8db8d55799f1c050de2a6fb72625d0980b78bc4c", |
| 58 | + "tests/test_quotient_attack.py": "cb82d85de13e20d3ff14b5692164b0213a692c37", |
| 59 | + "tests/test_three_factor_attack.py": "7ed6d70c448630d2759a644547deeb1895e0108d", |
| 60 | + "tests/test_ucns_candidate_ledger.py": "3167d980fbeb5e285a056da59a595463f729b556", |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +def git_blob_sha(data: bytes) -> str: |
| 65 | + header = f"blob {len(data)}\0".encode("ascii") |
| 66 | + return hashlib.sha1(header + data).hexdigest() |
| 67 | + |
| 68 | + |
| 69 | +def raw_url(path: str) -> str: |
| 70 | + quoted = urllib.parse.quote(path) |
| 71 | + return f"https://raw.githubusercontent.com/{SOURCE_REPO}/{SOURCE_COMMIT}/{quoted}" |
| 72 | + |
| 73 | + |
| 74 | +def fetch(path: str) -> bytes: |
| 75 | + request = urllib.request.Request(raw_url(path), headers={"User-Agent": "stack-pcea-research-migration/1"}) |
| 76 | + with urllib.request.urlopen(request, timeout=30) as response: |
| 77 | + return response.read() |
| 78 | + |
| 79 | + |
| 80 | +def verify(path: pathlib.Path, expected: str) -> tuple[bool, str]: |
| 81 | + if not path.is_file(): |
| 82 | + return False, "missing" |
| 83 | + actual = git_blob_sha(path.read_bytes()) |
| 84 | + return actual == expected, actual |
| 85 | + |
| 86 | + |
| 87 | +def main() -> int: |
| 88 | + parser = argparse.ArgumentParser() |
| 89 | + parser.add_argument("--target", default="migrated", help="materialization root relative to this script") |
| 90 | + parser.add_argument("--check", action="store_true", help="verify existing files; do not access network") |
| 91 | + parser.add_argument("--force", action="store_true", help="overwrite an existing mismatched file") |
| 92 | + args = parser.parse_args() |
| 93 | + |
| 94 | + root = pathlib.Path(__file__).resolve().parent / args.target |
| 95 | + failures: list[str] = [] |
| 96 | + |
| 97 | + for source_path, expected in FILES.items(): |
| 98 | + destination = root / source_path |
| 99 | + ok, observed = verify(destination, expected) |
| 100 | + if args.check: |
| 101 | + if not ok: |
| 102 | + failures.append(f"{source_path}: expected {expected}, observed {observed}") |
| 103 | + continue |
| 104 | + if ok: |
| 105 | + print(f"ok {source_path}") |
| 106 | + continue |
| 107 | + if destination.exists() and not args.force: |
| 108 | + failures.append(f"{source_path}: local file differs ({observed}); use --force to replace") |
| 109 | + continue |
| 110 | + data = fetch(source_path) |
| 111 | + actual = git_blob_sha(data) |
| 112 | + if actual != expected: |
| 113 | + failures.append(f"{source_path}: downloaded blob {actual}, expected {expected}") |
| 114 | + continue |
| 115 | + destination.parent.mkdir(parents=True, exist_ok=True) |
| 116 | + destination.write_bytes(data) |
| 117 | + print(f"written {source_path}") |
| 118 | + |
| 119 | + if failures: |
| 120 | + print("hmmm: materialization incomplete", file=sys.stderr) |
| 121 | + for failure in failures: |
| 122 | + print(f"- {failure}", file=sys.stderr) |
| 123 | + return 1 |
| 124 | + print(f"verified {len(FILES)} files from {SOURCE_REPO}@{SOURCE_COMMIT}") |
| 125 | + return 0 |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + raise SystemExit(main()) |
0 commit comments