Skip to content

Commit c21478a

Browse files
committed
[nrf fromtree] 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 <[email protected]> (cherry picked from commit ffe2028)
1 parent 8b5c612 commit c21478a

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
@@ -493,6 +493,9 @@ def __init__(self, cfg: RunnerConfig):
493493
self.logger = logging.getLogger(f'runners.{self.name()}')
494494
'''logging.Logger for this instance.'''
495495

496+
self.dry_run = _DRY_RUN
497+
'''log commands instead of executing them. Can be set by subclasses'''
498+
496499
@staticmethod
497500
def get_runners() -> list[type['ZephyrBinaryRunner']]:
498501
'''Get a list of all currently defined runner classes.'''
@@ -867,7 +870,7 @@ def run_client(self, client, **kwargs):
867870

868871
def _log_cmd(self, cmd: list[str]):
869872
escaped = ' '.join(shlex.quote(s) for s in cmd)
870-
if not _DRY_RUN:
873+
if not self.dry_run:
871874
self.logger.debug(escaped)
872875
else:
873876
self.logger.info(escaped)
@@ -880,7 +883,7 @@ def call(self, cmd: list[str], **kwargs) -> int:
880883
using subprocess directly, to keep accurate debug logs.
881884
'''
882885
self._log_cmd(cmd)
883-
if _DRY_RUN:
886+
if self.dry_run:
884887
return 0
885888
return subprocess.call(cmd, **kwargs)
886889

@@ -892,7 +895,7 @@ def check_call(self, cmd: list[str], **kwargs):
892895
using subprocess directly, to keep accurate debug logs.
893896
'''
894897
self._log_cmd(cmd)
895-
if _DRY_RUN:
898+
if self.dry_run:
896899
return
897900
subprocess.check_call(cmd, **kwargs)
898901

@@ -904,7 +907,7 @@ def check_output(self, cmd: list[str], **kwargs) -> bytes:
904907
using subprocess directly, to keep accurate debug logs.
905908
'''
906909
self._log_cmd(cmd)
907-
if _DRY_RUN:
910+
if self.dry_run:
908911
return b''
909912
return subprocess.check_output(cmd, **kwargs)
910913

@@ -925,7 +928,7 @@ def popen_ignore_int(self, cmd: list[str], **kwargs) -> subprocess.Popen:
925928
preexec = os.setsid # type: ignore
926929

927930
self._log_cmd(cmd)
928-
if _DRY_RUN:
931+
if self.dry_run:
929932
return _DebugDummyPopen() # type: ignore
930933

931934
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)