1# Copyright (c) 2017 Linaro Limited.
2#
3# SPDX-License-Identifier: Apache-2.0
4
5import importlib
6import logging
7
8from runners.core import MissingProgram, ZephyrBinaryRunner
9
10_logger = logging.getLogger('runners')
11
12def _import_runner_module(runner_name):
13    try:
14        importlib.import_module(f'runners.{runner_name}')
15    except ImportError as ie:
16        # Runners are supposed to gracefully handle failures when they
17        # import anything outside of stdlib, but they sometimes do
18        # not. Catch ImportError to handle this.
19        _logger.warning(f'The module for runner "{runner_name}" '
20                        f'could not be imported ({ie}). This most likely '
21                        'means it is not handling its dependencies properly. '
22                        'Please report this to the zephyr developers.')
23
24# We import these here to ensure the ZephyrBinaryRunner subclasses are
25# defined; otherwise, ZephyrBinaryRunner.get_runners() won't work.
26
27_names = [
28    # zephyr-keep-sorted-start
29    'bflb_mcu_tool',
30    'blackmagicprobe',
31    'bossac',
32    'canopen_program',
33    'dediprog',
34    'dfu',
35    'ecpprog',
36    'esp32',
37    'ezflashcli',
38    'gd32isp',
39    'hifive1',
40    'intel_adsp',
41    'intel_cyclonev',
42    'jlink',
43    'linkserver',
44    'mdb',
45    'minichlink',
46    'misc',
47    'native',
48    'nrfjprog',
49    'nrfutil',
50    'nsim',
51    'nxp_s32dbg',
52    'openocd',
53    'probe_rs',
54    'pyocd',
55    'qemu',
56    'renode',
57    'renode-robot',
58    'rfp',
59    'silabs_commander',
60    'spi_burn',
61    'spsdk',
62    'stlink_gdbserver',
63    'stm32cubeprogrammer',
64    'stm32flash',
65    'sy1xx',
66    'teensy',
67    'trace32',
68    'uf2',
69    'xsdb',
70    'xtensa',
71    # zephyr-keep-sorted-stop
72]
73
74for _name in _names:
75    _import_runner_module(_name)
76
77def get_runner_cls(runner):
78    '''Get a runner's class object, given its name.'''
79    for cls in ZephyrBinaryRunner.get_runners():
80        if cls.name() == runner:
81            return cls
82    raise ValueError(f'unknown runner "{runner}"')
83
84__all__ = ['ZephyrBinaryRunner', 'MissingProgram', 'get_runner_cls']
85