blob: fbc7f7d525d80a9a16190cabd0d5d99ec7d30aa0 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006# Enable other layers to have modules in the same named directory
7from pkgutil import extend_path
8__path__ = extend_path(__path__, __name__)
9
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010# Borrowed from CalledProcessError
11
12class 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 Williamsd8c66bc2016-06-20 12:57:21 -050020def 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 Bishop6e60e8b2018-02-01 10:27:11 -050043
44def make_logger_bitbake_compatible(logger):
45 import logging
46
47 """
Andrew Geisslerd1e89492021-02-12 15:35:20 -060048 We need to raise the log level of the info output so unittest
49 messages are visible on the console.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050050 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 def _bitbake_log_info(msg, *args, **kwargs):
52 logger.log(logging.INFO + 1, msg, *args, **kwargs)
53
Brad Bishop6e60e8b2018-02-01 10:27:11 -050054 logger.info = _bitbake_log_info
55
56 return logger
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057
58def 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