-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_roundtrip_check.py
More file actions
193 lines (151 loc) · 5.86 KB
/
json_roundtrip_check.py
File metadata and controls
193 lines (151 loc) · 5.86 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Round-trip exported HS3 JSON workspaces and compare against the originals."""
from __future__ import annotations
import argparse
import json
import re
import tempfile
from pathlib import Path
from typing import Any, Iterator, List, Mapping, Sequence, Tuple
try:
import ROOT # type: ignore
except ImportError as exc:
raise SystemExit(
"PyROOT is required to run this script. Ensure that ROOT is available in the environment."
) from exc
def configure_roofit_logging() -> None:
msgservice = ROOT.RooMsgService.instance()
msgservice.setGlobalKillBelow(ROOT.RooFit.WARNING)
msgservice.getStream(ROOT.RooFit.INFO).removeTopic(ROOT.RooFit.ObjectHandling)
msgservice.getStream(ROOT.RooFit.DEBUG).removeTopic(ROOT.RooFit.ObjectHandling)
def locate_json_files(paths: Sequence[Path]) -> List[Path]:
json_files: List[Path] = []
for path in paths:
if path.is_dir():
json_files.extend(sorted(p for p in path.glob("*.json") if p.is_file()))
elif path.suffix == ".json" and path.is_file():
json_files.append(path)
return json_files
def load_json(path: Path) -> object:
with path.open("r", encoding="utf-8") as handle:
return json.load(handle)
def _sort_key_for_dict(item: Mapping[str, Any]) -> Tuple[str, str]:
name = item.get("name")
if isinstance(name, str):
return ("name", name)
identifier = item.get("id")
if isinstance(identifier, str):
return ("id", identifier)
type_ = item.get("type")
if isinstance(type_, str):
return ("type", type_)
serialized = json.dumps(item, sort_keys=True, separators=(",", ":"))
return ("json", serialized)
def canonicalize(obj: object) -> object:
if isinstance(obj, dict):
return {key: canonicalize(obj[key]) for key in sorted(obj)}
if isinstance(obj, list):
if not obj:
return []
if all(isinstance(item, dict) for item in obj):
canonicalized_items = [canonicalize(item) for item in obj]
canonicalized_items.sort(key=_sort_key_for_dict)
return canonicalized_items
return [canonicalize(item) for item in obj]
return obj
IGNORED_PATHS = (
re.compile(r"\$\.data\[\d+\]\.axes\[\d+\]\.value"),
re.compile(r"\$\.metadata(?:\..*)?"),
)
def should_ignore(path: str) -> bool:
return any(pattern.fullmatch(path) for pattern in IGNORED_PATHS)
def find_difference(expected: object, actual: object, path: str = "$") -> str | None:
if should_ignore(path):
return None
if type(expected) != type(actual):
return f"{path}: type mismatch ({type(expected).__name__} vs {type(actual).__name__})"
if isinstance(expected, dict):
expected_keys = set(expected)
actual_keys = set(actual)
missing = expected_keys - actual_keys
if missing:
key = next(iter(sorted(missing)))
return f"{path}: missing key {key!r}"
unexpected = actual_keys - expected_keys
if unexpected:
key = next(iter(sorted(unexpected)))
return f"{path}: unexpected key {key!r}"
for key in sorted(expected_keys):
nested_path = f"{path}.{key}"
diff = find_difference(expected[key], actual[key], nested_path)
if diff:
return diff
return None
if isinstance(expected, list):
if len(expected) != len(actual):
return f"{path}: list length differs ({len(expected)} vs {len(actual)})"
for index, (exp_item, act_item) in enumerate(zip(expected, actual)):
nested_path = f"{path}[{index}]"
diff = find_difference(exp_item, act_item, nested_path)
if diff:
return diff
return None
if expected != actual:
return f"{path}: value differs ({expected!r} vs {actual!r})"
return None
def round_trip(json_path: Path) -> Tuple[bool, str | None]:
workspace = ROOT.RooWorkspace(f"ws_{json_path.stem}")
tool = ROOT.RooJSONFactoryWSTool(workspace)
tool.importJSON(str(json_path))
temp_path = Path("tmpdir") / json_path.name
tool.exportJSON(str(temp_path))
"""
original = canonicalize(load_json(json_path))
regenerated = canonicalize(load_json(temp_path))
"""
with tempfile.TemporaryDirectory() as tmpdir:
temp_path = Path(tmpdir) / json_path.name
tool.exportJSON(str(temp_path))
original = canonicalize(load_json(json_path))
regenerated = canonicalize(load_json(temp_path))
diff = find_difference(original, regenerated)
return diff is None, diff
def iter_input_paths(args: argparse.Namespace) -> Iterator[Path]:
if args.paths:
yield from args.paths
else:
yield Path("exportedJSON")
def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Re-import HS3 JSON workspaces and verify a round-trip export."
)
parser.add_argument(
"paths",
nargs="*",
type=Path,
help="JSON files or directories to verify (default: exportedJSON).",
)
return parser.parse_args(argv)
def main(argv: Sequence[str] | None = None) -> int:
args = parse_args(argv)
configure_roofit_logging()
json_paths = locate_json_files(list(iter_input_paths(args)))
if not json_paths:
print("No JSON files found to process.")
return 0
ok = True
for json_path in json_paths:
try:
matches, diff = round_trip(json_path)
except Exception as exc:
ok = False
print(f"❌ {json_path}: {exc}")
continue
if matches:
print(f"✅ {json_path}")
else:
ok = False
detail = f" ({diff})" if diff else ""
print(f"❌ {json_path}{detail}")
return 0 if ok else 1
if __name__ == "__main__":
raise SystemExit(main())