• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ARMmbed / mbed-os-tools / #457

24 Aug 2024 09:15PM UTC coverage: 0.0% (-59.9%) from 59.947%
#457

push

coveralls-python

web-flow
Merge 7c6dbce13 into c467d6f14

0 of 4902 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/mbed_os_tools/test/host_tests_registry/host_registry.py
1
# Copyright (c) 2018, Arm Limited and affiliates.
2
# SPDX-License-Identifier: Apache-2.0
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
try:
×
17
    from imp import load_source
×
18
except ImportError:
×
19
    import importlib
×
20
    import sys
×
21

22
    def load_source(module_name, file_path):
×
23
        spec = importlib.util.spec_from_file_location(module_name, file_path)
×
24
        module = importlib.util.module_from_spec(spec)
×
25
        spec.loader.exec_module(module)
×
26
        sys.modules[module_name] = module
×
27
        return module
×
28

29

30
from inspect import getmembers, isclass
×
31
from os import listdir
×
32
from os.path import abspath, exists, isdir, isfile, join
×
33

34
from ..host_tests.base_host_test import BaseHostTest
×
35

36

37
class HostRegistry:
×
38
    """ Class stores registry with host tests and objects representing them
39
    """
40
    HOST_TESTS = {}  # Map between host_test_name -> host_test_object
×
41

42
    def register_host_test(self, ht_name, ht_object):
×
43
        """! Registers host test object by name
44

45
        @param ht_name Host test unique name
46
        @param ht_object Host test class object
47
        """
48
        if ht_name not in self.HOST_TESTS:
×
49
            self.HOST_TESTS[ht_name] = ht_object
×
50

51
    def unregister_host_test(self, ht_name):
×
52
        """! Unregisters host test object by name
53

54
        @param ht_name Host test unique name
55
        """
56
        if ht_name in self.HOST_TESTS:
×
57
            del self.HOST_TESTS[ht_name]
×
58

59
    def get_host_test(self, ht_name):
×
60
        """! Fetches host test object by name
61

62
        @param ht_name Host test unique name
63

64
        @return Host test callable object or None if object is not found
65
        """
66
        return self.HOST_TESTS[ht_name] if ht_name in self.HOST_TESTS else None
×
67

68
    def is_host_test(self, ht_name):
×
69
        """! Checks (by name) if host test object is registered already
70

71
        @param ht_name Host test unique name
72

73
        @return True if ht_name is registered (available), else False
74
        """
75
        return (ht_name in self.HOST_TESTS and
×
76
                self.HOST_TESTS[ht_name] is not None)
77

78
    def table(self, verbose=False):
×
79
        """! Prints list of registered host test classes (by name)
80
            @Detail For devel & debug purposes
81
        """
82
        from prettytable import PrettyTable, HEADER
×
83
        column_names = ['name', 'class', 'origin']
×
84
        pt = PrettyTable(column_names, junction_char="|", hrules=HEADER)
×
85
        for column in column_names:
×
86
            pt.align[column] = 'l'
×
87

88
        for name, host_test in sorted(self.HOST_TESTS.items()):
×
89
            cls_str = str(host_test.__class__)
×
90
            if host_test.script_location:
×
91
                src_path = host_test.script_location
×
92
            else:
93
                src_path = 'mbed-host-tests'
×
94
            pt.add_row([name, cls_str, src_path])
×
95
        return pt.get_string()
×
96

97
    def register_from_path(self, path, verbose=False):
×
98
        """ Enumerates and registers locally stored host tests
99
            Host test are derived from mbed_os_tools.test.BaseHostTest classes
100
        """
101
        if path:
×
102
            path = path.strip('"')
×
103
            if verbose:
×
104
                print("HOST: Inspecting '%s' for local host tests..." % path)
×
105
            if exists(path) and isdir(path):
×
106
                python_modules = [
×
107
                    f for f in listdir(path)
108
                    if isfile(join(path, f)) and f.endswith(".py")
109
                ]
110
                for module_file in python_modules:
×
111
                    self._add_module_to_registry(path, module_file, verbose)
×
112

113
    def _add_module_to_registry(self, path, module_file, verbose):
×
114
        module_name = module_file[:-3]
×
115
        try:
×
116
            mod = load_source(module_name, abspath(join(path, module_file)))
×
117
        except Exception as e:
×
118
            print(
×
119
                "HOST: Error! While loading local host test module '%s'"
120
                % join(path, module_file)
121
            )
122
            print("HOST: %s" % str(e))
×
123
            return
×
124
        if verbose:
×
125
            print("HOST: Loading module '%s': %s" % (module_file, str(mod)))
×
126

127
        for name, obj in getmembers(mod):
×
128
            if (
×
129
                isclass(obj) and
130
                issubclass(obj, BaseHostTest) and
131
                str(obj) != str(BaseHostTest)
132
            ):
133
                if obj.name:
×
134
                    host_test_name = obj.name
×
135
                else:
136
                    host_test_name = module_name
×
137
                host_test_cls = obj
×
138
                host_test_cls.script_location = join(path, module_file)
×
139
                if verbose:
×
140
                    print(
×
141
                        "HOST: Found host test implementation: %s -|> %s"
142
                        % (str(obj), str(BaseHostTest))
143
                    )
144
                    print(
×
145
                        "HOST: Registering '%s' as '%s'"
146
                        % (str(host_test_cls), host_test_name)
147
                    )
148
                self.register_host_test(
×
149
                    host_test_name, host_test_cls()
150
                )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc