mirror of
https://git.yoctoproject.org/poky
synced 2026-05-31 12:49:46 +00:00
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>
This commit is contained in:
committed by
Richard Purdie
parent
f8a4bd9950
commit
ec7b604b1e
@@ -32,7 +32,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from wic import WicError
|
from wic import WicError
|
||||||
from wic.plugin import pluginmgr
|
from wic.plugin import PluginMgr
|
||||||
from wic.utils.misc import get_bitbake_var
|
from wic.utils.misc import get_bitbake_var
|
||||||
|
|
||||||
logger = logging.getLogger('wic')
|
logger = logging.getLogger('wic')
|
||||||
@@ -139,7 +139,7 @@ def list_source_plugins():
|
|||||||
"""
|
"""
|
||||||
List the available source plugins i.e. plugins available for --source.
|
List the available source plugins i.e. plugins available for --source.
|
||||||
"""
|
"""
|
||||||
plugins = pluginmgr.get_source_plugins()
|
plugins = PluginMgr.get_source_plugins()
|
||||||
|
|
||||||
for plugin in plugins:
|
for plugin in plugins:
|
||||||
print(" %s" % plugin)
|
print(" %s" % plugin)
|
||||||
@@ -185,7 +185,7 @@ def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
|
|||||||
os.makedirs(options.outdir)
|
os.makedirs(options.outdir)
|
||||||
|
|
||||||
pname = 'direct'
|
pname = 'direct'
|
||||||
plugin_class = pluginmgr.get_plugins('imager').get(pname)
|
plugin_class = PluginMgr.get_plugins('imager').get(pname)
|
||||||
if not plugin_class:
|
if not plugin_class:
|
||||||
raise WicError('Unknown plugin: %s' % pname)
|
raise WicError('Unknown plugin: %s' % pname)
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from wic.plugin import pluginmgr, PLUGIN_TYPES
|
from wic.plugin import PluginMgr, PLUGIN_TYPES
|
||||||
|
|
||||||
logger = logging.getLogger('wic')
|
logger = logging.getLogger('wic')
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ def get_wic_plugins_help():
|
|||||||
result = wic_plugins_help
|
result = wic_plugins_help
|
||||||
for plugin_type in PLUGIN_TYPES:
|
for plugin_type in PLUGIN_TYPES:
|
||||||
result += '\n\n%s PLUGINS\n\n' % plugin_type.upper()
|
result += '\n\n%s PLUGINS\n\n' % plugin_type.upper()
|
||||||
for name, plugin in pluginmgr.get_plugins(plugin_type).items():
|
for name, plugin in PluginMgr.get_plugins(plugin_type).items():
|
||||||
result += "\n %s plugin:\n" % name
|
result += "\n %s plugin:\n" % name
|
||||||
if plugin.__doc__:
|
if plugin.__doc__:
|
||||||
result += plugin.__doc__
|
result += plugin.__doc__
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import tempfile
|
|||||||
|
|
||||||
from wic import WicError
|
from wic import WicError
|
||||||
from wic.utils.misc import exec_cmd, exec_native_cmd, get_bitbake_var
|
from wic.utils.misc import exec_cmd, exec_native_cmd, get_bitbake_var
|
||||||
from wic.plugin import pluginmgr
|
from wic.plugin import PluginMgr
|
||||||
|
|
||||||
logger = logging.getLogger('wic')
|
logger = logging.getLogger('wic')
|
||||||
|
|
||||||
@@ -154,7 +154,7 @@ class Partition():
|
|||||||
break
|
break
|
||||||
return
|
return
|
||||||
|
|
||||||
plugins = pluginmgr.get_source_plugins()
|
plugins = PluginMgr.get_source_plugins()
|
||||||
|
|
||||||
if self.source not in plugins:
|
if self.source not in plugins:
|
||||||
raise WicError("The '%s' --source specified for %s doesn't exist.\n\t"
|
raise WicError("The '%s' --source specified for %s doesn't exist.\n\t"
|
||||||
@@ -176,7 +176,7 @@ class Partition():
|
|||||||
"do_configure_partition": None
|
"do_configure_partition": None
|
||||||
}
|
}
|
||||||
|
|
||||||
methods = pluginmgr.get_source_plugin_methods(self.source,
|
methods = PluginMgr.get_source_plugin_methods(self.source,
|
||||||
partition_methods)
|
partition_methods)
|
||||||
methods["do_configure_partition"](self, srcparams_dict, creator,
|
methods["do_configure_partition"](self, srcparams_dict, creator,
|
||||||
cr_workdir, oe_builddir, bootimg_dir,
|
cr_workdir, oe_builddir, bootimg_dir,
|
||||||
|
|||||||
+36
-45
@@ -22,8 +22,6 @@ import logging
|
|||||||
from wic import pluginbase, WicError
|
from wic import pluginbase, WicError
|
||||||
from wic.utils.misc import get_bitbake_var
|
from wic.utils.misc import get_bitbake_var
|
||||||
|
|
||||||
__ALL__ = ['PluginMgr', 'pluginmgr']
|
|
||||||
|
|
||||||
PLUGIN_TYPES = ["imager", "source"]
|
PLUGIN_TYPES = ["imager", "source"]
|
||||||
|
|
||||||
PLUGIN_DIR = "/lib/wic/plugins" # relative to scripts
|
PLUGIN_DIR = "/lib/wic/plugins" # relative to scripts
|
||||||
@@ -31,32 +29,22 @@ SCRIPTS_PLUGIN_DIR = "scripts" + PLUGIN_DIR
|
|||||||
|
|
||||||
logger = logging.getLogger('wic')
|
logger = logging.getLogger('wic')
|
||||||
|
|
||||||
class PluginMgr():
|
class PluginMgr:
|
||||||
plugin_dirs = {}
|
plugin_dirs = {}
|
||||||
|
wic_path = os.path.dirname(__file__)
|
||||||
|
eos = wic_path.rfind('scripts') + len('scripts')
|
||||||
|
scripts_path = wic_path[:eos]
|
||||||
|
plugin_dir = scripts_path + PLUGIN_DIR
|
||||||
|
layers_path = None
|
||||||
|
|
||||||
# make the manager class as singleton
|
@classmethod
|
||||||
_instance = None
|
def _build_plugin_dir_list(cls, plugin_dir, ptype):
|
||||||
def __new__(cls, *args, **kwargs):
|
if cls.layers_path is None:
|
||||||
if not cls._instance:
|
cls.layers_path = get_bitbake_var("BBLAYERS")
|
||||||
cls._instance = super(PluginMgr, cls).__new__(cls, *args, **kwargs)
|
|
||||||
|
|
||||||
return cls._instance
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
wic_path = os.path.dirname(__file__)
|
|
||||||
eos = wic_path.rfind('scripts') + len('scripts')
|
|
||||||
scripts_path = wic_path[:eos]
|
|
||||||
self.scripts_path = scripts_path
|
|
||||||
self.plugin_dir = scripts_path + PLUGIN_DIR
|
|
||||||
self.layers_path = None
|
|
||||||
|
|
||||||
def _build_plugin_dir_list(self, plugin_dir, ptype):
|
|
||||||
if self.layers_path is None:
|
|
||||||
self.layers_path = get_bitbake_var("BBLAYERS")
|
|
||||||
layer_dirs = []
|
layer_dirs = []
|
||||||
|
|
||||||
if self.layers_path is not None:
|
if cls.layers_path is not None:
|
||||||
for layer_path in self.layers_path.split():
|
for layer_path in cls.layers_path.split():
|
||||||
path = os.path.join(layer_path, SCRIPTS_PLUGIN_DIR, ptype)
|
path = os.path.join(layer_path, SCRIPTS_PLUGIN_DIR, ptype)
|
||||||
layer_dirs.append(path)
|
layer_dirs.append(path)
|
||||||
|
|
||||||
@@ -65,25 +53,28 @@ class PluginMgr():
|
|||||||
|
|
||||||
return layer_dirs
|
return layer_dirs
|
||||||
|
|
||||||
def append_dirs(self, dirs):
|
@classmethod
|
||||||
|
def append_dirs(cls, dirs):
|
||||||
for path in dirs:
|
for path in dirs:
|
||||||
self._add_plugindir(path)
|
cls._add_plugindir(path)
|
||||||
|
|
||||||
# load all the plugins AGAIN
|
# load all the plugins AGAIN
|
||||||
self._load_all()
|
cls._load_all()
|
||||||
|
|
||||||
def _add_plugindir(self, path):
|
@classmethod
|
||||||
|
def _add_plugindir(cls, path):
|
||||||
path = os.path.abspath(os.path.expanduser(path))
|
path = os.path.abspath(os.path.expanduser(path))
|
||||||
|
|
||||||
if not os.path.isdir(path):
|
if not os.path.isdir(path):
|
||||||
return
|
return
|
||||||
|
|
||||||
if path not in self.plugin_dirs:
|
if path not in cls.plugin_dirs:
|
||||||
self.plugin_dirs[path] = False
|
cls.plugin_dirs[path] = False
|
||||||
# the value True/False means "loaded"
|
# the value True/False means "loaded"
|
||||||
|
|
||||||
def _load_all(self):
|
@classmethod
|
||||||
for (pdir, loaded) in self.plugin_dirs.items():
|
def _load_all(cls):
|
||||||
|
for (pdir, loaded) in cls.plugin_dirs.items():
|
||||||
if loaded:
|
if loaded:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -91,12 +82,11 @@ class PluginMgr():
|
|||||||
for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]:
|
for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]:
|
||||||
if mod and mod != '__init__':
|
if mod and mod != '__init__':
|
||||||
if mod in sys.modules:
|
if mod in sys.modules:
|
||||||
#self.plugin_dirs[pdir] = True
|
|
||||||
logger.warning("Module %s already exists, skip", mod)
|
logger.warning("Module %s already exists, skip", mod)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
pymod = __import__(mod)
|
pymod = __import__(mod)
|
||||||
self.plugin_dirs[pdir] = True
|
cls.plugin_dirs[pdir] = True
|
||||||
logger.debug("Plugin module %s:%s imported",
|
logger.debug("Plugin module %s:%s imported",
|
||||||
mod, pymod.__file__)
|
mod, pymod.__file__)
|
||||||
except ImportError as err:
|
except ImportError as err:
|
||||||
@@ -105,30 +95,33 @@ class PluginMgr():
|
|||||||
|
|
||||||
del sys.path[0]
|
del sys.path[0]
|
||||||
|
|
||||||
def get_plugins(self, ptype):
|
@classmethod
|
||||||
|
def get_plugins(cls, ptype):
|
||||||
""" the return value is dict of name:class pairs """
|
""" the return value is dict of name:class pairs """
|
||||||
|
|
||||||
if ptype not in PLUGIN_TYPES:
|
if ptype not in PLUGIN_TYPES:
|
||||||
raise WicError('%s is not valid plugin type' % ptype)
|
raise WicError('%s is not valid plugin type' % ptype)
|
||||||
|
|
||||||
plugins_dir = self._build_plugin_dir_list(self.plugin_dir, ptype)
|
plugins_dir = cls._build_plugin_dir_list(cls.plugin_dir, ptype)
|
||||||
|
|
||||||
self.append_dirs(plugins_dir)
|
cls.append_dirs(plugins_dir)
|
||||||
|
|
||||||
return pluginbase.get_plugins(ptype)
|
return pluginbase.get_plugins(ptype)
|
||||||
|
|
||||||
def get_source_plugins(self):
|
@classmethod
|
||||||
|
def get_source_plugins(cls):
|
||||||
"""
|
"""
|
||||||
Return list of available source plugins.
|
Return list of available source plugins.
|
||||||
"""
|
"""
|
||||||
plugins_dir = self._build_plugin_dir_list(self.plugin_dir, 'source')
|
plugins_dir = cls._build_plugin_dir_list(cls.plugin_dir, 'source')
|
||||||
|
|
||||||
self.append_dirs(plugins_dir)
|
cls.append_dirs(plugins_dir)
|
||||||
|
|
||||||
return self.get_plugins('source')
|
return cls.get_plugins('source')
|
||||||
|
|
||||||
|
|
||||||
def get_source_plugin_methods(self, source_name, methods):
|
@classmethod
|
||||||
|
def get_source_plugin_methods(cls, source_name, methods):
|
||||||
"""
|
"""
|
||||||
The methods param is a dict with the method names to find. On
|
The methods param is a dict with the method names to find. On
|
||||||
return, the dict values will be filled in with pointers to the
|
return, the dict values will be filled in with pointers to the
|
||||||
@@ -136,7 +129,7 @@ class PluginMgr():
|
|||||||
None is returned.
|
None is returned.
|
||||||
"""
|
"""
|
||||||
return_methods = None
|
return_methods = None
|
||||||
for _source_name, klass in self.get_plugins('source').items():
|
for _source_name, klass in cls.get_plugins('source').items():
|
||||||
if _source_name == source_name:
|
if _source_name == source_name:
|
||||||
for _method_name in methods:
|
for _method_name in methods:
|
||||||
if not hasattr(klass, _method_name):
|
if not hasattr(klass, _method_name):
|
||||||
@@ -147,5 +140,3 @@ class PluginMgr():
|
|||||||
methods[_method_name] = func
|
methods[_method_name] = func
|
||||||
return_methods = methods
|
return_methods = methods
|
||||||
return return_methods
|
return return_methods
|
||||||
|
|
||||||
pluginmgr = PluginMgr()
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ from time import strftime
|
|||||||
from wic import WicError
|
from wic import WicError
|
||||||
from wic.filemap import sparse_copy
|
from wic.filemap import sparse_copy
|
||||||
from wic.ksparser import KickStart, KickStartError
|
from wic.ksparser import KickStart, KickStartError
|
||||||
from wic.plugin import pluginmgr
|
from wic.plugin import PluginMgr
|
||||||
from wic.pluginbase import ImagerPlugin
|
from wic.pluginbase import ImagerPlugin
|
||||||
from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
|
from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
|
||||||
|
|
||||||
@@ -198,7 +198,7 @@ class DirectPlugin(ImagerPlugin):
|
|||||||
disk_name = self.parts[0].disk
|
disk_name = self.parts[0].disk
|
||||||
if source_plugin:
|
if source_plugin:
|
||||||
name = "do_install_disk"
|
name = "do_install_disk"
|
||||||
methods = pluginmgr.get_source_plugin_methods(source_plugin,
|
methods = PluginMgr.get_source_plugin_methods(source_plugin,
|
||||||
{name: None})
|
{name: None})
|
||||||
methods["do_install_disk"](self._image, disk_name, self, self.workdir,
|
methods["do_install_disk"](self._image, disk_name, self, self.workdir,
|
||||||
self.oe_builddir, self.bootimg_dir,
|
self.oe_builddir, self.bootimg_dir,
|
||||||
|
|||||||
Reference in New Issue
Block a user