Spaces:
Sleeping
Sleeping
File size: 1,654 Bytes
4a5269c | 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 | # backend/scripts/apply_trainer_schemas.py
from __future__ import annotations
import json
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
TEMPLATES_DIR = REPO_ROOT / "backend" / "templates"
SCHEMAS_DIR = REPO_ROOT / "backend" / "trainer_schemas"
def main() -> None:
if not SCHEMAS_DIR.exists():
raise RuntimeError(f"Missing schemas dir: {SCHEMAS_DIR}")
schema_files = sorted(SCHEMAS_DIR.glob("*.schema.json"))
if not schema_files:
raise RuntimeError(f"No schema files found in: {SCHEMAS_DIR}")
applied = 0
for sf in schema_files:
template_id = sf.name.replace(".schema.json", "")
template_path = TEMPLATES_DIR / f"{template_id}.json"
if not template_path.exists():
print(f"⚠️ skip (no template file): {template_path}")
continue
new_schema = json.loads(sf.read_text(encoding="utf-8"))
if not isinstance(new_schema, dict):
raise RuntimeError(f"Invalid schema json (not object): {sf}")
if not isinstance(new_schema.get("fields"), list):
raise RuntimeError(f"Invalid schema json (missing fields[]): {sf}")
t = json.loads(template_path.read_text(encoding="utf-8"))
t["schema"] = new_schema
# Optional: bump template version when schema changes
# t["version"] = int(t.get("version") or 0) + 1
template_path.write_text(json.dumps(t, indent=2) + "\n", encoding="utf-8")
print(f"✅ updated {template_path} fields={len(new_schema['fields'])}")
applied += 1
print(f"done. applied={applied}")
if __name__ == "__main__":
main() |