Merge branch 'release-0.17' into devel
[tools/mic.git] / mic / plugin.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright (c) 2011 Intel, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 import os, sys
19 import msger
20 import pluginbase
21 from mic.conf import configmgr
22 from mic.utils import errors
23
24 __ALL__ = ['PluginMgr', 'pluginmgr']
25
26 PLUGIN_TYPES = ["imager", "backend"] # TODO  "hook"
27
28 class PluginMgr(object):
29     plugin_dirs = {}
30
31     # make the manager class as singleton
32     _instance = None
33     def __new__(cls, *args, **kwargs):
34         if not cls._instance:
35             cls._instance = super(PluginMgr, cls).__new__(cls, *args, **kwargs)
36
37         return cls._instance
38
39     def __init__(self):
40         self.plugin_dir = configmgr.common['plugin_dir']
41
42     def append_dirs(self, dirs):
43         for path in dirs:
44             self._add_plugindir(path)
45
46         # load all the plugins AGAIN
47         self._load_all()
48
49     def _add_plugindir(self, path):
50         path = os.path.abspath(os.path.expanduser(path))
51
52         if not os.path.isdir(path):
53             msger.warning("Plugin dir is not a directory or does not exist: %s"\
54                           % path)
55             return
56
57         if path not in self.plugin_dirs:
58             self.plugin_dirs[path] = False
59             # the value True/False means "loaded"
60
61     def _load_all(self):
62         for (pdir, loaded) in self.plugin_dirs.iteritems():
63             if loaded: continue
64
65             sys.path.insert(0, pdir)
66             for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]:
67                 if mod and mod != '__init__':
68                     if mod in sys.modules:
69                         #self.plugin_dirs[pdir] = True
70                         msger.warning("Module %s already exists, skip" % mod)
71                     else:
72                         try:
73                             pymod = __import__(mod)
74                             self.plugin_dirs[pdir] = True
75                             msger.debug("Plugin module %s:%s imported"\
76                                         % (mod, pymod.__file__))
77                         except ImportError, err:
78                             msg = 'Failed to load plugin %s/%s: %s' \
79                                 % (os.path.basename(pdir), mod, err)
80                             msger.warning(msg)
81
82             del(sys.path[0])
83
84     def get_plugins(self, ptype):
85         """ the return value is dict of name:class pairs """
86
87         if ptype not in PLUGIN_TYPES:
88             raise errors.CreatorError('%s is not valid plugin type' % ptype)
89
90         self._add_plugindir(os.path.join(self.plugin_dir, ptype))
91         self._load_all()
92
93         return pluginbase.get_plugins(ptype)
94
95 pluginmgr = PluginMgr()