diff options
author | Sipke Vriend <sipke.vriend@xilinx.com> | 2014-01-30 16:25:54 +1000 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2014-02-02 22:37:41 +0000 |
commit | c99622e38b0a08dcdbe096a41732fef732bb1207 (patch) | |
tree | fb63682fdf7d8e3099cef4d066f241baeac2b720 /meta/lib/oeqa/controllers/testtargetloader.py | |
parent | 6e59874e886192f1600126eaa0363ffb0f423f21 (diff) | |
download | poky-c99622e38b0a08dcdbe096a41732fef732bb1207.tar.gz |
lib/oeqa: allow multiple layers to provide their own TEST_TARGET class
Use a python module "folder" rather than a single module within
layers to ensure multiple layers can define a TEST_TARGET class.
Current implementation using controllers.py module will only allow
a single layer to define test targets.
Add a controllers folder as well as a TestTargetLoader class whose
job is to load the given TEST_TARGET class from any number of
python modules within the oeqa/controllers/ directory of any
layer.
The only condition will be that layers will need to ensure
the TEST_TARGET class name they provide is unique otherwise there
is no guarantee which class is instantiated. a bb.warn is used
to alude to this if it happens.
(From OE-Core rev: 3f25705f4a986e06cbd397aaea52b841c1a1e054)
Signed-off-by: Sipke Vriend <sipke.vriend@xilinx.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib/oeqa/controllers/testtargetloader.py')
-rw-r--r-- | meta/lib/oeqa/controllers/testtargetloader.py | 69 |
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 @@ | |||
1 | import types | ||
2 | import bb | ||
3 | |||
4 | # This class is responsible for loading a test target controller | ||
5 | class 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 | ||