Data, not hand-written code: tools/extract_registry.py reads widgets/common/registry.h and cross-checks it three ways - the on-disk widgets/components/ dirs, widgets/tools/sections.py, and the default_size() rule from gen_registry.py - failing on any drift. Re-derived counts (lightgui eb0b5fc): 547 widgets, 11 sections; families Gtk 102, Ind 250, Inst 66, Stock 37, T 90, X 2. The survey Stock 38 / Inst 67 counted the shared non-widget headers StockData.h / InstData.h; XGraph/XSpreadsheet form a sixth family X (2) the survey family table omitted. Includes the extraction harness: tools/common.py, extract_all.py, check.py (make check re-derives tables from the C++ sources and fails on drift), tools/README.md, and the top-level Makefile.
129 lines
4.8 KiB
Python
129 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Drift check: re-derive every data table from the C++ sources and
|
|
fail if any data/*.json disagrees.
|
|
|
|
Each extractor already cross-checks the sources as it runs; this
|
|
script additionally proves the committed JSON files are exactly what
|
|
the current sources produce — so a hand-edit to a JSON file, or a
|
|
change in the C repo that nobody regenerated for, is caught here.
|
|
|
|
Usage:
|
|
python3 tools/check.py — check all five tables
|
|
python3 tools/check.py registry — check one table
|
|
make check — check all five tables
|
|
|
|
Exit code 0 = all present tables match the sources.
|
|
Exit code 1 = a table is missing or disagrees (details printed).
|
|
"""
|
|
|
|
import importlib
|
|
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from common import DATA_DIR, data_without_provenance, load_json
|
|
|
|
# name -> (extractor module, data file, expected provenance generator)
|
|
CHECKS = {
|
|
"registry": ("extract_registry", "registry.json",
|
|
"python3 tools/extract_registry.py"),
|
|
"palette": ("extract_palette", "palette.json",
|
|
"python3 tools/extract_palette.py"),
|
|
"props": ("extract_props", "props.json",
|
|
"python3 tools/extract_props.py"),
|
|
"events": ("extract_events", "events.json",
|
|
"python3 tools/extract_events.py"),
|
|
"indicators": ("extract_indicators", "indicators.json",
|
|
"python3 tools/extract_indicators.py"),
|
|
}
|
|
|
|
|
|
def diff_paths(a, b, path=""):
|
|
"""Yield human-readable paths where two JSON structures differ."""
|
|
if type(a) is not type(b):
|
|
yield "%s: type %s vs %s" % (path or "(root)",
|
|
type(a).__name__, type(b).__name__)
|
|
return
|
|
if isinstance(a, dict):
|
|
for k in sorted(set(a) | set(b)):
|
|
if k not in a:
|
|
yield "%s.%s: missing in fresh derivation" % (path, k)
|
|
elif k not in b:
|
|
yield "%s.%s: missing in committed file" % (path, k)
|
|
else:
|
|
yield from diff_paths(a[k], b[k], "%s.%s" % (path, k))
|
|
elif isinstance(a, list):
|
|
if len(a) != len(b):
|
|
yield "%s: length %d (fresh) vs %d (committed)" % (
|
|
path or "(root)", len(a), len(b))
|
|
return
|
|
for i, (x, y) in enumerate(zip(a, b)):
|
|
yield from diff_paths(x, y, "%s[%d]" % (path, i))
|
|
elif a != b:
|
|
yield "%s: %r (fresh) vs %r (committed)" % (path, a, b)
|
|
|
|
|
|
def check_one(name):
|
|
module_name, data_file, generator = CHECKS[name]
|
|
path = os.path.join(DATA_DIR, data_file)
|
|
if not os.path.isfile(path):
|
|
return ["%s: MISSING data file %s" % (name, path)]
|
|
try:
|
|
module = importlib.import_module(module_name)
|
|
except ImportError as e:
|
|
return ["%s: cannot import %s: %s" % (name, module_name, e)]
|
|
try:
|
|
fresh = module.build()
|
|
except Exception as e:
|
|
return ["%s: re-derivation failed: %s: %s"
|
|
% (name, type(e).__name__, e)]
|
|
committed = load_json(path)
|
|
problems = []
|
|
prov = committed.get("_provenance", {})
|
|
if prov.get("generated_by") != generator:
|
|
problems.append("%s: _provenance.generated_by is %r, expected %r"
|
|
% (name, prov.get("generated_by"), generator))
|
|
if sorted(prov.get("source_files", [])) != sorted(
|
|
fresh["_provenance"]["source_files"]):
|
|
problems.append("%s: _provenance.source_files changed: %r vs %r"
|
|
% (name, prov.get("source_files"),
|
|
fresh["_provenance"]["source_files"]))
|
|
diffs = list(diff_paths(data_without_provenance(fresh),
|
|
data_without_provenance(committed)))
|
|
if diffs:
|
|
problems.append("%s: %d difference(s) vs committed %s:"
|
|
% (name, len(diffs), data_file))
|
|
problems.extend(" " + d for d in diffs[:20])
|
|
if len(diffs) > 20:
|
|
problems.append(" ... and %d more" % (len(diffs) - 20))
|
|
return problems
|
|
|
|
|
|
def main():
|
|
names = sys.argv[1:] or list(CHECKS)
|
|
unknown = [n for n in names if n not in CHECKS]
|
|
if unknown:
|
|
print("check: unknown table(s): %s (valid: %s)"
|
|
% (", ".join(unknown), ", ".join(CHECKS)), file=sys.stderr)
|
|
return 2
|
|
all_problems = []
|
|
for name in names:
|
|
problems = check_one(name)
|
|
if problems:
|
|
all_problems.extend(problems)
|
|
print("check: %s: FAIL" % name)
|
|
else:
|
|
print("check: %s: OK" % name)
|
|
if all_problems:
|
|
print("\n".join(all_problems), file=sys.stderr)
|
|
print("check: FAILED — regenerate with `make regen` "
|
|
"or fix the drift above", file=sys.stderr)
|
|
return 1
|
|
print("check: all %d table(s) match the C++ sources"
|
|
% len(names))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|