20261117 固化13步自然回放、数据哈希与第二版性能基线
This commit is contained in:
78
scripts/experiments/reuse_eval/fetch_bundle.py
Normal file
78
scripts/experiments/reuse_eval/fetch_bundle.py
Normal file
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fetch a reviewed bundle manifest via parallel compressed rsync, then hash all files.
|
||||
|
||||
Fetch the small exported manifest first. No remote mutation, cleanup, RP or server
|
||||
execution is performed here. Source absolute paths are provenance, destinations
|
||||
are always relative to the local bundle root.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
from common import file_sha256
|
||||
from replay_bundle import load_manifest, safe_path, verify
|
||||
|
||||
|
||||
def fetch(root: Path, manifest: dict, host: str, key: Path, jobs: int) -> dict:
|
||||
if not 1 <= jobs <= 16 or host.startswith("-") or any(c.isspace() for c in host):
|
||||
raise ValueError("invalid transfer endpoint/job count")
|
||||
ssh = shlex.join(["ssh", "-F", "/dev/null", "-i", str(key.resolve()), "-o", "IdentitiesOnly=yes",
|
||||
"-o", "BatchMode=yes", "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=yes"])
|
||||
archive_root = Path(next(r["source"] for r in manifest["files"] if r["path"] == "archive/contract.json")).parent
|
||||
pending, other = [], []
|
||||
for row in manifest["files"]:
|
||||
target = safe_path(root, row["path"])
|
||||
if target.exists():
|
||||
if target.stat().st_size == row["bytes"] and file_sha256(target) == row["sha256"]:
|
||||
continue
|
||||
raise ValueError("existing local content differs: " + row["path"])
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
if row["path"].startswith("archive/"):
|
||||
relative = row["path"][len("archive/"):]
|
||||
if Path(row["source"]) != archive_root / relative:
|
||||
raise ValueError("archive source layout mismatch")
|
||||
pending.append((row["bytes"], relative))
|
||||
else:
|
||||
other.append(row)
|
||||
groups = [[] for _ in range(jobs)]
|
||||
sizes = [0] * jobs
|
||||
for size, relative in sorted(pending, reverse=True):
|
||||
index = sizes.index(min(sizes))
|
||||
groups[index].append(relative)
|
||||
sizes[index] += size
|
||||
base = ["rsync", "-rtz", "--compress-level=3", "--timeout=120", "--protect-args", "-e", ssh]
|
||||
def transfer(index: int):
|
||||
with tempfile.TemporaryDirectory(prefix="rpki-bundle-list-") as tmp:
|
||||
listing = Path(tmp) / "files"
|
||||
listing.write_text("\n".join(groups[index]) + "\n")
|
||||
if groups[index]:
|
||||
subprocess.run(base + ["--files-from=" + str(listing), host + ":" + str(archive_root) + "/",
|
||||
str(root / "archive") + "/"], check=True)
|
||||
with ThreadPoolExecutor(max_workers=jobs) as pool:
|
||||
list(pool.map(transfer, range(jobs)))
|
||||
for row in other:
|
||||
subprocess.run(base + [host + ":" + row["source"], str(safe_path(root, row["path"]))], check=True)
|
||||
return verify(root, manifest)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--manifest", type=Path, required=True)
|
||||
parser.add_argument("--expected-sha256", required=True)
|
||||
parser.add_argument("--root", type=Path, required=True)
|
||||
parser.add_argument("--host", required=True)
|
||||
parser.add_argument("--identity", type=Path, required=True)
|
||||
parser.add_argument("--jobs", type=int, default=8)
|
||||
args = parser.parse_args()
|
||||
manifest = load_manifest(args.manifest, args.expected_sha256)
|
||||
print(json.dumps(fetch(args.root, manifest, args.host, args.identity, args.jobs)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user