Skip to content

Commit aa67175

Browse files
committed
scripts: west_commands: install JSON hook to commands using jsonschema
Commands using a jsonschema based protocol will now report errors formatted in JSON. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 468f3b3 commit aa67175

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

scripts/west_commands/create_board/ncs_create_board.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from pathlib import Path
55
import json
66
import shutil
7+
import sys
78

89
from jinja2 import Environment, FileSystemLoader
910
from west.commands import WestCommand
10-
from west import log
1111
from yaml import load
1212
import jsonschema
1313

@@ -16,6 +16,12 @@
1616
except ImportError:
1717
from yaml import Loader
1818

19+
sys.path.append(str(Path(__file__).parents[1]))
20+
import utils
21+
22+
23+
utils.install_json_excepthook()
24+
1925

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

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

8894
if not series:
89-
log.err(f"Invalid/unsupported SoC: {req_soc}")
90-
return
95+
raise ValueError(f"Invalid/unsupported SoC: {req_soc}")
9196

9297
targets = []
9398
for variant in soc["variants"]:
@@ -130,8 +135,7 @@ def do_run(self, args, unknown_args):
130135
break
131136

132137
if not targets:
133-
log.err(f"Invalid/unsupported variant: {req_variant}")
134-
return
138+
raise ValueError(f"Invalid/unsupported variant: {req_variant}")
135139

136140
# prepare Jinja environment
137141
env = Environment(

scripts/west_commands/ncs-bicr.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33

44
import json
55
import os
6+
import sys
67
from pathlib import Path
78

89
import jsonschema
910
from west.commands import WestCommand
1011

12+
sys.path.append(str(Path(__file__).parent))
13+
import utils
14+
15+
16+
utils.install_json_excepthook()
17+
1118

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

7077
if args.json_schema:
7178
schema = {

scripts/west_commands/ncs-board-actions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
from west.commands import WestCommand
1111

12+
sys.path.append(str(Path(__file__).parent))
13+
import utils
14+
15+
16+
utils.install_json_excepthook()
17+
1218

1319
SCRIPT_DIR = Path(__file__).absolute().parent
1420
BICRGEN = (

0 commit comments

Comments
 (0)