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