summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/pluginbase.py
diff options
context:
space:
mode:
authorEd Bartosh <ed.bartosh@linux.intel.com>2017-02-15 21:24:38 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2017-03-04 23:18:17 +0000
commit0c0ed61992c248a2427d0eb9905939e05c9de5b0 (patch)
tree9d67bab2e0e5ce94949696f2e14fc67526f302c2 /scripts/lib/wic/pluginbase.py
parent93b3eb37ff623f84b5b9c843353d3e2b406687ad (diff)
downloadpoky-0c0ed61992c248a2427d0eb9905939e05c9de5b0.tar.gz
wic: move PluginMgr class to pluginbase
As PluginMgr class contains only one method it's better to move it to pluginbase to have all plugin related APIs in one module. (From OE-Core rev: 244585b369ecc0019002ca51bf7f8fd506234462) Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/wic/pluginbase.py')
-rw-r--r--scripts/lib/wic/pluginbase.py47
1 files changed, 44 insertions, 3 deletions
diff --git a/scripts/lib/wic/pluginbase.py b/scripts/lib/wic/pluginbase.py
index 743170a5fa..93f0b663a3 100644
--- a/scripts/lib/wic/pluginbase.py
+++ b/scripts/lib/wic/pluginbase.py
@@ -15,16 +15,59 @@
15# with this program; if not, write to the Free Software Foundation, Inc., 59 15# with this program; if not, write to the Free Software Foundation, Inc., 59
16# Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 17
18__all__ = ['ImagerPlugin', 'SourcePlugin', 'get_plugins'] 18__all__ = ['ImagerPlugin', 'SourcePlugin']
19 19
20import os
20import logging 21import logging
21 22
22from collections import defaultdict 23from collections import defaultdict
24from importlib.machinery import SourceFileLoader
23 25
24from wic import WicError 26from wic import WicError
27from wic.utils.misc import get_bitbake_var
28
29PLUGIN_TYPES = ["imager", "source"]
30
31SCRIPTS_PLUGIN_DIR = "scripts/lib/wic/plugins"
25 32
26logger = logging.getLogger('wic') 33logger = logging.getLogger('wic')
27 34
35class PluginMgr:
36 _plugin_dirs = []
37 _plugins = {}
38
39 @classmethod
40 def get_plugins(cls, ptype):
41 """Get dictionary of <plugin_name>:<class> pairs."""
42 if ptype not in PLUGIN_TYPES:
43 raise WicError('%s is not valid plugin type' % ptype)
44
45 if ptype in cls._plugins:
46 return cls._plugins[ptype]
47
48 # collect plugin directories
49 if not cls._plugin_dirs:
50 cls._plugin_dirs = [os.path.join(os.path.dirname(__file__), 'plugins')]
51 layers = get_bitbake_var("BBLAYERS") or ''
52 for layer_path in layers.split():
53 path = os.path.join(layer_path, SCRIPTS_PLUGIN_DIR)
54 path = os.path.abspath(os.path.expanduser(path))
55 if path not in cls._plugin_dirs and os.path.isdir(path):
56 cls._plugin_dirs.insert(0, path)
57
58 # load plugins
59 for pdir in cls._plugin_dirs:
60 ppath = os.path.join(pdir, ptype)
61 if os.path.isdir(ppath):
62 for fname in os.listdir(ppath):
63 if fname.endswith('.py'):
64 mname = fname[:-3]
65 mpath = os.path.join(ppath, fname)
66 SourceFileLoader(mname, mpath).load_module()
67
68 cls._plugins[ptype] = PluginMeta.plugins.get(ptype)
69 return cls._plugins[ptype]
70
28class PluginMeta(type): 71class PluginMeta(type):
29 plugins = defaultdict(dict) 72 plugins = defaultdict(dict)
30 def __new__(cls, name, bases, attrs): 73 def __new__(cls, name, bases, attrs):
@@ -97,5 +140,3 @@ class SourcePlugin(metaclass=PluginMeta):
97 """ 140 """
98 logger.debug("SourcePlugin: do_prepare_partition: part: %s", part) 141 logger.debug("SourcePlugin: do_prepare_partition: part: %s", part)
99 142
100def get_plugins(typen):
101 return PluginMeta.plugins.get(typen)