diff options
Diffstat (limited to 'scripts/lib/wic/plugin.py')
-rw-r--r-- | scripts/lib/wic/plugin.py | 83 |
1 files changed, 37 insertions, 46 deletions
diff --git a/scripts/lib/wic/plugin.py b/scripts/lib/wic/plugin.py index ac55ecf545..7632c3acd3 100644 --- a/scripts/lib/wic/plugin.py +++ b/scripts/lib/wic/plugin.py | |||
@@ -22,8 +22,6 @@ import logging | |||
22 | from wic import pluginbase, WicError | 22 | from wic import pluginbase, WicError |
23 | from wic.utils.misc import get_bitbake_var | 23 | from wic.utils.misc import get_bitbake_var |
24 | 24 | ||
25 | __ALL__ = ['PluginMgr', 'pluginmgr'] | ||
26 | |||
27 | PLUGIN_TYPES = ["imager", "source"] | 25 | PLUGIN_TYPES = ["imager", "source"] |
28 | 26 | ||
29 | PLUGIN_DIR = "/lib/wic/plugins" # relative to scripts | 27 | PLUGIN_DIR = "/lib/wic/plugins" # relative to scripts |
@@ -31,32 +29,22 @@ SCRIPTS_PLUGIN_DIR = "scripts" + PLUGIN_DIR | |||
31 | 29 | ||
32 | logger = logging.getLogger('wic') | 30 | logger = logging.getLogger('wic') |
33 | 31 | ||
34 | class PluginMgr(): | 32 | class PluginMgr: |
35 | plugin_dirs = {} | 33 | plugin_dirs = {} |
36 | 34 | wic_path = os.path.dirname(__file__) | |
37 | # make the manager class as singleton | 35 | eos = wic_path.rfind('scripts') + len('scripts') |
38 | _instance = None | 36 | scripts_path = wic_path[:eos] |
39 | def __new__(cls, *args, **kwargs): | 37 | plugin_dir = scripts_path + PLUGIN_DIR |
40 | if not cls._instance: | 38 | layers_path = None |
41 | cls._instance = super(PluginMgr, cls).__new__(cls, *args, **kwargs) | 39 | |
42 | 40 | @classmethod | |
43 | return cls._instance | 41 | def _build_plugin_dir_list(cls, plugin_dir, ptype): |
44 | 42 | if cls.layers_path is None: | |
45 | def __init__(self): | 43 | cls.layers_path = get_bitbake_var("BBLAYERS") |
46 | wic_path = os.path.dirname(__file__) | ||
47 | eos = wic_path.rfind('scripts') + len('scripts') | ||
48 | scripts_path = wic_path[:eos] | ||
49 | self.scripts_path = scripts_path | ||
50 | self.plugin_dir = scripts_path + PLUGIN_DIR | ||
51 | self.layers_path = None | ||
52 | |||
53 | def _build_plugin_dir_list(self, plugin_dir, ptype): | ||
54 | if self.layers_path is None: | ||
55 | self.layers_path = get_bitbake_var("BBLAYERS") | ||
56 | layer_dirs = [] | 44 | layer_dirs = [] |
57 | 45 | ||
58 | if self.layers_path is not None: | 46 | if cls.layers_path is not None: |
59 | for layer_path in self.layers_path.split(): | 47 | for layer_path in cls.layers_path.split(): |
60 | path = os.path.join(layer_path, SCRIPTS_PLUGIN_DIR, ptype) | 48 | path = os.path.join(layer_path, SCRIPTS_PLUGIN_DIR, ptype) |
61 | layer_dirs.append(path) | 49 | layer_dirs.append(path) |
62 | 50 | ||
@@ -65,25 +53,28 @@ class PluginMgr(): | |||
65 | 53 | ||
66 | return layer_dirs | 54 | return layer_dirs |
67 | 55 | ||
68 | def append_dirs(self, dirs): | 56 | @classmethod |
57 | def append_dirs(cls, dirs): | ||
69 | for path in dirs: | 58 | for path in dirs: |
70 | self._add_plugindir(path) | 59 | cls._add_plugindir(path) |
71 | 60 | ||
72 | # load all the plugins AGAIN | 61 | # load all the plugins AGAIN |
73 | self._load_all() | 62 | cls._load_all() |
74 | 63 | ||
75 | def _add_plugindir(self, path): | 64 | @classmethod |
65 | def _add_plugindir(cls, path): | ||
76 | path = os.path.abspath(os.path.expanduser(path)) | 66 | path = os.path.abspath(os.path.expanduser(path)) |
77 | 67 | ||
78 | if not os.path.isdir(path): | 68 | if not os.path.isdir(path): |
79 | return | 69 | return |
80 | 70 | ||
81 | if path not in self.plugin_dirs: | 71 | if path not in cls.plugin_dirs: |
82 | self.plugin_dirs[path] = False | 72 | cls.plugin_dirs[path] = False |
83 | # the value True/False means "loaded" | 73 | # the value True/False means "loaded" |
84 | 74 | ||
85 | def _load_all(self): | 75 | @classmethod |
86 | for (pdir, loaded) in self.plugin_dirs.items(): | 76 | def _load_all(cls): |
77 | for (pdir, loaded) in cls.plugin_dirs.items(): | ||
87 | if loaded: | 78 | if loaded: |
88 | continue | 79 | continue |
89 | 80 | ||
@@ -91,12 +82,11 @@ class PluginMgr(): | |||
91 | for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]: | 82 | for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]: |
92 | if mod and mod != '__init__': | 83 | if mod and mod != '__init__': |
93 | if mod in sys.modules: | 84 | if mod in sys.modules: |
94 | #self.plugin_dirs[pdir] = True | ||
95 | logger.warning("Module %s already exists, skip", mod) | 85 | logger.warning("Module %s already exists, skip", mod) |
96 | else: | 86 | else: |
97 | try: | 87 | try: |
98 | pymod = __import__(mod) | 88 | pymod = __import__(mod) |
99 | self.plugin_dirs[pdir] = True | 89 | cls.plugin_dirs[pdir] = True |
100 | logger.debug("Plugin module %s:%s imported", | 90 | logger.debug("Plugin module %s:%s imported", |
101 | mod, pymod.__file__) | 91 | mod, pymod.__file__) |
102 | except ImportError as err: | 92 | except ImportError as err: |
@@ -105,30 +95,33 @@ class PluginMgr(): | |||
105 | 95 | ||
106 | del sys.path[0] | 96 | del sys.path[0] |
107 | 97 | ||
108 | def get_plugins(self, ptype): | 98 | @classmethod |
99 | def get_plugins(cls, ptype): | ||
109 | """ the return value is dict of name:class pairs """ | 100 | """ the return value is dict of name:class pairs """ |
110 | 101 | ||
111 | if ptype not in PLUGIN_TYPES: | 102 | if ptype not in PLUGIN_TYPES: |
112 | raise WicError('%s is not valid plugin type' % ptype) | 103 | raise WicError('%s is not valid plugin type' % ptype) |
113 | 104 | ||
114 | plugins_dir = self._build_plugin_dir_list(self.plugin_dir, ptype) | 105 | plugins_dir = cls._build_plugin_dir_list(cls.plugin_dir, ptype) |
115 | 106 | ||
116 | self.append_dirs(plugins_dir) | 107 | cls.append_dirs(plugins_dir) |
117 | 108 | ||
118 | return pluginbase.get_plugins(ptype) | 109 | return pluginbase.get_plugins(ptype) |
119 | 110 | ||
120 | def get_source_plugins(self): | 111 | @classmethod |
112 | def get_source_plugins(cls): | ||
121 | """ | 113 | """ |
122 | Return list of available source plugins. | 114 | Return list of available source plugins. |
123 | """ | 115 | """ |
124 | plugins_dir = self._build_plugin_dir_list(self.plugin_dir, 'source') | 116 | plugins_dir = cls._build_plugin_dir_list(cls.plugin_dir, 'source') |
125 | 117 | ||
126 | self.append_dirs(plugins_dir) | 118 | cls.append_dirs(plugins_dir) |
127 | 119 | ||
128 | return self.get_plugins('source') | 120 | return cls.get_plugins('source') |
129 | 121 | ||
130 | 122 | ||
131 | def get_source_plugin_methods(self, source_name, methods): | 123 | @classmethod |
124 | def get_source_plugin_methods(cls, source_name, methods): | ||
132 | """ | 125 | """ |
133 | The methods param is a dict with the method names to find. On | 126 | The methods param is a dict with the method names to find. On |
134 | return, the dict values will be filled in with pointers to the | 127 | return, the dict values will be filled in with pointers to the |
@@ -136,7 +129,7 @@ class PluginMgr(): | |||
136 | None is returned. | 129 | None is returned. |
137 | """ | 130 | """ |
138 | return_methods = None | 131 | return_methods = None |
139 | for _source_name, klass in self.get_plugins('source').items(): | 132 | for _source_name, klass in cls.get_plugins('source').items(): |
140 | if _source_name == source_name: | 133 | if _source_name == source_name: |
141 | for _method_name in methods: | 134 | for _method_name in methods: |
142 | if not hasattr(klass, _method_name): | 135 | if not hasattr(klass, _method_name): |
@@ -147,5 +140,3 @@ class PluginMgr(): | |||
147 | methods[_method_name] = func | 140 | methods[_method_name] = func |
148 | return_methods = methods | 141 | return_methods = methods |
149 | return return_methods | 142 | return return_methods |
150 | |||
151 | pluginmgr = PluginMgr() | ||