Skip to content

Commit cd8d43b

Browse files
Marti BolivarAnas Nashif
Marti Bolivar
authored and
Anas Nashif
committed
scripts: runner: generalize commands to "capabilities"
Some configuration options or device tree nodes affect the way that runners ought to behave, but there's no good way for them to report whether they can handle them. One motivating example is CONFIG_FLASH_LOAD_OFFSET, as influenced by the zephyr,code-partition chosen node in the DT for architectures where CONFIG_HAS_FLASH_LOAD_OFFSET=y. If CONFIG_FLASH_LOAD_OFFSET is nonzero, the 'flash' command ought to place the kernel at that address offset from the device flash's start address. Runners don't support this right now, which should be fixed. However, we don't want to mandate support for this feature, since not all targets need it. We need to let runners declare what their capabilities are. Make it so by adding a RunnerCaps class to the runner core. This currently just states which commands a runner can handle, but can be generalized to implement the above use case. Signed-off-by: Marti Bolivar <[email protected]>
1 parent e33ec24 commit cd8d43b

File tree

7 files changed

+42
-27
lines changed

7 files changed

+42
-27
lines changed

scripts/support/runner/bossac.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import platform
1010

11-
from .core import ZephyrBinaryRunner, get_env_or_bail
11+
from .core import ZephyrBinaryRunner, RunnerCaps, get_env_or_bail
1212

1313
DEFAULT_BOSSAC_PORT = '/dev/ttyACM0'
1414

@@ -28,8 +28,8 @@ def name(cls):
2828
return 'bossac'
2929

3030
@classmethod
31-
def handles_command(cls, command):
32-
return command == 'flash'
31+
def capabilities(cls):
32+
return RunnerCaps(commands={'flash'})
3333

3434
def create_from_env(command, debug):
3535
'''Create flasher from environment.

scripts/support/runner/core.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ def _parser_darwin(self, cmd):
143143
return {int(b) for b in used_bytes}
144144

145145

146+
class RunnerCaps:
147+
'''This class represents a runner class's capabilities.
148+
149+
The most basic capability is the set of supported commands,
150+
available in the commands field. This defaults to all three
151+
commands.'''
152+
153+
def __init__(self, commands={'flash', 'debug', 'debugserver'}):
154+
self.commands = commands
155+
156+
146157
class ZephyrBinaryRunner(abc.ABC):
147158
'''Abstract superclass for binary runners (flashers, debuggers).
148159
@@ -184,7 +195,7 @@ class ZephyrBinaryRunner(abc.ABC):
184195
185196
1. Define a ZephyrBinaryRunner subclass, and implement its
186197
abstract methods. Override any methods you need to, especially
187-
handles_command().
198+
capabilities().
188199
189200
2. Make sure the Python module defining your runner class is
190201
imported by this package's __init__.py (otherwise,
@@ -229,7 +240,8 @@ def create_runner(runner_name, command, debug):
229240
else:
230241
raise ValueError('no runner named {} is known'.format(runner_name))
231242

232-
if not cls.handles_command(command):
243+
caps = cls.capabilities()
244+
if command not in caps.commands:
233245
raise ValueError('runner {} does not implement command {}'.format(
234246
runner_name, command))
235247

@@ -246,13 +258,15 @@ def name(cls):
246258
etc.).'''
247259

248260
@classmethod
249-
def handles_command(cls, command):
250-
'''Return True iff this class can run the given command.
261+
def capabilities(cls):
262+
'''Returns a RunnerCaps representing this runner's capabilities.
263+
264+
This implementation returns the default capabilities, which
265+
includes support for all three commands, but no other special
266+
powers.
251267
252-
The default implementation returns True if the command is
253-
valid (i.e. is one of "flash", "debug", and "debugserver").
254-
Subclasses should override if they only provide a subset.'''
255-
return command in {'flash', 'debug', 'debugserver'}
268+
Subclasses should override appropriately if needed.'''
269+
return RunnerCaps()
256270

257271
@staticmethod
258272
@abc.abstractmethod
@@ -263,7 +277,8 @@ def run(self, command, **kwargs):
263277
'''Runs command ('flash', 'debug', 'debugserver').
264278
265279
This is the main entry point to this runner.'''
266-
if not self.handles_command(command):
280+
caps = self.capabilities()
281+
if command not in caps.commands:
267282
raise ValueError('runner {} does not implement command {}'.format(
268283
self.name(), command))
269284
self.do_run(command, **kwargs)

scripts/support/runner/dfu.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
import time
1010

11-
from .core import ZephyrBinaryRunner, get_env_or_bail
11+
from .core import ZephyrBinaryRunner, RunnerCaps, get_env_or_bail
1212

1313

1414
class DfuUtilBinaryRunner(ZephyrBinaryRunner):
@@ -30,8 +30,8 @@ def name(cls):
3030
return 'dfu-util'
3131

3232
@classmethod
33-
def handles_command(cls, command):
34-
return command == 'flash'
33+
def capabilities(cls):
34+
return RunnerCaps(commands={'flash'})
3535

3636
def create_from_env(command, debug):
3737
'''Create flasher from environment.

scripts/support/runner/esp32.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from os import path
88
import os
99

10-
from .core import ZephyrBinaryRunner, get_env_or_bail
10+
from .core import ZephyrBinaryRunner, RunnerCaps, get_env_or_bail
1111

1212

1313
class Esp32BinaryRunner(ZephyrBinaryRunner):
@@ -30,8 +30,8 @@ def name(cls):
3030
return 'esp32'
3131

3232
@classmethod
33-
def handles_command(cls, command):
34-
return command == 'flash'
33+
def capabilities(cls):
34+
return RunnerCaps(commands={'flash'})
3535

3636
def create_from_env(command, debug):
3737
'''Create flasher from environment.

scripts/support/runner/jlink.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from os import path
88
import os
99

10-
from .core import ZephyrBinaryRunner, get_env_or_bail
10+
from .core import ZephyrBinaryRunner, RunnerCaps, get_env_or_bail
1111

1212
DEFAULT_JLINK_GDB_PORT = 2331
1313

@@ -33,8 +33,8 @@ def name(cls):
3333
return 'jlink'
3434

3535
@classmethod
36-
def handles_command(cls, command):
37-
return command in {'debug', 'debugserver'}
36+
def capabilities(cls):
37+
return RunnerCaps(commands={'debug', 'debugserver'})
3838

3939
def create_from_env(command, debug):
4040
'''Create runner from environment.

scripts/support/runner/nrfjprog.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from os import path
88
import sys
99

10-
from .core import ZephyrBinaryRunner, get_env_or_bail
10+
from .core import ZephyrBinaryRunner, RunnerCaps, get_env_or_bail
1111

1212

1313
class NrfJprogBinaryRunner(ZephyrBinaryRunner):
@@ -23,8 +23,8 @@ def name(cls):
2323
return 'nrfjprog'
2424

2525
@classmethod
26-
def handles_command(cls, command):
27-
return command == 'flash'
26+
def capabilities(cls):
27+
return RunnerCaps(commands={'flash'})
2828

2929
def create_from_env(command, debug):
3030
'''Create flasher from environment.

scripts/support/runner/xtensa.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from os import path
88

9-
from .core import ZephyrBinaryRunner, get_env_or_bail
9+
from .core import ZephyrBinaryRunner, RunnerCaps, get_env_or_bail
1010

1111

1212
class XtensaBinaryRunner(ZephyrBinaryRunner):
@@ -22,8 +22,8 @@ def name(cls):
2222
return 'xtensa'
2323

2424
@classmethod
25-
def handles_command(cls, command):
26-
return command == 'debug'
25+
def capabilities(cls):
26+
return RunnerCaps(commands={'debug'})
2727

2828
def create_from_env(command, debug):
2929
'''Create runner from environment.

0 commit comments

Comments
 (0)