more concise plugin related expressions
authorJF Ding <jian-feng.ding@intel.com>
Tue, 13 Dec 2011 07:19:43 +0000 (23:19 -0800)
committerJF Ding <jian-feng.ding@intel.com>
Tue, 13 Dec 2011 07:19:43 +0000 (23:19 -0800)
mic/creator.py
mic/plugin.py [moved from mic/pluginmgr.py with 78% similarity]
plugins/imager/fs_plugin.py
plugins/imager/livecd_plugin.py
plugins/imager/liveusb_plugin.py
plugins/imager/loop_plugin.py
plugins/imager/raw_plugin.py
tools/mic

index 90d14d5..9b25e86 100644 (file)
 import os, sys
 from optparse import SUPPRESS_HELP
 
-from mic import pluginmgr, msger
+from mic import msger
 from mic.utils import cmdln, errors, rpmmisc
 from conf import configmgr
+from plugin import pluginmgr
 
 class Creator(cmdln.Cmdln):
     """${name}: create an image
@@ -38,10 +39,8 @@ class Creator(cmdln.Cmdln):
         cmdln.Cmdln.__init__(self, *args, **kwargs)
 
         # get cmds from pluginmgr
-        self.plugincmds = pluginmgr.PluginMgr().get_plugins('imager')
-
         # mix-in do_subcmd interface
-        for subcmd, klass in self.plugincmds.iteritems():
+        for subcmd, klass in pluginmgr.get_plugins('imager').iteritems():
             if not hasattr(klass, 'do_create'):
                 msger.warning("Unsurpport subcmd: %s" % subcmd)
                 continue
@@ -141,8 +140,8 @@ class Creator(cmdln.Cmdln):
             if self.options.arch in supported_arch:
                 configmgr.create['arch'] = self.options.arch
             else:
-                raise errors.Usage('Invalid architecture: "%s".\n' \
-                                   '  Supported architectures are: \n' \
+                raise errors.Usage('Invalid architecture: "%s".\n'
+                                   '  Supported architectures are: \n'
                                    '  %s\n' % (self.options.arch, ', '.join(supported_arch)))
 
         if self.options.pkgmgr is not None:
similarity index 78%
rename from mic/pluginmgr.py
rename to mic/plugin.py
index f47cb67..6f86e7d 100644 (file)
 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 import os, sys
-from mic import msger
-from mic import pluginbase
+import msger
+import pluginbase
+
+__ALL__ = ['PluginMgr', 'pluginmgr']
 
 DEFAULT_PLUGIN_LOCATION = "/usr/lib/mic/plugins"
 
@@ -34,27 +36,28 @@ class PluginMgr(object):
 
         return cls._instance
 
-    def __init__(self, plugin_dirs=[]):
+    def __init__(self):
 
         # default plugin directory
         for pt in PLUGIN_TYPES:
             self._add_plugindir(os.path.join(DEFAULT_PLUGIN_LOCATION, pt))
 
-        for dir in plugin_dirs:
-            self._add_plugindir(dir)
+    def append_dirs(self, dirs):
+        for path in dirs:
+            self._add_plugindir(path)
 
-        # load all the plugins
+        # load all the plugins AGAIN
         self._load_all()
 
-    def _add_plugindir(self, dir):
-        dir = os.path.abspath(os.path.expanduser(dir))
+    def _add_plugindir(self, path):
+        path = os.path.abspath(os.path.expanduser(path))
 
-        if not os.path.isdir(dir):
-            msger.warning("Plugin dir is not a directory or does not exist: %s" % dir)
+        if not os.path.isdir(path):
+            msger.warning("Plugin dir is not a directory or does not exist: %s" % path)
             return
 
-        if dir not in self.plugin_dirs:
-            self.plugin_dirs[dir] = False
+        if path not in self.plugin_dirs:
+            self.plugin_dirs[path] = False
             # the value True/False means "loaded"
 
     def _load_all(self):
@@ -65,7 +68,8 @@ class PluginMgr(object):
             for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]:
                 if mod and mod != '__init__':
                     if mod in sys.modules:
-                        msger.debug("Module %s already exists, skip" % mod)
+                        #self.plugin_dirs[pdir] = True
+                        msger.warning("Module %s already exists, skip" % mod)
                     else:
                         try:
                             pymod = __import__(mod)
@@ -78,4 +82,10 @@ class PluginMgr(object):
 
     def get_plugins(self, ptype):
         """ the return value is dict of name:class pairs """
+
+        # good place to load all the plugins
+        self._load_all()
+
         return pluginbase.get_plugins(ptype)
+
+pluginmgr = PluginMgr()
index eb40658..eb7dab5 100644 (file)
 
 import os
 
-from mic import pluginmgr, chroot, msger
+from mic import chroot, msger
 from mic.utils import cmdln, misc, errors
 from mic.imager import fs
 from mic.conf import configmgr
+from mic.plugin import pluginmgr
 
 from mic.pluginbase import ImagerPlugin
 class FsPlugin(ImagerPlugin):
@@ -60,13 +61,13 @@ class FsPlugin(ImagerPlugin):
 
         # try to find the pkgmgr
         pkgmgr = None
-        for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems():
+        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
             if key == creatoropts['pkgmgr']:
                 pkgmgr = pcls
                 break
 
         if not pkgmgr:
-            pkgmgrs = pluginmgr.PluginMgr().get_plugins('backend').keys()
+            pkgmgrs = pluginmgr.get_plugins('backend').keys()
             raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))
 
         creator = fs.FsImageCreator(creatoropts, pkgmgr)
index e7d9357..ade1821 100644 (file)
@@ -19,10 +19,11 @@ import os
 import shutil
 import tempfile
 
-from mic import pluginmgr, chroot, msger
+from mic import chroot, msger
 from mic.utils import misc, fs_related, errors
 from mic.conf import configmgr
 import mic.imager.livecd as livecd
+from mic.plugin import pluginmgr
 
 from mic.pluginbase import ImagerPlugin
 class LiveCDPlugin(ImagerPlugin):
@@ -65,13 +66,13 @@ class LiveCDPlugin(ImagerPlugin):
 
         # try to find the pkgmgr
         pkgmgr = None
-        for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems():
+        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
             if key == creatoropts['pkgmgr']:
                 pkgmgr = pcls
                 break
 
         if not pkgmgr:
-            pkgmgrs = pluginmgr.PluginMgr().get_plugins('backend').keys()
+            pkgmgrs = pluginmgr.get_plugins('backend').keys()
             raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))
 
         creator = livecd.LiveCDImageCreator(creatoropts, pkgmgr)
index 5beeceb..7b420c6 100644 (file)
@@ -19,10 +19,11 @@ import os
 import shutil
 import tempfile
 
-from mic import pluginmgr, chroot, msger
+from mic import chroot, msger
 from mic.utils import misc, fs_related, errors
 from mic.utils.partitionedfs import PartitionedMount
 from mic.conf import configmgr
+from mic.plugin import pluginmgr
 
 import mic.imager.liveusb as liveusb
 
@@ -67,13 +68,13 @@ class LiveUSBPlugin(ImagerPlugin):
 
         # try to find the pkgmgr
         pkgmgr = None
-        for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems():
+        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
             if key == creatoropts['pkgmgr']:
                 pkgmgr = pcls
                 break
 
         if not pkgmgr:
-            pkgmgrs = pluginmgr.PluginMgr().get_plugins('backend').keys()
+            pkgmgrs = pluginmgr.get_plugins('backend').keys()
             raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))
 
         creator = liveusb.LiveUSBImageCreator(creatoropts, pkgmgr)
index 5341baf..dd4b7bf 100644 (file)
@@ -19,9 +19,10 @@ import os
 import shutil
 import tempfile
 
-from mic import pluginmgr, chroot, msger
+from mic import chroot, msger
 from mic.utils import misc, fs_related, errors, cmdln
 from mic.conf import configmgr
+from mic.plugin import pluginmgr
 import mic.imager.loop as loop
 
 from mic.pluginbase import ImagerPlugin
@@ -62,13 +63,13 @@ class LoopPlugin(ImagerPlugin):
 
         # try to find the pkgmgr
         pkgmgr = None
-        for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems():
+        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
             if key == creatoropts['pkgmgr']:
                 pkgmgr = pcls
                 break
 
         if not pkgmgr:
-            pkgmgrs = pluginmgr.PluginMgr().get_plugins('backend').keys()
+            pkgmgrs = pluginmgr.get_plugins('backend').keys()
             raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))
 
         creator = loop.LoopImageCreator(creatoropts, pkgmgr, opts.taring_to)
index adf9ea1..dbf060f 100644 (file)
@@ -20,9 +20,10 @@ import shutil
 import re
 import tempfile
 
-from mic import pluginmgr, chroot, msger
+from mic import chroot, msger
 from mic.utils import misc, fs_related, errors, runner
 from mic.conf import configmgr
+from mic.plugin import pluginmgr
 from mic.utils.partitionedfs import PartitionedMount
 
 import mic.imager.raw as raw
@@ -64,13 +65,13 @@ class RawPlugin(ImagerPlugin):
 
         # try to find the pkgmgr
         pkgmgr = None
-        for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems():
+        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
             if key == creatoropts['pkgmgr']:
                 pkgmgr = pcls
                 break
 
         if not pkgmgr:
-            pkgmgrs = pluginmgr.PluginMgr().get_plugins('backend').keys()
+            pkgmgrs = pluginmgr.get_plugins('backend').keys()
             raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))
 
         creator = raw.RawImageCreator(creatoropts, pkgmgr)
index 67beabe..a331a32 100755 (executable)
--- a/tools/mic
+++ b/tools/mic
 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 import os, sys
-from mic import msger, creator, pluginmgr
+from mic import msger, creator
 from mic.utils import cmdln, misc, errors
+from mic.plugin import pluginmgr
 from mic.__version__ import VERSION
 
-class Mic(cmdln.Cmdln):
+class MicCmd(cmdln.Cmdln):
     """
     Usage: mic SUBCOMMAND [OPTS] [ARGS...]
 
-    Mic Image Creation Tool.
+    mic Means the Image Creation tool
     Try 'mic help SUBCOMAND' for help on a specific subcommand.
 
     ${command_list}
@@ -108,7 +109,7 @@ class Mic(cmdln.Cmdln):
 
         srcimager = None
         destimager = None
-        for iname, icls in pluginmgr.PluginMgr().get_plugins('imager').iteritems():
+        for iname, icls in pluginmgr.get_plugins('imager').iteritems():
            if iname == srcformat and hasattr(icls, "do_unpack"):
                srcimager = icls
            if iname == destformat and hasattr(icls, "do_pack"):
@@ -149,7 +150,7 @@ class Mic(cmdln.Cmdln):
             imagetype = "loop"
 
         chrootclass = None
-        for pname, pcls in pluginmgr.PluginMgr().get_plugins('imager').iteritems():
+        for pname, pcls in pluginmgr.get_plugins('imager').iteritems():
             if pname == imagetype and hasattr(pcls, "do_chroot"):
                 chrootclass = pcls
                 break
@@ -161,7 +162,7 @@ class Mic(cmdln.Cmdln):
 
 if __name__ == "__main__":
     try:
-        mic = Mic()
+        mic = MicCmd()
         sys.exit(mic.main())
 
     except KeyboardInterrupt: