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    'sftool',
60    'silabs_commander',
61    'spi_burn',
62    'spsdk',
63    'stlink_gdbserver',
64    'stm32cubeprogrammer',
65    'stm32flash',
66    'sy1xx',
67    'teensy',
68    'trace32',
69    'uf2',
70    'xsdb',
71    'xtensa',
72    # zephyr-keep-sorted-stop
73]
74
75for _name in _names:
76    _import_runner_module(_name)
77
78def get_runner_cls(runner):
79    '''Get a runner's class object, given its name.'''
80    for cls in ZephyrBinaryRunner.get_runners():
81        if cls.name() == runner:
82            return cls
83    raise ValueError(f'unknown runner "{runner}"')
84
85__all__ = ['ZephyrBinaryRunner', 'MissingProgram', 'get_runner_cls']
86