Skip to content

Commit c9dfbfa

Browse files
Marti Bolivarnashif
authored andcommitted
scripts: west: convert runner to use log module
Replace all informational messages with calls to log functions. Cases which must interact via the terminal and standard output are not modified. Signed-off-by: Marti Bolivar <[email protected]>
1 parent bc7b1c5 commit c9dfbfa

File tree

9 files changed

+54
-35
lines changed

9 files changed

+54
-35
lines changed

scripts/meta/west/runner/core.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
import signal
1919
import subprocess
2020

21+
from .. import log
2122
from ..util import quote_sh_list
2223

2324
# Turn on to enable just printing the commands that would be run,
2425
# without actually running them. This can break runners that are expecting
2526
# output or if one command depends on another, so it's just for debugging.
26-
DEBUG = False
27+
JUST_PRINT = False
2728

2829

2930
class _DebugDummyPopen:
@@ -404,12 +405,13 @@ def call(self, cmd):
404405
subprocess and get its return code, rather than
405406
using subprocess directly, to keep accurate debug logs.
406407
'''
407-
if DEBUG or self.debug:
408-
print(quote_sh_list(cmd))
408+
quoted = quote_sh_list(cmd)
409409

410-
if DEBUG:
410+
if JUST_PRINT:
411+
log.inf(quoted)
411412
return 0
412413

414+
log.dbg(quoted)
413415
return subprocess.call(cmd)
414416

415417
def check_call(self, cmd):
@@ -419,17 +421,16 @@ def check_call(self, cmd):
419421
subprocess and check that it executed correctly, rather than
420422
using subprocess directly, to keep accurate debug logs.
421423
'''
422-
if DEBUG or self.debug:
423-
print(quote_sh_list(cmd))
424+
quoted = quote_sh_list(cmd)
424425

425-
if DEBUG:
426+
if JUST_PRINT:
427+
log.inf(quoted)
426428
return
427429

430+
log.dbg(quoted)
428431
try:
429432
subprocess.check_call(cmd)
430433
except subprocess.CalledProcessError:
431-
if self.debug:
432-
print('Error running {}'.format(quote_sh_list(cmd)))
433434
raise
434435

435436
def check_output(self, cmd):
@@ -439,17 +440,16 @@ def check_output(self, cmd):
439440
subprocess and check that it executed correctly, rather than
440441
using subprocess directly, to keep accurate debug logs.
441442
'''
442-
if self.debug:
443-
print(quote_sh_list(cmd))
443+
quoted = quote_sh_list(cmd)
444444

445-
if DEBUG:
445+
if JUST_PRINT:
446+
log.inf(quoted)
446447
return b''
447448

449+
log.dbg(quoted)
448450
try:
449451
return subprocess.check_output(cmd)
450452
except subprocess.CalledProcessError:
451-
if self.debug:
452-
print('Error running {}'.format(quote_sh_list(cmd)))
453453
raise
454454

455455
def popen_ignore_int(self, cmd):
@@ -459,16 +459,16 @@ def popen_ignore_int(self, cmd):
459459
cflags = 0
460460
preexec = None
461461
system = platform.system()
462+
quoted = quote_sh_list(cmd)
462463

463464
if system == 'Windows':
464465
cflags |= subprocess.CREATE_NEW_PROCESS_GROUP
465466
elif system in {'Linux', 'Darwin'}:
466467
preexec = os.setsid
467468

468-
if DEBUG or self.debug:
469-
print(quote_sh_list(cmd))
470-
471-
if DEBUG:
469+
if JUST_PRINT:
470+
log.inf(quoted)
472471
return _DebugDummyPopen()
473472

473+
log.dbg(quoted)
474474
return subprocess.Popen(cmd, creationflags=cflags, preexec_fn=preexec)

scripts/meta/west/runner/dfu.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import time
1111

12+
from .. import log
1213
from .core import ZephyrBinaryRunner, RunnerCaps, BuildConfiguration
1314

1415

@@ -92,6 +93,11 @@ def do_run(self, command, **kwargs):
9293
reset = False
9394
if not self.find_device():
9495
reset = True
96+
log.dbg('Device not found, waiting for it',
97+
level=log.VERBOSE_EXTREME)
98+
# Use of print() here is advised. We don't want to lose
99+
# this information in a separate log -- this is
100+
# interactive and requires a terminal.
95101
print('Please reset your board to switch to DFU mode...')
96102
while not self.find_device():
97103
time.sleep(0.1)

scripts/meta/west/runner/esp32.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from os import path
88

9+
from .. import log
910
from .core import ZephyrBinaryRunner, RunnerCaps
1011

1112

@@ -79,8 +80,8 @@ def do_run(self, command, **kwargs):
7980
'--flash_size', self.flash_size,
8081
'0x1000', bin_name]
8182

82-
print("Converting ELF to BIN")
83+
log.inf("Converting ELF to BIN")
8384
self.check_call(cmd_convert)
8485

85-
print("Flashing ESP32 on {} ({}bps)".format(self.device, self.baud))
86+
log.inf("Flashing ESP32 on {} ({}bps)".format(self.device, self.baud))
8687
self.check_call(cmd_flash)

scripts/meta/west/runner/intel_s1000.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
'''Runner for debugging and flashing intel_s1000 devices'''
77
from os import path
8-
from .core import ZephyrBinaryRunner
98
import time
109
import subprocess
1110

11+
from .. import log
12+
from .core import ZephyrBinaryRunner
13+
1214
DEFAULT_XT_GDB_PORT = 20000
1315

1416

@@ -122,7 +124,7 @@ def do_debug(self):
122124
raise subprocess.CalledProcessError((retcode, gdb_cmd))
123125

124126
def print_gdbserver_message(self, gdb_port):
125-
print('Intel S1000 GDB server running on port {}'.format(gdb_port))
127+
log.inf('Intel S1000 GDB server running on port {}'.format(gdb_port))
126128

127129
def debugserver(self, **kwargs):
128130
topology_file = kwargs['ocd-topology']

scripts/meta/west/runner/jlink.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import os
88
import tempfile
9+
10+
from .. import log
911
from .core import ZephyrBinaryRunner, RunnerCaps, BuildConfiguration
1012

1113
DEFAULT_JLINK_GDB_PORT = 2331
@@ -78,7 +80,7 @@ def create_from_args(cls, args):
7880
tui=args.tui, debug=args.verbose)
7981

8082
def print_gdbserver_message(self):
81-
print('J-Link GDB server running on port {}'.format(self.gdb_port))
83+
log.inf('J-Link GDB server running on port {}'.format(self.gdb_port))
8284

8385
def do_run(self, command, **kwargs):
8486
server_cmd = (self.gdbserver_cmd +
@@ -137,5 +139,5 @@ def flash(self, **kwargs):
137139
'-device', self.device,
138140
'-CommanderScript', fname])
139141

140-
print('Flashing Target Device')
142+
log.inf('Flashing Target Device')
141143
self.check_call(cmd)

scripts/meta/west/runner/nios2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
'''Runner for NIOS II, based on quartus-flash.py and GDB.'''
66

7+
from .. import log
78
from .core import ZephyrBinaryRunner, NetworkPortHelper
89

910

@@ -67,7 +68,7 @@ def flash(self, **kwargs):
6768
self.check_call(cmd)
6869

6970
def print_gdbserver_message(self, gdb_port):
70-
print('Nios II GDB server running on port {}'.format(gdb_port))
71+
log.inf('Nios II GDB server running on port {}'.format(gdb_port))
7172

7273
def debug_debugserver(self, command, **kwargs):
7374
# Per comments in the shell script, the NIOSII GDB server

scripts/meta/west/runner/nrfjprog.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import sys
88

9+
from .. import log
910
from .core import ZephyrBinaryRunner, RunnerCaps
1011

1112

@@ -52,6 +53,12 @@ def get_board_snr_from_user(self):
5253
raise RuntimeError('"nrfjprog --ids" returned 0; is a debugger already connected?')
5354
return board_snr
5455

56+
log.dbg("Refusing the temptation to guess a board",
57+
level=log.VERBOSE_EXTREME)
58+
59+
# Use of print() here is advised. We don't want to lose
60+
# this information in a separate log -- this is
61+
# interactive and requires a terminal.
5562
print('There are multiple boards connected.')
5663
for i, snr in enumerate(snrs, 1):
5764
print('{}. {}'.format(i, snr))
@@ -95,5 +102,5 @@ def do_run(self, command, **kwargs):
95102
for cmd in commands:
96103
self.check_call(cmd)
97104

98-
print('Board with serial number {} flashed successfully.'.format(
105+
log.inf('Board with serial number {} flashed successfully.'.format(
99106
board_snr))

scripts/meta/west/runner/pyocd.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import sys
99
from .core import ZephyrBinaryRunner, RunnerCaps, BuildConfiguration
10+
from .. import log
1011

1112
DEFAULT_PYOCD_GDB_PORT = 3333
1213

@@ -79,13 +80,11 @@ def do_add_parser(cls, parser):
7980
def create_from_args(cls, args):
8081
daparg = os.environ.get('PYOCD_DAPARG')
8182
if daparg:
82-
print('Warning: setting PYOCD_DAPARG in the environment is',
83-
'deprecated; use the --daparg option instead.',
84-
file=sys.stderr)
83+
log.wrn('Setting PYOCD_DAPARG in the environment is',
84+
'deprecated; use the --daparg option instead.')
8585
if args.daparg is None:
86-
print('Missing --daparg set to {} from environment'.format(
87-
daparg),
88-
file=sys.stderr)
86+
log.dbg('Missing --daparg set to {} from environment'.format(
87+
daparg), level=log.VERBOSE_VERY)
8988
args.daparg = daparg
9089

9190
build_conf = BuildConfiguration(os.getcwd())
@@ -120,11 +119,11 @@ def flash(self, **kwargs):
120119
self.flashtool_extra +
121120
[self.bin_name])
122121

123-
print('Flashing Target Device')
122+
log.inf('Flashing Target Device')
124123
self.check_call(cmd)
125124

126125
def print_gdbserver_message(self):
127-
print('pyOCD GDB server running on port {}'.format(self.gdb_port))
126+
log.inf('pyOCD GDB server running on port {}'.format(self.gdb_port))
128127

129128
def debug_debugserver(self, command, **kwargs):
130129
server_cmd = ([self.gdbserver] +

scripts/meta/west/runner/qemu.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
'''Runner stub for QEMU.'''
66

7+
from .. import log
78
from .core import ZephyrBinaryRunner
89

910

@@ -27,4 +28,4 @@ def create_from_args(command, args):
2728

2829
def do_run(self, command, **kwargs):
2930
if command == 'debugserver':
30-
print('Detached GDB server')
31+
log.inf('Detached GDB server')

0 commit comments

Comments
 (0)