Skip to content

Commit

Permalink
scripts: west_commands: install JSON hook to commands using jsonschema
Browse files Browse the repository at this point in the history
Commands using a jsonschema based protocol will now report errors
formatted in JSON.

Signed-off-by: Gerard Marull-Paretas <[email protected]>
  • Loading branch information
gmarull committed Jan 2, 2025
1 parent 468f3b3 commit aa67175
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
16 changes: 10 additions & 6 deletions scripts/west_commands/create_board/ncs_create_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from pathlib import Path
import json
import shutil
import sys

from jinja2 import Environment, FileSystemLoader
from west.commands import WestCommand
from west import log
from yaml import load
import jsonschema

Expand All @@ -16,6 +16,12 @@
except ImportError:
from yaml import Loader

sys.path.append(str(Path(__file__).parents[1]))
import utils


utils.install_json_excepthook()


SCRIPT_DIR = Path(__file__).absolute().parent
TEMPLATE_DIR = SCRIPT_DIR / "templates"
Expand Down Expand Up @@ -67,7 +73,7 @@ def do_run(self, args, unknown_args):
try:
jsonschema.validate(input, schema)
except jsonschema.ValidationError as e:
raise Exception("Board configuration is not valid") from e
raise ValueError("Board configuration is not valid") from e

soc_parts = input["soc"].split("-")
req_soc = soc_parts[0].lower()
Expand All @@ -86,8 +92,7 @@ def do_run(self, args, unknown_args):
break

if not series:
log.err(f"Invalid/unsupported SoC: {req_soc}")
return
raise ValueError(f"Invalid/unsupported SoC: {req_soc}")

targets = []
for variant in soc["variants"]:
Expand Down Expand Up @@ -130,8 +135,7 @@ def do_run(self, args, unknown_args):
break

if not targets:
log.err(f"Invalid/unsupported variant: {req_variant}")
return
raise ValueError(f"Invalid/unsupported variant: {req_variant}")

# prepare Jinja environment
env = Environment(
Expand Down
9 changes: 8 additions & 1 deletion scripts/west_commands/ncs-bicr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@

import json
import os
import sys
from pathlib import Path

import jsonschema
from west.commands import WestCommand

sys.path.append(str(Path(__file__).parent))
import utils


utils.install_json_excepthook()


SCRIPT_DIR = Path(__file__).absolute().parent
SCHEMA = SCRIPT_DIR.parents[2] / "zephyr" / "soc" / "nordic" / "nrf54h" / "bicr" / "bicr-schema.json"
Expand Down Expand Up @@ -65,7 +72,7 @@ def do_run(self, args, unknown_args):
try:
jsonschema.validate(bicr, schema)
except jsonschema.ValidationError as e:
raise Exception("Invalid BICR") from e
raise ValueError("Invalid BICR") from e

if args.json_schema:
schema = {
Expand Down
6 changes: 6 additions & 0 deletions scripts/west_commands/ncs-board-actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

from west.commands import WestCommand

sys.path.append(str(Path(__file__).parent))
import utils


utils.install_json_excepthook()


SCRIPT_DIR = Path(__file__).absolute().parent
BICRGEN = (
Expand Down

0 comments on commit aa67175

Please sign in to comment.