summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/controllers/testtargetloader.py
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oeqa/controllers/testtargetloader.py')
-rw-r--r--meta/lib/oeqa/controllers/testtargetloader.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/meta/lib/oeqa/controllers/testtargetloader.py b/meta/lib/oeqa/controllers/testtargetloader.py
new file mode 100644
index 0000000000..019bbfd840
--- /dev/null
+++ b/meta/lib/oeqa/controllers/testtargetloader.py
@@ -0,0 +1,69 @@
1import types
2import bb
3
4# This class is responsible for loading a test target controller
5class TestTargetLoader:
6
7 # Search oeqa.controllers module directory for and return a controller
8 # corresponding to the given target name.
9 # AttributeError raised if not found.
10 # ImportError raised if a provided module can not be imported.
11 def get_controller_module(self, target, bbpath):
12 controllerslist = self.get_controller_modulenames(bbpath)
13 bb.note("Available controller modules: %s" % str(controllerslist))
14 controller = self.load_controller_from_name(target, controllerslist)
15 return controller
16
17 # Return a list of all python modules in lib/oeqa/controllers for each
18 # layer in bbpath
19 def get_controller_modulenames(self, bbpath):
20
21 controllerslist = []
22
23 def add_controller_list(path):
24 if not os.path.exists(os.path.join(path, '__init__.py')):
25 bb.fatal('Controllers directory %s exists but is missing __init__.py' % path)
26 files = sorted([f for f in os.listdir(path) if f.endswith('.py') and not f.startswith('_')])
27 for f in files:
28 module = 'oeqa.controllers.' + f[:-3]
29 if module not in controllerslist:
30 controllerslist.append(module)
31 else:
32 bb.warn("Duplicate controller module found for %s, only one added. Layers should create unique controller module names" % module)
33
34 for p in bbpath:
35 controllerpath = os.path.join(p, 'lib', 'oeqa', 'controllers')
36 bb.debug(2, 'Searching for target controllers in %s' % controllerpath)
37 if os.path.exists(controllerpath):
38 add_controller_list(controllerpath)
39 return controllerslist
40
41 # Search for and return a controller from given target name and
42 # set of module names.
43 # Raise AttributeError if not found.
44 # Raise ImportError if a provided module can not be imported
45 def load_controller_from_name(self, target, modulenames):
46 for name in modulenames:
47 obj = self.load_controller_from_module(target, name)
48 if obj:
49 return obj
50 raise AttributeError("Unable to load {0} from available modules: {1}".format(target, str(modulenames)))
51
52 # Search for and return a controller or None from given module name
53 def load_controller_from_module(self, target, modulename):
54 obj = None
55 # import module, allowing it to raise import exception
56 module = __import__(modulename, globals(), locals(), [target])
57 # look for target class in the module, catching any exceptions as it
58 # is valid that a module may not have the target class.
59 try:
60 obj = getattr(module, target)
61 if obj:
62 from oeqa.targetcontrol import BaseTarget
63 if (not isinstance(obj, (type, types.ClassType))):
64 bb.warn("Target {0} found, but not of type Class".format(target))
65 if( not issubclass(obj, BaseTarget)):
66 bb.warn("Target {0} found, but subclass is not BaseTarget".format(target))
67 except:
68 obj = None
69 return obj