Skip to content

Commit 7af2c99

Browse files
committed
scripts: runners: Enable reusing the core dry run logic
In order to avoid duplication of logic in runners, allow subclasses of ZephyrBinaryRunner to set self.dry_run so that they can then reuse the logic in core.py to log instead of execute. Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
1 parent c5486dc commit 7af2c99

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

scripts/west_commands/runners/core.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,9 @@ def __init__(self, cfg: RunnerConfig):
488488
self.logger = logging.getLogger(f'runners.{self.name()}')
489489
'''logging.Logger for this instance.'''
490490

491+
self.dry_run = _DRY_RUN
492+
'''log commands instead of executing them. Can be set by subclasses'''
493+
491494
@staticmethod
492495
def get_runners() -> list[type['ZephyrBinaryRunner']]:
493496
'''Get a list of all currently defined runner classes.'''
@@ -860,7 +863,7 @@ def run_client(self, client, **kwargs):
860863

861864
def _log_cmd(self, cmd: list[str]):
862865
escaped = ' '.join(shlex.quote(s) for s in cmd)
863-
if not _DRY_RUN:
866+
if not self.dry_run:
864867
self.logger.debug(escaped)
865868
else:
866869
self.logger.info(escaped)
@@ -873,7 +876,7 @@ def call(self, cmd: list[str], **kwargs) -> int:
873876
using subprocess directly, to keep accurate debug logs.
874877
'''
875878
self._log_cmd(cmd)
876-
if _DRY_RUN:
879+
if self.dry_run:
877880
return 0
878881
return subprocess.call(cmd, **kwargs)
879882

@@ -885,7 +888,7 @@ def check_call(self, cmd: list[str], **kwargs):
885888
using subprocess directly, to keep accurate debug logs.
886889
'''
887890
self._log_cmd(cmd)
888-
if _DRY_RUN:
891+
if self.dry_run:
889892
return
890893
subprocess.check_call(cmd, **kwargs)
891894

@@ -897,7 +900,7 @@ def check_output(self, cmd: list[str], **kwargs) -> bytes:
897900
using subprocess directly, to keep accurate debug logs.
898901
'''
899902
self._log_cmd(cmd)
900-
if _DRY_RUN:
903+
if self.dry_run:
901904
return b''
902905
return subprocess.check_output(cmd, **kwargs)
903906

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

920923
self._log_cmd(cmd)
921-
if _DRY_RUN:
924+
if self.dry_run:
922925
return _DebugDummyPopen() # type: ignore
923926

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

scripts/west_commands/runners/nrfutil.py

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

77
import json
8-
import shlex
98
import subprocess
109
import sys
1110
from pathlib import Path
1211

13-
from runners.core import _DRY_RUN
1412
from runners.nrf_common import NrfBinaryRunner
1513

1614

@@ -70,14 +68,10 @@ def _exec(self, args, force=False):
7068
jout_all = []
7169

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

74-
escaped = ' '.join(shlex.quote(s) for s in cmd)
75-
if _DRY_RUN or (self.dry_run):
76-
self.logger.info(escaped)
77-
if not force:
78-
return {}
79-
else:
80-
self.logger.debug(escaped)
73+
if self.dry_run and not force:
74+
return {}
8175

8276
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as p:
8377
for line in iter(p.stdout.readline, b''):

0 commit comments

Comments
 (0)