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    'blackmagicprobe',
30    'bossac',
31    'canopen_program',
32    'dediprog',
33    'dfu',
34    'ecpprog',
35    'esp32',
36    'ezflashcli',
37    'gd32isp',
38    'hifive1',
39    'intel_adsp',
40    'intel_cyclonev',
41    'jlink',
42    'linkserver',
43    'mdb',
44    'minichlink',
45    'misc',
46    'native',
47    'nios2',
48    'nrfjprog',
49    'nrfutil',
50    'nsim',
51    'nxp_s32dbg',
52    'openocd',
53    'probe_rs',
54    'pyocd',
55    'qemu',
56    'renode',
57    'renode-robot',
58    'silabs_commander',
59    'spi_burn',
60    'stm32cubeprogrammer',
61    'stm32flash',
62    'teensy',
63    'trace32',
64    'uf2',
65    'xsdb',
66    'xtensa',
67    # zephyr-keep-sorted-stop
68]
69
70for _name in _names:
71    _import_runner_module(_name)
72
73def get_runner_cls(runner):
74    '''Get a runner's class object, given its name.'''
75    for cls in ZephyrBinaryRunner.get_runners():
76        if cls.name() == runner:
77            return cls
78    raise ValueError(f'unknown runner "{runner}"')
79
80__all__ = ['ZephyrBinaryRunner', 'MissingProgram', 'get_runner_cls']
81