3 Copyright (c) 2011-2015 ARM Limited
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
17 Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
21 from imp
import load_source
27 spec = importlib.util.spec_from_file_location(module_name, file_path)
28 module = importlib.util.module_from_spec(spec)
29 spec.loader.exec_module(module)
30 sys.modules[module_name] = module
34 from inspect
import getmembers, isclass
35 from os
import listdir
36 from os.path
import abspath, exists, isdir, isfile, join
38 from ..host_tests.base_host_test
import BaseHostTest
42 """ Class stores registry with host tests and objects representing them
47 """! Registers host test object by name
49 @param ht_name Host test unique name
50 @param ht_object Host test class object
56 """! Unregisters host test object by name
58 @param ht_name Host test unique name
64 """! Fetches host test object by name
66 @param ht_name Host test unique name
68 @return Host test callable object or None if object is not found
73 """! Checks (by name) if host test object is registered already
75 @param ht_name Host test unique name
77 @return True if ht_name is registered (available), else False
79 return (ht_name
in self.
HOST_TESTSHOST_TESTS
and
80 self.
HOST_TESTSHOST_TESTS[ht_name]
is not None)
83 """! Prints list of registered host test classes (by name)
84 @Detail For devel & debug purposes
86 from prettytable
import PrettyTable, HEADER
87 column_names = [
'name',
'class',
'origin']
88 pt = PrettyTable(column_names, junction_char=
"|", hrules=HEADER)
89 for column
in column_names:
90 pt.align[column] =
'l'
92 for name, host_test
in sorted(self.
HOST_TESTSHOST_TESTS.items()):
93 cls_str = str(host_test.__class__)
94 if host_test.script_location:
95 src_path = host_test.script_location
97 src_path =
'mbed-host-tests'
98 pt.add_row([name, cls_str, src_path])
99 return pt.get_string()
102 """ Enumerates and registers locally stored host tests
103 Host test are derived from mbed_host_tests.BaseHostTest classes
106 path = path.strip(
'"')
108 print(
"HOST: Inspecting '%s' for local host tests..." % path)
109 if exists(path)
and isdir(path):
111 f
for f
in listdir(path)
112 if isfile(join(path, f))
and f.endswith(
".py")
114 for module_file
in python_modules:
117 def _add_module_to_registry(self, path, module_file, verbose):
118 module_name = module_file[:-3]
120 mod =
load_source(module_name, abspath(join(path, module_file)))
121 except Exception
as e:
123 "HOST: Error! While loading local host test module '%s'"
124 % join(path, module_file)
126 print(
"HOST: %s" % str(e))
129 print(
"HOST: Loading module '%s': %s" % (module_file, str(mod)))
131 for name, obj
in getmembers(mod):
134 issubclass(obj, BaseHostTest)
and
135 str(obj) != str(BaseHostTest)
138 host_test_name = obj.name
140 host_test_name = module_name
142 host_test_cls.script_location = join(path, module_file)
145 "HOST: Found host test implementation: %s -|> %s"
146 % (str(obj), str(BaseHostTest))
149 "HOST: Registering '%s' as '%s'"
150 % (str(host_test_cls), host_test_name)
153 host_test_name, host_test_cls()
def unregister_host_test(self, ht_name)
Unregisters host test object by name.
def register_host_test(self, ht_name, ht_object)
Registers host test object by name.
def is_host_test(self, ht_name)
Checks (by name) if host test object is registered already.
def register_from_path(self, path, verbose=False)
def _add_module_to_registry(self, path, module_file, verbose)
def get_host_test(self, ht_name)
Fetches host test object by name.
def table(self, verbose=False)
Prints list of registered host test classes (by name) @Detail For devel & debug purposes.
def load_source(module_name, file_path)