xref: /OK3568_Linux_fs/yocto/poky/meta/lib/oeqa/utils/__init__.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#
2# SPDX-License-Identifier: MIT
3#
4# Enable other layers to have modules in the same named directory
5from pkgutil import extend_path
6__path__ = extend_path(__path__, __name__)
7
8# Borrowed from CalledProcessError
9
10class 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
18def 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
41
42def make_logger_bitbake_compatible(logger):
43    import logging
44
45    """
46    We need to raise the log level of the info output so unittest
47    messages are visible on the console.
48    """
49    def _bitbake_log_info(msg, *args, **kwargs):
50        logger.log(logging.INFO + 1, msg, *args, **kwargs)
51
52    logger.info = _bitbake_log_info
53
54    return logger
55
56def 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
91