1# Copyright (c) 2017 Linaro Limited.
2#
3# SPDX-License-Identifier: Apache-2.0
4
5import importlib
6import logging
7
8from runners.core import ZephyrBinaryRunner, MissingProgram
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    'blackmagicprobe',
29    'bossac',
30    'canopen_program',
31    'dediprog',
32    'dfu',
33    'esp32',
34    'ezflashcli',
35    'gd32isp',
36    'hifive1',
37    'intel_adsp',
38    'intel_cyclonev',
39    'jlink',
40    'mdb',
41    'misc',
42    'nios2',
43    'nrfjprog',
44    'nrfutil',
45    'nsim',
46    'openocd',
47    'pyocd',
48    'qemu',
49    'silabs_commander',
50    'spi_burn',
51    'stm32cubeprogrammer',
52    'stm32flash',
53    'trace32',
54    'uf2',
55    'xtensa',
56    # Keep this list sorted by runner name; don't add to the end.
57]
58
59for _name in _names:
60    _import_runner_module(_name)
61
62def get_runner_cls(runner):
63    '''Get a runner's class object, given its name.'''
64    for cls in ZephyrBinaryRunner.get_runners():
65        if cls.name() == runner:
66            return cls
67    raise ValueError('unknown runner "{}"'.format(runner))
68
69__all__ = ['ZephyrBinaryRunner', 'get_runner_cls']
70