60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Build site/data.json from data/reports/*.yml.
|
||
|
|
|
||
|
|
Every report is validated against data/schema.json before being included —
|
||
|
|
a malformed report fails the build loudly instead of shipping a broken row
|
||
|
|
to the site (same idea as archy's own scripts/validate-app-manifest.sh:
|
||
|
|
fail the build, don't ship the drift).
|
||
|
|
"""
|
||
|
|
import json
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import yaml
|
||
|
|
from jsonschema import Draft7Validator
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parent.parent
|
||
|
|
REPORTS_DIR = ROOT / "data" / "reports"
|
||
|
|
SCHEMA_PATH = ROOT / "data" / "schema.json"
|
||
|
|
OUT_PATH = ROOT / "site" / "data.json"
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> int:
|
||
|
|
schema = json.loads(SCHEMA_PATH.read_text())
|
||
|
|
validator = Draft7Validator(schema)
|
||
|
|
|
||
|
|
reports = []
|
||
|
|
errors = []
|
||
|
|
|
||
|
|
for path in sorted(REPORTS_DIR.glob("*.yml")):
|
||
|
|
try:
|
||
|
|
data = yaml.safe_load(path.read_text())
|
||
|
|
except yaml.YAMLError as e:
|
||
|
|
errors.append(f"{path.name}: invalid YAML — {e}")
|
||
|
|
continue
|
||
|
|
|
||
|
|
report_errors = sorted(validator.iter_errors(data), key=lambda e: e.path)
|
||
|
|
if report_errors:
|
||
|
|
for err in report_errors:
|
||
|
|
loc = ".".join(str(p) for p in err.path) or "(root)"
|
||
|
|
errors.append(f"{path.name}: {loc}: {err.message}")
|
||
|
|
continue
|
||
|
|
|
||
|
|
data["_source_file"] = path.name
|
||
|
|
reports.append(data)
|
||
|
|
|
||
|
|
if errors:
|
||
|
|
print(f"Build failed — {len(errors)} error(s):", file=sys.stderr)
|
||
|
|
for e in errors:
|
||
|
|
print(f" {e}", file=sys.stderr)
|
||
|
|
return 1
|
||
|
|
|
||
|
|
reports.sort(key=lambda r: (r["device_model"], r["tested_date"]))
|
||
|
|
OUT_PATH.write_text(json.dumps(reports, indent=2) + "\n")
|
||
|
|
print(f"Wrote {len(reports)} report(s) to {OUT_PATH.relative_to(ROOT)}")
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|