File size: 2,030 Bytes
c5f5de4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""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())