Skip to content

Commit e14d83f

Browse files
committed
scripts: runners: Generalize the dry run option for subclass use
Add a new parameter to the ZephyrBinaryRunner init() method to specify whether this should be a dry run. The default is still taken from the _DRY_RUN global, but can be overridden by subclasses. Since the new dry_run parameter has a default value this should not break backwards compatibility with out-of-tree runners. Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
1 parent 96e6cf5 commit e14d83f

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

scripts/west_commands/runners/core.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
# info rather than debug level), without actually running them. This
4343
# can break runners that are expecting output or if one command
4444
# depends on another, so it's just for debugging.
45+
# Note that this is also configurable at ZephyrBinaryRunner init() time, with
46+
# the default being taken from this global
4547
_DRY_RUN = False
4648

4749
_logger = logging.getLogger('runners')
@@ -483,12 +485,15 @@ class ZephyrBinaryRunner(abc.ABC):
483485
commands in its constructor. The actual command execution is
484486
handled in the run() method.'''
485487

486-
def __init__(self, cfg: RunnerConfig):
488+
def __init__(self, cfg: RunnerConfig, dry_run: bool=_DRY_RUN):
487489
'''Initialize core runner state.'''
488490

489491
self.cfg = cfg
490492
'''RunnerConfig for this instance.'''
491493

494+
self.dry_run = dry_run
495+
'''Dry run or not for this instance.'''
496+
492497
self.logger = logging.getLogger(f'runners.{self.name()}')
493498
'''logging.Logger for this instance.'''
494499

@@ -860,7 +865,7 @@ def run_client(self, client, **kwargs):
860865

861866
def _log_cmd(self, cmd: list[str]):
862867
escaped = ' '.join(shlex.quote(s) for s in cmd)
863-
if not _DRY_RUN:
868+
if not self.dry_run:
864869
self.logger.debug(escaped)
865870
else:
866871
self.logger.info(escaped)
@@ -873,7 +878,7 @@ def call(self, cmd: list[str], **kwargs) -> int:
873878
using subprocess directly, to keep accurate debug logs.
874879
'''
875880
self._log_cmd(cmd)
876-
if _DRY_RUN:
881+
if self.dry_run:
877882
return 0
878883
return subprocess.call(cmd, **kwargs)
879884

@@ -885,7 +890,7 @@ def check_call(self, cmd: list[str], **kwargs):
885890
using subprocess directly, to keep accurate debug logs.
886891
'''
887892
self._log_cmd(cmd)
888-
if _DRY_RUN:
893+
if self.dry_run:
889894
return
890895
subprocess.check_call(cmd, **kwargs)
891896

@@ -897,7 +902,7 @@ def check_output(self, cmd: list[str], **kwargs) -> bytes:
897902
using subprocess directly, to keep accurate debug logs.
898903
'''
899904
self._log_cmd(cmd)
900-
if _DRY_RUN:
905+
if self.dry_run:
901906
return b''
902907
return subprocess.check_output(cmd, **kwargs)
903908

@@ -918,7 +923,7 @@ def popen_ignore_int(self, cmd: list[str], **kwargs) -> subprocess.Popen:
918923
preexec = os.setsid # type: ignore
919924

920925
self._log_cmd(cmd)
921-
if _DRY_RUN:
926+
if self.dry_run:
922927
return _DebugDummyPopen() # type: ignore
923928

924929
return subprocess.Popen(cmd, creationflags=cflags, preexec_fn=preexec, **kwargs)

scripts/west_commands/runners/nrfutil.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import sys
1010
from pathlib import Path
1111

12-
from runners.core import _DRY_RUN
1312
from runners.nrf_common import NrfBinaryRunner
1413

1514

@@ -70,7 +69,7 @@ def _exec(self, args):
7069
cmd = ['nrfutil', '--json', 'device'] + args
7170
self._log_cmd(cmd)
7271

73-
if _DRY_RUN:
72+
if self.dry_run:
7473
return {}
7574

7675
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as p:

0 commit comments

Comments
 (0)