Mbed Host Tests
host_registry.py
Go to the documentation of this file.
1 """
2 mbed SDK
3 Copyright (c) 2011-2015 ARM Limited
4 
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
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
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.
16 
17 Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
18 """
19 
20 try:
21  from imp import load_source
22 except ImportError:
23  import importlib
24  import sys
25 
26  def load_source(module_name, file_path):
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
31  return module
32 
33 
34 from inspect import getmembers, isclass
35 from os import listdir
36 from os.path import abspath, exists, isdir, isfile, join
37 
38 from ..host_tests.base_host_test import BaseHostTest
39 
40 
42  """ Class stores registry with host tests and objects representing them
43  """
44  HOST_TESTS = {} # Map between host_test_name -> host_test_object
45 
46  def register_host_test(self, ht_name, ht_object):
47  """! Registers host test object by name
48 
49  @param ht_name Host test unique name
50  @param ht_object Host test class object
51  """
52  if ht_name not in self.HOST_TESTSHOST_TESTS:
53  self.HOST_TESTSHOST_TESTS[ht_name] = ht_object
54 
55  def unregister_host_test(self, ht_name):
56  """! Unregisters host test object by name
57 
58  @param ht_name Host test unique name
59  """
60  if ht_name in self.HOST_TESTSHOST_TESTS:
61  del self.HOST_TESTSHOST_TESTS[ht_name]
62 
63  def get_host_test(self, ht_name):
64  """! Fetches host test object by name
65 
66  @param ht_name Host test unique name
67 
68  @return Host test callable object or None if object is not found
69  """
70  return self.HOST_TESTSHOST_TESTS[ht_name] if ht_name in self.HOST_TESTSHOST_TESTS else None
71 
72  def is_host_test(self, ht_name):
73  """! Checks (by name) if host test object is registered already
74 
75  @param ht_name Host test unique name
76 
77  @return True if ht_name is registered (available), else False
78  """
79  return (ht_name in self.HOST_TESTSHOST_TESTS and
80  self.HOST_TESTSHOST_TESTS[ht_name] is not None)
81 
82  def table(self, verbose=False):
83  """! Prints list of registered host test classes (by name)
84  @Detail For devel & debug purposes
85  """
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'
91 
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
96  else:
97  src_path = 'mbed-host-tests'
98  pt.add_row([name, cls_str, src_path])
99  return pt.get_string()
100 
101  def register_from_path(self, path, verbose=False):
102  """ Enumerates and registers locally stored host tests
103  Host test are derived from mbed_host_tests.BaseHostTest classes
104  """
105  if path:
106  path = path.strip('"')
107  if verbose:
108  print("HOST: Inspecting '%s' for local host tests..." % path)
109  if exists(path) and isdir(path):
110  python_modules = [
111  f for f in listdir(path)
112  if isfile(join(path, f)) and f.endswith(".py")
113  ]
114  for module_file in python_modules:
115  self._add_module_to_registry_add_module_to_registry(path, module_file, verbose)
116 
117  def _add_module_to_registry(self, path, module_file, verbose):
118  module_name = module_file[:-3]
119  try:
120  mod = load_source(module_name, abspath(join(path, module_file)))
121  except Exception as e:
122  print(
123  "HOST: Error! While loading local host test module '%s'"
124  % join(path, module_file)
125  )
126  print("HOST: %s" % str(e))
127  return
128  if verbose:
129  print("HOST: Loading module '%s': %s" % (module_file, str(mod)))
130 
131  for name, obj in getmembers(mod):
132  if (
133  isclass(obj) and
134  issubclass(obj, BaseHostTest) and
135  str(obj) != str(BaseHostTest)
136  ):
137  if obj.name:
138  host_test_name = obj.name
139  else:
140  host_test_name = module_name
141  host_test_cls = obj
142  host_test_cls.script_location = join(path, module_file)
143  if verbose:
144  print(
145  "HOST: Found host test implementation: %s -|> %s"
146  % (str(obj), str(BaseHostTest))
147  )
148  print(
149  "HOST: Registering '%s' as '%s'"
150  % (str(host_test_cls), host_test_name)
151  )
152  self.register_host_testregister_host_test(
153  host_test_name, host_test_cls()
154  )
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 _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.