diff options
author | Adrian Dudau <adrian.dudau@enea.com> | 2013-12-12 13:38:32 +0100 |
---|---|---|
committer | Adrian Dudau <adrian.dudau@enea.com> | 2013-12-12 13:50:20 +0100 |
commit | e2e6f6fe07049f33cb6348780fa975162752e421 (patch) | |
tree | b1813295411235d1297a0ed642b1346b24fdfb12 /scripts/lib/mic/plugin.py | |
download | poky-e2e6f6fe07049f33cb6348780fa975162752e421.tar.gz |
initial commit of Enea Linux 3.1
Migrated from the internal git server on the dora-enea branch
Signed-off-by: Adrian Dudau <adrian.dudau@enea.com>
Diffstat (limited to 'scripts/lib/mic/plugin.py')
-rw-r--r-- | scripts/lib/mic/plugin.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/scripts/lib/mic/plugin.py b/scripts/lib/mic/plugin.py new file mode 100644 index 0000000000..7c296e9765 --- /dev/null +++ b/scripts/lib/mic/plugin.py | |||
@@ -0,0 +1,102 @@ | |||
1 | #!/usr/bin/python -tt | ||
2 | # | ||
3 | # Copyright (c) 2011 Intel, Inc. | ||
4 | # | ||
5 | # This program is free software; you can redistribute it and/or modify it | ||
6 | # under the terms of the GNU General Public License as published by the Free | ||
7 | # Software Foundation; version 2 of the License | ||
8 | # | ||
9 | # This program is distributed in the hope that it will be useful, but | ||
10 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
11 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
12 | # for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License along | ||
15 | # with this program; if not, write to the Free Software Foundation, Inc., 59 | ||
16 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
17 | |||
18 | import os, sys | ||
19 | |||
20 | from mic import msger | ||
21 | from mic import pluginbase | ||
22 | from mic.conf import configmgr | ||
23 | from mic.utils import errors | ||
24 | |||
25 | |||
26 | __ALL__ = ['PluginMgr', 'pluginmgr'] | ||
27 | |||
28 | PLUGIN_TYPES = ["imager", "backend"] # TODO "hook" | ||
29 | |||
30 | |||
31 | class PluginMgr(object): | ||
32 | plugin_dirs = {} | ||
33 | |||
34 | # make the manager class as singleton | ||
35 | _instance = None | ||
36 | def __new__(cls, *args, **kwargs): | ||
37 | if not cls._instance: | ||
38 | cls._instance = super(PluginMgr, cls).__new__(cls, *args, **kwargs) | ||
39 | |||
40 | return cls._instance | ||
41 | |||
42 | def __init__(self): | ||
43 | mic_path = os.path.dirname(__file__) | ||
44 | eos = mic_path.find('scripts') + len('scripts') | ||
45 | scripts_path = mic_path[:eos] | ||
46 | |||
47 | self.plugin_dir = scripts_path + "/lib/mic/plugins" | ||
48 | |||
49 | def append_dirs(self, dirs): | ||
50 | for path in dirs: | ||
51 | self._add_plugindir(path) | ||
52 | |||
53 | # load all the plugins AGAIN | ||
54 | self._load_all() | ||
55 | |||
56 | def _add_plugindir(self, path): | ||
57 | path = os.path.abspath(os.path.expanduser(path)) | ||
58 | |||
59 | if not os.path.isdir(path): | ||
60 | msger.warning("Plugin dir is not a directory or does not exist: %s"\ | ||
61 | % path) | ||
62 | return | ||
63 | |||
64 | if path not in self.plugin_dirs: | ||
65 | self.plugin_dirs[path] = False | ||
66 | # the value True/False means "loaded" | ||
67 | |||
68 | def _load_all(self): | ||
69 | for (pdir, loaded) in self.plugin_dirs.iteritems(): | ||
70 | if loaded: continue | ||
71 | |||
72 | sys.path.insert(0, pdir) | ||
73 | for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]: | ||
74 | if mod and mod != '__init__': | ||
75 | if mod in sys.modules: | ||
76 | #self.plugin_dirs[pdir] = True | ||
77 | msger.warning("Module %s already exists, skip" % mod) | ||
78 | else: | ||
79 | try: | ||
80 | pymod = __import__(mod) | ||
81 | self.plugin_dirs[pdir] = True | ||
82 | msger.debug("Plugin module %s:%s imported"\ | ||
83 | % (mod, pymod.__file__)) | ||
84 | except ImportError, err: | ||
85 | msg = 'Failed to load plugin %s/%s: %s' \ | ||
86 | % (os.path.basename(pdir), mod, err) | ||
87 | msger.warning(msg) | ||
88 | |||
89 | del(sys.path[0]) | ||
90 | |||
91 | def get_plugins(self, ptype): | ||
92 | """ the return value is dict of name:class pairs """ | ||
93 | |||
94 | if ptype not in PLUGIN_TYPES: | ||
95 | raise errors.CreatorError('%s is not valid plugin type' % ptype) | ||
96 | |||
97 | self._add_plugindir(os.path.join(self.plugin_dir, ptype)) | ||
98 | self._load_all() | ||
99 | |||
100 | return pluginbase.get_plugins(ptype) | ||
101 | |||
102 | pluginmgr = PluginMgr() | ||