|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import re |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +# Ensure the repo root is importable even when running a script from /tools. |
| 11 | +_REPO_ROOT = Path(__file__).resolve().parents[1] |
| 12 | +if str(_REPO_ROOT) not in sys.path: |
| 13 | + sys.path.insert(0, str(_REPO_ROOT)) |
| 14 | + |
| 15 | +from local_nexus_controller.models import ImportBundle |
| 16 | + |
| 17 | + |
| 18 | +_HAS_PLACEHOLDER_RE = re.compile(r"(\{[A-Z_]+\}|\$\{[A-Z_]+\}|%[A-Z_]+%)") |
| 19 | + |
| 20 | + |
| 21 | +def _looks_like_windows_abs_path(p: str) -> bool: |
| 22 | + # Accept both C:\foo and C:/foo |
| 23 | + return bool(re.match(r"^[a-zA-Z]:[\\/]", p)) |
| 24 | + |
| 25 | + |
| 26 | +def _to_posixish(p: str) -> str: |
| 27 | + return p.replace("\\", "/") |
| 28 | + |
| 29 | + |
| 30 | +def normalize_path_value(value: str) -> str: |
| 31 | + """ |
| 32 | + Normalize Windows path strings to use forward slashes. |
| 33 | + If the value looks like an absolute path and does not contain placeholders, |
| 34 | + resolve it to an absolute normalized path. |
| 35 | + """ |
| 36 | + |
| 37 | + v = str(value) |
| 38 | + v = _to_posixish(v) |
| 39 | + |
| 40 | + if _HAS_PLACEHOLDER_RE.search(v): |
| 41 | + return v |
| 42 | + |
| 43 | + if _looks_like_windows_abs_path(v): |
| 44 | + try: |
| 45 | + resolved = Path(v).resolve() |
| 46 | + return _to_posixish(str(resolved)) |
| 47 | + except Exception: |
| 48 | + # Best-effort: keep the slash-normalized value |
| 49 | + return v |
| 50 | + |
| 51 | + return v |
| 52 | + |
| 53 | + |
| 54 | +def normalize_bundle_dict(obj: dict[str, Any]) -> dict[str, Any]: |
| 55 | + out = json.loads(json.dumps(obj)) # deep copy (keeps only JSON types) |
| 56 | + svc = out.get("service") or {} |
| 57 | + |
| 58 | + if isinstance(svc, dict): |
| 59 | + wd = svc.get("working_directory") |
| 60 | + if isinstance(wd, str) and wd.strip(): |
| 61 | + svc["working_directory"] = normalize_path_value(wd) |
| 62 | + |
| 63 | + cps = svc.get("config_paths") |
| 64 | + if isinstance(cps, list): |
| 65 | + svc["config_paths"] = [normalize_path_value(x) if isinstance(x, str) else x for x in cps] |
| 66 | + |
| 67 | + out["service"] = svc |
| 68 | + return out |
| 69 | + |
| 70 | + |
| 71 | +def main() -> int: |
| 72 | + ap = argparse.ArgumentParser(description="Validate (and optionally normalize) a Local Nexus Import Bundle JSON.") |
| 73 | + ap.add_argument("path", help="Path to a bundle JSON file (single object or list).") |
| 74 | + ap.add_argument("--normalize", action="store_true", help="Normalize path separators and resolve absolute paths.") |
| 75 | + ap.add_argument( |
| 76 | + "--output", |
| 77 | + help="Write normalized JSON to this path (requires --normalize). If omitted, prints normalized JSON to stdout.", |
| 78 | + ) |
| 79 | + args = ap.parse_args() |
| 80 | + |
| 81 | + path = Path(args.path).expanduser().resolve() |
| 82 | + if not path.exists(): |
| 83 | + raise SystemExit(f"Bundle not found: {path}") |
| 84 | + |
| 85 | + payload = json.loads(path.read_text(encoding="utf-8")) |
| 86 | + items: list[dict[str, Any]] |
| 87 | + if isinstance(payload, list): |
| 88 | + items = payload |
| 89 | + else: |
| 90 | + items = [payload] |
| 91 | + |
| 92 | + normalized_items: list[dict[str, Any]] = [] |
| 93 | + for idx, item in enumerate(items, start=1): |
| 94 | + if not isinstance(item, dict): |
| 95 | + raise SystemExit(f"Item {idx} is not a JSON object.") |
| 96 | + raw = normalize_bundle_dict(item) if args.normalize else item |
| 97 | + |
| 98 | + # Validate against the controller schema (raises on error) |
| 99 | + ImportBundle.model_validate(raw) # type: ignore[attr-defined] |
| 100 | + normalized_items.append(raw) |
| 101 | + |
| 102 | + print(f"OK: validated {len(normalized_items)} bundle(s) from {path}") |
| 103 | + |
| 104 | + if args.normalize: |
| 105 | + output_payload: Any = normalized_items if isinstance(payload, list) else normalized_items[0] |
| 106 | + text = json.dumps(output_payload, indent=2, ensure_ascii=False) |
| 107 | + if args.output: |
| 108 | + out_path = Path(args.output).expanduser().resolve() |
| 109 | + out_path.write_text(text + "\n", encoding="utf-8") |
| 110 | + print(f"Wrote normalized bundle to: {out_path}") |
| 111 | + else: |
| 112 | + print(text) |
| 113 | + |
| 114 | + return 0 |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + raise SystemExit(main()) |
| 119 | + |
0 commit comments