micro / faisi /src /sandbox /prepare_windows.py
ppJustin's picture
Add files using upload-large-folder tool
c5f5de4 verified
Raw History Blame Contribute Delete
2.03 kB
"""Prepare the trusted observation-window index using W1-02 case adapters."""
import argparse
import json
import os
from pathlib import Path
import sys
import tempfile
from .snapshot import DATASETS
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--w102-source", type=Path, required=True)
parser.add_argument("--raw-root", type=Path, required=True)
parser.add_argument("--snapshot-root", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
# This trusted preparation step runs outside the query service.
sys.path.insert(0, str(args.w102_source.resolve()))
from adapters import normalize_pack, pack_batches, v1_batches
windows = {}
for dataset in DATASETS:
raw = args.raw_root / dataset
if not (raw / ".verified.json").is_file():
raise ValueError(f"missing integrity receipt for {dataset}")
if dataset == "openrca_v1":
groups = v1_batches(raw)
else:
groups = (normalize_pack(dataset, base, case_dir, meta)
for case_dir, meta, base in pack_batches(raw, dataset))
profile = {case.case_id: [case.start, case.boundary, case.end]
for group in groups for case in group.cases}
available = {item.name for item in (args.snapshot_root / dataset).iterdir() if item.is_dir()}
if profile.keys() != available:
raise ValueError(f"observation inventory differs for {dataset}")
windows[dataset] = profile
print(f"{dataset}: {len(profile)} observation windows", flush=True)
args.output.parent.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile(dir=args.output.parent, mode="w", delete=False) as stream:
json.dump({"version": 1, "windows": windows}, stream, separators=(",", ":"), sort_keys=True)
os.replace(stream.name, args.output)
return 0
if __name__ == "__main__":
raise SystemExit(main())