|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Manifest-driven multi-version source build. |
| 3 | +
|
| 4 | +Reads sources/manifest.yaml and, for every (type, version) entry, runs the same |
| 5 | +validate -> enrich transform as run_all.py to produce |
| 6 | +``sources/generated/<type>/<version>.json``, then writes |
| 7 | +``sources/generated/manifest.json`` -- the runtime registry the apps consume |
| 8 | +(mirrors schemas/source-manifest.v1.schema.json / the SourceManifest TS type). |
| 9 | +
|
| 10 | +The legacy flat output (PreScanDPIA.json/DPIA.json/IAMA.json) is produced separately |
| 11 | +by run_all.py and is unchanged; wiring the build sites to this orchestrator is a |
| 12 | +deliberate follow-up step (it touches CI and the container build). |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import argparse |
| 18 | +import json |
| 19 | +import logging |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +from definition_enricher import DefinitionEnricher |
| 23 | +from manifest import load_manifest |
| 24 | +from schema_validator import SchemaValidator |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +def build_sources( |
| 30 | + manifest_path: Path, |
| 31 | + sources_dir: Path, |
| 32 | + schema_path: Path, |
| 33 | + generated_dir: Path, |
| 34 | + script_dir: Path | None = None, |
| 35 | +) -> Path: |
| 36 | + """Build every (type, version) declared in the manifest into a nested enriched JSON, |
| 37 | + then write the runtime manifest. Returns the runtime manifest path. |
| 38 | +
|
| 39 | + Raises ValueError when a source fails schema validation -- the build must not emit a |
| 40 | + registry pointing at half-valid artefacts. |
| 41 | + """ |
| 42 | + script_dir = script_dir or Path(__file__).parent |
| 43 | + manifest = load_manifest(manifest_path) |
| 44 | + validator = SchemaValidator(script_dir) |
| 45 | + enricher = DefinitionEnricher(script_dir) |
| 46 | + |
| 47 | + for type_name, type_def in manifest["types"].items(): |
| 48 | + begrippen_yaml = sources_dir / type_def["begrippenkader"] |
| 49 | + once_per_page = type_def.get("enrichOncePerPage", False) |
| 50 | + for version in type_def["versions"]: |
| 51 | + source = sources_dir / version["file"] |
| 52 | + is_valid, errors, _validated = validator.validate_yaml(source, schema_path) |
| 53 | + if not is_valid: |
| 54 | + raise ValueError( |
| 55 | + f"{type_name} {version['version']}: schema validation failed: {errors}" |
| 56 | + ) |
| 57 | + output = generated_dir / type_name / f"{version['version']}.json" |
| 58 | + enricher.enrich_and_export(source, begrippen_yaml, output, once_per_page=once_per_page) |
| 59 | + logger.info("Built %s %s -> %s", type_name, version["version"], output) |
| 60 | + |
| 61 | + runtime_path = generated_dir / "manifest.json" |
| 62 | + runtime_path.parent.mkdir(parents=True, exist_ok=True) |
| 63 | + runtime_path.write_text( |
| 64 | + json.dumps(manifest, indent=2, ensure_ascii=False) + "\n", encoding="utf-8" |
| 65 | + ) |
| 66 | + logger.info("Wrote runtime manifest %s", runtime_path) |
| 67 | + return runtime_path |
| 68 | + |
| 69 | + |
| 70 | +def main() -> None: |
| 71 | + parser = argparse.ArgumentParser(description="Manifest-driven multi-version source build") |
| 72 | + parser.add_argument( |
| 73 | + "--manifest", type=Path, required=True, help="Path to sources/manifest.yaml" |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + "--sources-dir", type=Path, required=True, help="Directory holding the source YAMLs" |
| 77 | + ) |
| 78 | + parser.add_argument( |
| 79 | + "--schema", |
| 80 | + type=Path, |
| 81 | + required=True, |
| 82 | + help="assessment-definition schema to validate every source against", |
| 83 | + ) |
| 84 | + parser.add_argument( |
| 85 | + "--generated-dir", type=Path, required=True, help="Output directory for the generated JSONs" |
| 86 | + ) |
| 87 | + args = parser.parse_args() |
| 88 | + build_sources(args.manifest, args.sources_dir, args.schema, args.generated_dir) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") |
| 93 | + main() |
0 commit comments