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 Feb 17, 2025
1 parent 5341a41 commit 6dc332e
Showing 1 changed file with 10 additions and 6 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, TemplateNotFound
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

0 comments on commit 6dc332e

Please sign in to comment.