zephyr/scripts/west_commands/runners/__init__.py
Martí Bolívar d61545b513 scripts: runners: handle failure to import individual runners
We need all available runners to be defined as subclasses of
runners.core.ZephyrBinaryRunner in order to be able to look them up by
name at runtime. We do this by importing them from runners.__init__.

This process periodically fails when some runner or other cannot be
imported, usually because it is trying to import something outside of
stdlib and not handling ImportError.

Rather than letting this bring down the entire Python process, catch
and log the error. Sort the list again while we're here.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-03-17 11:30:45 +01:00

62 lines
1.7 KiB
Python

# Copyright (c) 2017 Linaro Limited.
#
# SPDX-License-Identifier: Apache-2.0
import importlib
import logging
from runners.core import ZephyrBinaryRunner, MissingProgram
_logger = logging.getLogger('runners')
def _import_runner_module(runner_name):
try:
importlib.import_module(f'runners.{runner_name}')
except ImportError:
# Runners are supposed to gracefully handle failures when they
# import anything outside of stdlib, but they sometimes do
# not. Catch ImportError to handle this.
_logger.warning(f'The module for runner "{runner_name}" '
'could not be imported. This most likely means '
'it is not handling its dependencies properly. '
'Please report this to the zephyr developers.')
# We import these here to ensure the ZephyrBinaryRunner subclasses are
# defined; otherwise, ZephyrBinaryRunner.get_runners() won't work.
_names = [
'blackmagicprobe',
'bossac',
'canopen_program',
'dediprog',
'dfu',
'esp32',
'hifive1',
'intel_s1000',
'jlink',
'mdb',
'misc',
'nios2',
'nrfjprog',
'nsim',
'openocd',
'pyocd',
'qemu',
'stm32cubeprogrammer',
'stm32flash',
'xtensa',
# Keep this list sorted by runner name; don't add to the end.
]
for _name in _names:
_import_runner_module(_name)
def get_runner_cls(runner):
'''Get a runner's class object, given its name.'''
for cls in ZephyrBinaryRunner.get_runners():
if cls.name() == runner:
return cls
raise ValueError('unknown runner "{}"'.format(runner))
__all__ = ['ZephyrBinaryRunner', 'get_runner_cls']