Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 4 | # Enable other layers to have modules in the same named directory |
| 5 | from pkgutil import extend_path |
| 6 | __path__ = extend_path(__path__, __name__) |
| 7 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | # Borrowed from CalledProcessError |
| 9 | |
| 10 | class CommandError(Exception): |
| 11 | def __init__(self, retcode, cmd, output = None): |
| 12 | self.retcode = retcode |
| 13 | self.cmd = cmd |
| 14 | self.output = output |
| 15 | def __str__(self): |
| 16 | return "Command '%s' returned non-zero exit status %d with output: %s" % (self.cmd, self.retcode, self.output) |
| 17 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 18 | def avoid_paths_in_environ(paths): |
| 19 | """ |
| 20 | Searches for every path in os.environ['PATH'] |
| 21 | if found remove it. |
| 22 | |
| 23 | Returns new PATH without avoided PATHs. |
| 24 | """ |
| 25 | import os |
| 26 | |
| 27 | new_path = '' |
| 28 | for p in os.environ['PATH'].split(':'): |
| 29 | avoid = False |
| 30 | for pa in paths: |
| 31 | if pa in p: |
| 32 | avoid = True |
| 33 | break |
| 34 | if avoid: |
| 35 | continue |
| 36 | |
| 37 | new_path = new_path + p + ':' |
| 38 | |
| 39 | new_path = new_path[:-1] |
| 40 | return new_path |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 41 | |
| 42 | def make_logger_bitbake_compatible(logger): |
| 43 | import logging |
| 44 | |
| 45 | """ |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 46 | We need to raise the log level of the info output so unittest |
| 47 | messages are visible on the console. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 49 | def _bitbake_log_info(msg, *args, **kwargs): |
| 50 | logger.log(logging.INFO + 1, msg, *args, **kwargs) |
| 51 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 52 | logger.info = _bitbake_log_info |
| 53 | |
| 54 | return logger |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 55 | |
| 56 | def load_test_components(logger, executor): |
| 57 | import sys |
| 58 | import os |
| 59 | import importlib |
| 60 | |
| 61 | from oeqa.core.context import OETestContextExecutor |
| 62 | |
| 63 | components = {} |
| 64 | |
| 65 | for path in sys.path: |
| 66 | base_dir = os.path.join(path, 'oeqa') |
| 67 | if os.path.exists(base_dir) and os.path.isdir(base_dir): |
| 68 | for file in os.listdir(base_dir): |
| 69 | comp_name = file |
| 70 | comp_context = os.path.join(base_dir, file, 'context.py') |
| 71 | if os.path.exists(comp_context): |
| 72 | comp_plugin = importlib.import_module('oeqa.%s.%s' % \ |
| 73 | (comp_name, 'context')) |
| 74 | try: |
| 75 | if not issubclass(comp_plugin._executor_class, |
| 76 | OETestContextExecutor): |
| 77 | raise TypeError("Component %s in %s, _executor_class "\ |
| 78 | "isn't derived from OETestContextExecutor."\ |
| 79 | % (comp_name, comp_context)) |
| 80 | |
| 81 | if comp_plugin._executor_class._script_executor \ |
| 82 | != executor: |
| 83 | continue |
| 84 | |
| 85 | components[comp_name] = comp_plugin._executor_class() |
| 86 | except AttributeError: |
| 87 | raise AttributeError("Component %s in %s don't have "\ |
| 88 | "_executor_class defined." % (comp_name, comp_context)) |
| 89 | |
| 90 | return components |