diff options
| author | Ed Bartosh <ed.bartosh@linux.intel.com> | 2017-02-15 21:24:38 +0200 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2017-03-04 23:18:17 +0000 |
| commit | 0c0ed61992c248a2427d0eb9905939e05c9de5b0 (patch) | |
| tree | 9d67bab2e0e5ce94949696f2e14fc67526f302c2 /scripts/lib/wic/pluginbase.py | |
| parent | 93b3eb37ff623f84b5b9c843353d3e2b406687ad (diff) | |
| download | poky-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.py | 47 |
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 | ||
| 20 | import os | ||
| 20 | import logging | 21 | import logging |
| 21 | 22 | ||
| 22 | from collections import defaultdict | 23 | from collections import defaultdict |
| 24 | from importlib.machinery import SourceFileLoader | ||
| 23 | 25 | ||
| 24 | from wic import WicError | 26 | from wic import WicError |
| 27 | from wic.utils.misc import get_bitbake_var | ||
| 28 | |||
| 29 | PLUGIN_TYPES = ["imager", "source"] | ||
| 30 | |||
| 31 | SCRIPTS_PLUGIN_DIR = "scripts/lib/wic/plugins" | ||
| 25 | 32 | ||
| 26 | logger = logging.getLogger('wic') | 33 | logger = logging.getLogger('wic') |
| 27 | 34 | ||
| 35 | class 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 | |||
| 28 | class PluginMeta(type): | 71 | class 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 | ||
| 100 | def get_plugins(typen): | ||
| 101 | return PluginMeta.plugins.get(typen) | ||
