1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6# Enable other layers to have modules in the same named directory
7from pkgutil import extend_path
8__path__ = extend_path(__path__, __name__)
9
10# 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
20def 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
43
44def make_logger_bitbake_compatible(logger):
45    import logging
46
47    """
48    We need to raise the log level of the info output so unittest
49    messages are visible on the console.
50    """
51    def _bitbake_log_info(msg, *args, **kwargs):
52        logger.log(logging.INFO + 1, msg, *args, **kwargs)
53
54    logger.info = _bitbake_log_info
55
56    return logger
57
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
93
94def get_json_result_dir(d):
95    json_result_dir = os.path.join(d.getVar("LOG_DIR"), 'oeqa')
96    custom_json_result_dir = d.getVar("OEQA_JSON_RESULT_DIR")
97    if custom_json_result_dir:
98        json_result_dir = custom_json_result_dir
99    return json_result_dir