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