added
This commit is contained in:
commit
530e0a77f1
10 files changed
+478
No files matched your search
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Validate automation.yaml against its JSON Schema and find template markers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any, Iterator
|
||||
|
||||
import yaml
|
||||
from jsonschema import Draft202012Validator
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DEFAULT_MANIFEST = ROOT / "automation.yaml"
|
||||
DEFAULT_SCHEMA = ROOT / "automation.schema.json"
|
||||
PLACEHOLDER_MARKERS = ("replace-me", "replace me", "example.invalid")
|
||||
|
||||
|
||||
def iter_strings(value: Any, path: str = "$") -> Iterator[tuple[str, str]]:
|
||||
if isinstance(value, dict):
|
||||
for key, child in value.items():
|
||||
yield from iter_strings(child, f"{path}.{key}")
|
||||
elif isinstance(value, list):
|
||||
for index, child in enumerate(value):
|
||||
yield from iter_strings(child, f"{path}[{index}]")
|
||||
elif isinstance(value, str):
|
||||
yield path, value
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("manifest", nargs="?", type=Path, default=DEFAULT_MANIFEST)
|
||||
parser.add_argument("--schema", type=Path, default=DEFAULT_SCHEMA)
|
||||
parser.add_argument("--strict", action="store_true", help="Fail on unresolved template markers.")
|
||||
args = parser.parse_args()
|
||||
|
||||
manifest = yaml.safe_load(args.manifest.read_text(encoding="utf-8"))
|
||||
schema = json.loads(args.schema.read_text(encoding="utf-8"))
|
||||
errors = sorted(Draft202012Validator(schema).iter_errors(manifest), key=lambda e: list(e.absolute_path))
|
||||
|
||||
if errors:
|
||||
print("Manifest schema validation failed:", file=sys.stderr)
|
||||
for error in errors:
|
||||
location = "$" + "".join(f"[{part!r}]" for part in error.absolute_path)
|
||||
print(f"- {location}: {error.message}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
unresolved = [
|
||||
(path, value)
|
||||
for path, value in iter_strings(manifest)
|
||||
if any(marker in value.lower() for marker in PLACEHOLDER_MARKERS)
|
||||
]
|
||||
if unresolved:
|
||||
stream = sys.stderr if args.strict else sys.stdout
|
||||
print("Unresolved template markers:", file=stream)
|
||||
for path, value in unresolved:
|
||||
print(f"- {path}: {value}", file=stream)
|
||||
if args.strict:
|
||||
return 2
|
||||
|
||||
print(f"Valid manifest: {args.manifest}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
|
||||
Reference in new issue
Block a user