-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_governance_artifact_json.py
More file actions
executable file
·95 lines (73 loc) · 2.55 KB
/
Copy pathexport_governance_artifact_json.py
File metadata and controls
executable file
·95 lines (73 loc) · 2.55 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
#!/usr/bin/env python3
"""Export governance YAML artifact to canonical JSON."""
from __future__ import annotations
import argparse
import datetime
import json
from pathlib import Path
import shlex
import yaml
from governance_artifact_constants import DEFAULT_JSON, DEFAULT_YAML
TOOL_VERSION = "1.2.0"
def normalize(value: object) -> object:
if isinstance(value, datetime.date):
return value.isoformat()
if isinstance(value, dict):
return {k: normalize(v) for k, v in value.items()}
if isinstance(value, list):
return [normalize(v) for v in value]
return value
def fail(message: str) -> None:
raise SystemExit(f"ERROR: {message}")
def remediation_command(yaml_rel: str, json_rel: str) -> str:
cmd = "scripts/export_governance_artifact_json.py --root ."
cmd += f" --yaml {shlex.quote(yaml_rel)} --json {shlex.quote(json_rel)}"
return cmd
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Export governance artifact YAML to JSON"
)
parser.add_argument("--root", default=".")
parser.add_argument(
"--yaml", default=DEFAULT_YAML, help="YAML artifact path relative to --root"
)
parser.add_argument(
"--json", default=DEFAULT_JSON, help="JSON output path relative to --root"
)
parser.add_argument(
"--verify",
action="store_true",
help="Check whether JSON output is up to date without writing",
)
parser.add_argument(
"--version",
action="version",
version=f"export_governance_artifact_json.py {TOOL_VERSION}",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
root = Path(args.root).resolve()
yaml_path = root / args.yaml
json_path = root / args.json
if not yaml_path.exists():
fail(f"YAML artifact not found: {yaml_path}")
data = yaml.safe_load(yaml_path.read_text())
normalized = normalize(data)
rendered = json.dumps(normalized, indent=2, sort_keys=True) + "\n"
if args.verify:
if not json_path.exists():
fail(f"JSON artifact not found: {json_path}")
current = json_path.read_text()
if current != rendered:
fail(
"JSON artifact is stale; run "
f"{remediation_command(args.yaml, args.json)}"
)
print(f"OK: JSON verified {json_path}")
return
json_path.parent.mkdir(parents=True, exist_ok=True)
json_path.write_text(rendered)
print(f"OK: wrote {json_path}")
if __name__ == "__main__":
main()