summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorEd Bartosh <ed.bartosh@linux.intel.com>2017-02-15 14:16:38 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2017-03-04 23:18:17 +0000
commitec7b604b1e45b6d9d20e9cd8731002b488b20dd3 (patch)
tree3b1fb256a72663b5707462d5c194cf7ae24dbc8c /scripts
parentf8a4bd99503f5b63bbc748fedd4b7f32afbf0cb7 (diff)
downloadpoky-ec7b604b1e45b6d9d20e9cd8731002b488b20dd3.tar.gz
wic: use PluginMgr directly
Instead of making a singleton object of PluginMgr class it's simpler to use PluginMgr class directly as any class is a singleton. (From OE-Core rev: cbe7dbd31f2292416d8e801e142679c69d9a44bc) Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/lib/wic/engine.py6
-rw-r--r--scripts/lib/wic/help.py4
-rw-r--r--scripts/lib/wic/partition.py6
-rw-r--r--scripts/lib/wic/plugin.py83
-rw-r--r--scripts/lib/wic/plugins/imager/direct.py4
5 files changed, 47 insertions, 56 deletions
diff --git a/scripts/lib/wic/engine.py b/scripts/lib/wic/engine.py
index c29924b781..98e7b9526e 100644
--- a/scripts/lib/wic/engine.py
+++ b/scripts/lib/wic/engine.py
@@ -32,7 +32,7 @@ import logging
32import os 32import os
33 33
34from wic import WicError 34from wic import WicError
35from wic.plugin import pluginmgr 35from wic.plugin import PluginMgr
36from wic.utils.misc import get_bitbake_var 36from wic.utils.misc import get_bitbake_var
37 37
38logger = logging.getLogger('wic') 38logger = logging.getLogger('wic')
@@ -139,7 +139,7 @@ def list_source_plugins():
139 """ 139 """
140 List the available source plugins i.e. plugins available for --source. 140 List the available source plugins i.e. plugins available for --source.
141 """ 141 """
142 plugins = pluginmgr.get_source_plugins() 142 plugins = PluginMgr.get_source_plugins()
143 143
144 for plugin in plugins: 144 for plugin in plugins:
145 print(" %s" % plugin) 145 print(" %s" % plugin)
@@ -185,7 +185,7 @@ def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
185 os.makedirs(options.outdir) 185 os.makedirs(options.outdir)
186 186
187 pname = 'direct' 187 pname = 'direct'
188 plugin_class = pluginmgr.get_plugins('imager').get(pname) 188 plugin_class = PluginMgr.get_plugins('imager').get(pname)
189 if not plugin_class: 189 if not plugin_class:
190 raise WicError('Unknown plugin: %s' % pname) 190 raise WicError('Unknown plugin: %s' % pname)
191 191
diff --git a/scripts/lib/wic/help.py b/scripts/lib/wic/help.py
index 47e3d1666b..4aba12de75 100644
--- a/scripts/lib/wic/help.py
+++ b/scripts/lib/wic/help.py
@@ -28,7 +28,7 @@
28import subprocess 28import subprocess
29import logging 29import logging
30 30
31from wic.plugin import pluginmgr, PLUGIN_TYPES 31from wic.plugin import PluginMgr, PLUGIN_TYPES
32 32
33logger = logging.getLogger('wic') 33logger = logging.getLogger('wic')
34 34
@@ -68,7 +68,7 @@ def get_wic_plugins_help():
68 result = wic_plugins_help 68 result = wic_plugins_help
69 for plugin_type in PLUGIN_TYPES: 69 for plugin_type in PLUGIN_TYPES:
70 result += '\n\n%s PLUGINS\n\n' % plugin_type.upper() 70 result += '\n\n%s PLUGINS\n\n' % plugin_type.upper()
71 for name, plugin in pluginmgr.get_plugins(plugin_type).items(): 71 for name, plugin in PluginMgr.get_plugins(plugin_type).items():
72 result += "\n %s plugin:\n" % name 72 result += "\n %s plugin:\n" % name
73 if plugin.__doc__: 73 if plugin.__doc__:
74 result += plugin.__doc__ 74 result += plugin.__doc__
diff --git a/scripts/lib/wic/partition.py b/scripts/lib/wic/partition.py
index 8e531e4dcc..f8d4274bcd 100644
--- a/scripts/lib/wic/partition.py
+++ b/scripts/lib/wic/partition.py
@@ -30,7 +30,7 @@ import tempfile
30 30
31from wic import WicError 31from wic import WicError
32from wic.utils.misc import exec_cmd, exec_native_cmd, get_bitbake_var 32from wic.utils.misc import exec_cmd, exec_native_cmd, get_bitbake_var
33from wic.plugin import pluginmgr 33from wic.plugin import PluginMgr
34 34
35logger = logging.getLogger('wic') 35logger = logging.getLogger('wic')
36 36
@@ -154,7 +154,7 @@ class Partition():
154 break 154 break
155 return 155 return
156 156
157 plugins = pluginmgr.get_source_plugins() 157 plugins = PluginMgr.get_source_plugins()
158 158
159 if self.source not in plugins: 159 if self.source not in plugins:
160 raise WicError("The '%s' --source specified for %s doesn't exist.\n\t" 160 raise WicError("The '%s' --source specified for %s doesn't exist.\n\t"
@@ -176,7 +176,7 @@ class Partition():
176 "do_configure_partition": None 176 "do_configure_partition": None
177 } 177 }
178 178
179 methods = pluginmgr.get_source_plugin_methods(self.source, 179 methods = PluginMgr.get_source_plugin_methods(self.source,
180 partition_methods) 180 partition_methods)
181 methods["do_configure_partition"](self, srcparams_dict, creator, 181 methods["do_configure_partition"](self, srcparams_dict, creator,
182 cr_workdir, oe_builddir, bootimg_dir, 182 cr_workdir, oe_builddir, bootimg_dir,
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
22from wic import pluginbase, WicError 22from wic import pluginbase, WicError
23from wic.utils.misc import get_bitbake_var 23from wic.utils.misc import get_bitbake_var
24 24
25__ALL__ = ['PluginMgr', 'pluginmgr']
26
27PLUGIN_TYPES = ["imager", "source"] 25PLUGIN_TYPES = ["imager", "source"]
28 26
29PLUGIN_DIR = "/lib/wic/plugins" # relative to scripts 27PLUGIN_DIR = "/lib/wic/plugins" # relative to scripts
@@ -31,32 +29,22 @@ SCRIPTS_PLUGIN_DIR = "scripts" + PLUGIN_DIR
31 29
32logger = logging.getLogger('wic') 30logger = logging.getLogger('wic')
33 31
34class PluginMgr(): 32class 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
151pluginmgr = PluginMgr()
diff --git a/scripts/lib/wic/plugins/imager/direct.py b/scripts/lib/wic/plugins/imager/direct.py
index 6d1ed8c205..b93273e877 100644
--- a/scripts/lib/wic/plugins/imager/direct.py
+++ b/scripts/lib/wic/plugins/imager/direct.py
@@ -35,7 +35,7 @@ from time import strftime
35from wic import WicError 35from wic import WicError
36from wic.filemap import sparse_copy 36from wic.filemap import sparse_copy
37from wic.ksparser import KickStart, KickStartError 37from wic.ksparser import KickStart, KickStartError
38from wic.plugin import pluginmgr 38from wic.plugin import PluginMgr
39from wic.pluginbase import ImagerPlugin 39from wic.pluginbase import ImagerPlugin
40from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd 40from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
41 41
@@ -198,7 +198,7 @@ class DirectPlugin(ImagerPlugin):
198 disk_name = self.parts[0].disk 198 disk_name = self.parts[0].disk
199 if source_plugin: 199 if source_plugin:
200 name = "do_install_disk" 200 name = "do_install_disk"
201 methods = pluginmgr.get_source_plugin_methods(source_plugin, 201 methods = PluginMgr.get_source_plugin_methods(source_plugin,
202 {name: None}) 202 {name: None})
203 methods["do_install_disk"](self._image, disk_name, self, self.workdir, 203 methods["do_install_disk"](self._image, disk_name, self, self.workdir,
204 self.oe_builddir, self.bootimg_dir, 204 self.oe_builddir, self.bootimg_dir,