Skip to content

Commit 0cf8411

Browse files
committed
scripts: runners: nrfutil: Add a new --dry-run parameter
In order to allow for users to invoke "west flash" without actual hardware connected but still running the logic and pregeneration of commands, specifically the json file. Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
1 parent 96e6cf5 commit 0cf8411

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

scripts/west_commands/runners/nrf_common.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class NrfBinaryRunner(ZephyrBinaryRunner):
5353

5454
def __init__(self, cfg, family, softreset, pinreset, dev_id, erase=False,
5555
erase_mode=None, ext_erase_mode=None, reset=True,
56-
tool_opt=None, force=False, recover=False):
56+
tool_opt=None, force=False, recover=False, dry_run=False):
5757
super().__init__(cfg)
5858
self.hex_ = cfg.hex_file
5959
# The old --nrf-family options takes upper-case family names
@@ -67,6 +67,7 @@ def __init__(self, cfg, family, softreset, pinreset, dev_id, erase=False,
6767
self.reset = bool(reset)
6868
self.force = force
6969
self.recover = bool(recover)
70+
self.dry_run = bool(dry_run)
7071

7172
self.tool_opt = []
7273
if tool_opt is not None:
@@ -118,7 +119,6 @@ def do_add_parser(cls, parser):
118119
choices=['none', 'ranges', 'all'],
119120
help='Select the type of erase operation for the '
120121
'external non-volatile memory')
121-
122122
parser.set_defaults(reset=True)
123123

124124
@classmethod
@@ -139,7 +139,10 @@ def ensure_snr(self):
139139
self.dev_id = [d.lstrip("0") for d in dev_id]
140140
return
141141
if not dev_id or "*" in dev_id:
142-
dev_id = self.get_board_snr(dev_id or "*")
142+
if not self.dry_run:
143+
dev_id = self.get_board_snr(dev_id or "*")
144+
else:
145+
dev_id = "DEVICEID" # for a dry run
143146
self.dev_id = dev_id.lstrip("0")
144147

145148
@abc.abstractmethod

scripts/west_commands/runners/nrfutil.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'''Runner for flashing with nrfutil.'''
66

77
import json
8+
import shlex
89
import subprocess
910
import sys
1011
from pathlib import Path
@@ -18,12 +19,12 @@ class NrfUtilBinaryRunner(NrfBinaryRunner):
1819

1920
def __init__(self, cfg, family, softreset, pinreset, dev_id, erase=False,
2021
erase_mode=None, ext_erase_mode=None, reset=True, tool_opt=None,
21-
force=False, recover=False,
22-
ext_mem_config_file=None):
22+
force=False, recover=False, ext_mem_config_file=None,
23+
dry_run=False):
2324

2425
super().__init__(cfg, family, softreset, pinreset, dev_id, erase,
2526
erase_mode, ext_erase_mode, reset, tool_opt, force,
26-
recover)
27+
recover, dry_run)
2728

2829
self.ext_mem_config_file = ext_mem_config_file
2930

@@ -55,23 +56,32 @@ def do_create(cls, cfg, args):
5556
ext_erase_mode=args.ext_erase_mode,
5657
reset=args.reset, tool_opt=args.tool_opt,
5758
force=args.force, recover=args.recover,
58-
ext_mem_config_file=args.ext_mem_config_file)
59+
ext_mem_config_file=args.ext_mem_config_file,
60+
dry_run=args.dry_run)
5961

6062
@classmethod
6163
def do_add_parser(cls, parser):
6264
super().do_add_parser(parser)
6365
parser.add_argument('--ext-mem-config-file', required=False,
6466
dest='ext_mem_config_file',
6567
help='path to an JSON file with external memory configuration')
68+
parser.add_argument('--dry-run', required=False,
69+
action='store_true',
70+
help='''Generate all the commands without actually
71+
executing them''')
6672

67-
def _exec(self, args):
73+
def _exec(self, args, force=False):
6874
jout_all = []
6975

7076
cmd = ['nrfutil', '--json', 'device'] + args
71-
self._log_cmd(cmd)
7277

73-
if _DRY_RUN:
74-
return {}
78+
escaped = ' '.join(shlex.quote(s) for s in cmd)
79+
if _DRY_RUN or (self.dry_run):
80+
self.logger.info(escaped)
81+
if not force:
82+
return {}
83+
else:
84+
self.logger.debug(escaped)
7585

7686
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as p:
7787
for line in iter(p.stdout.readline, b''):
@@ -148,7 +158,7 @@ def _append_batch(self, op, json_file):
148158
cmd += ['--core', op['core']] if op.get('core') else []
149159
cmd += ['--x-family', f'{self.family}']
150160
cmd += ['--x-append-batch', f'{json_file}']
151-
self._exec(cmd)
161+
self._exec(cmd, force=True)
152162

153163
def _exec_batch(self):
154164
# Use x-append-batch to get the JSON from nrfutil itself

0 commit comments

Comments
 (0)