Updated chroot's cleanup function
[tools/mic.git] / mic / pluginmgr.py
1 #!/usr/bin/python -t
2 import os
3 import sys
4 import logging
5
6 DEFAULT_PLUGIN_LOCATION = "/usr/lib/mic/plugins"
7 DEFINED_PLGUIN_TYPES = ["imager", "backend", "hook"]
8 STRING_PLUGIN_MARK = "mic_plugin"
9 STRING_PTYPE_MARK = "plugin_type"
10
11 class PluginMgr(object):
12     def __init__(self, plugin_dirs=[]):
13         self.plugin_locations = []
14         self.plugin_sets = {}
15         self.plugin_types = DEFINED_PLGUIN_TYPES
16         # initial plugin directory
17         self.addPluginDir(DEFAULT_PLUGIN_LOCATION)
18         for directory in plugin_dirs:
19             self.addPluginDir(os.path.expanduser(directory))
20         # intial plugin sets
21         for plugintype in self.plugin_types:
22             self.plugin_sets[plugintype] = []
23     
24     def addPluginDir(self, plugin_dir=None):
25         if not os.path.exists(plugin_dir):
26             logging.debug("Directory already exists: %s" % plugin_dir)
27             return
28         if not os.path.isdir(plugin_dir):
29             logging.debug("Not a directory: %s" % plugin_dir)
30             return
31         if not (plugin_dir in self.plugin_locations):
32             self.plugin_locations.append(plugin_dir)
33
34     def pluginCheck(self, pymod):
35         if not hasattr(pymod, STRING_PLUGIN_MARK):
36             logging.debug("Not a valid plugin: %s" % pymod.__file__)
37             logging.debug("Please check whether %s given" % STRING_PLUGIN_MARK)
38             return False
39         plclass = getattr(pymod, STRING_PLUGIN_MARK)[1]
40         if not hasattr(plclass, STRING_PTYPE_MARK):
41             logging.debug("Not a valid plugin: %s" % pymod.__file__)
42             logging.debug("Please check whether %s given" % STRING_PTYPE_MARK)
43             return False
44         pltype = getattr(plclass, STRING_PTYPE_MARK)
45         if not (pltype in self.plugin_types):
46             logging.debug("Unsupported plugin type in %s: %s" % (pymod.__file__, plugintype))
47             return False
48         return True
49
50     def importModule(self, dir_path, plugin_filename):
51         if plugin_filename.endswith(".pyc"):
52             return
53         if not plugin_filename.endswith(".py"):
54             logging.debug("Not a python file: %s" % os.path.join(dir_path, plugin_filename))
55             return
56         if plugin_filename == "__init__.py":
57             logging.debug("Unsupported python file: %s" % os.path.join(dir_path, plugin_filename))
58             return
59         modname = os.path.splitext(plugin_filename)[0]
60         if sys.modules.has_key(modname):
61             pymod = sys.modules[modname]
62             logging.debug("Module %s already exists: %s" % (modname, pymod.__file__))
63         else:
64             pymod = __import__(modname)
65             pymod.__file__ = os.path.join(dir_path, plugin_filename)
66         if not self.pluginCheck(pymod):
67             logging.warn("Failed to check plugin: %s" % os.path.join(dir_path, plugin_filename))
68             return
69         (pname, pcls) = pymod.__dict__[STRING_PLUGIN_MARK]
70         plugintype = getattr(pcls, STRING_PTYPE_MARK)
71         self.plugin_sets[plugintype].append((pname, pcls))
72
73     def loadPlugins(self):
74         for pdir in map(os.path.abspath, self.plugin_locations):
75             for pitem in os.walk(pdir):
76                 sys.path.insert(0, pitem[0])
77                 for pf in pitem[2]:
78                     self.importModule(pitem[0], pf)
79                 del(sys.path[0])
80
81     def getPluginByCateg(self, categ = None):
82         if not (categ in self.plugin_types):
83             logging.warn("Failed to get plugin category: %s" % categ)
84             return None
85         else:
86             return self.plugin_sets[categ]                            
87
88     def getImagerPlugins(self):
89         return self.plugin_sets['imager']
90
91     def getBackendPlugins(self):
92         return self.plugin_sets['backend']
93
94     def getHookPlugins(self):
95         return self.plugin_sets['hook']
96
97     def listAllPlugins(self):
98         for key in self.plugin_sets.keys():
99             sys.stdout.write("plugin type (%s) :::\n" % key)
100             for item in self.plugin_sets[key]:
101                 sys.stdout.write("%-6s: %s\n" % (item[0], item[1]))
102
103     def getPluginType(self, plugin_str):
104         pass
105
106 if __name__ == "__main__":
107     logging.getLogger().setLevel(logging.DEBUG)
108
109     pluginmgr = PluginMgr()
110     pluginmgr.loadPlugins()
111     pluginmgr.listAllPlugins()
112