Merge release-0.28.17 from 'tools/mic'
[platform/upstream/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
20 from mic import msger
21 from mic import pluginbase
22 from mic.conf import configmgr
23 from mic.utils import errors
24
25
26 __ALL__ = ['PluginMgr', 'pluginmgr']
27
28 PLUGIN_TYPES = ["imager", "backend"] # TODO  "hook"
29
30
31 class PluginMgr(object):
32     plugin_dirs = {}
33
34     # make the manager class as singleton
35     _instance = None
36     def __new__(cls, *args, **kwargs):
37         if not cls._instance:
38             cls._instance = super(PluginMgr, cls).__new__(cls, *args, **kwargs)
39
40         return cls._instance
41
42     def __init__(self):
43         self.plugin_dir = configmgr.common['plugin_dir']
44
45     def append_dirs(self, dirs):
46         for path in dirs:
47             self._add_plugindir(path)
48
49         # load all the plugins AGAIN
50         self._load_all()
51
52     def _add_plugindir(self, path):
53         path = os.path.abspath(os.path.expanduser(path))
54
55         if not os.path.isdir(path):
56             msger.warning("Plugin dir is not a directory or does not exist: %s"\
57                           % path)
58             return
59
60         if path not in self.plugin_dirs:
61             self.plugin_dirs[path] = False
62             # the value True/False means "loaded"
63
64     def _load_all(self):
65         for (pdir, loaded) in self.plugin_dirs.iteritems():
66             if loaded: continue
67
68             sys.path.insert(0, pdir)
69             for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]:
70                 if mod and mod != '__init__':
71                     if mod in sys.modules:
72                         #self.plugin_dirs[pdir] = True
73                         msger.warning("Module %s already exists, skip" % mod)
74                     else:
75                         try:
76                             pymod = __import__(mod)
77                             self.plugin_dirs[pdir] = True
78                             msger.debug("Plugin module %s:%s imported"\
79                                         % (mod, pymod.__file__))
80                         except ImportError as err:
81                             msg = 'Failed to load plugin %s/%s: %s' \
82                                 % (os.path.basename(pdir), mod, err)
83                             msger.verbose(msg)
84
85             del(sys.path[0])
86
87     def get_plugins(self, ptype):
88         """ the return value is dict of name:class pairs """
89
90         if ptype not in PLUGIN_TYPES:
91             raise errors.CreatorError('%s is not valid plugin type' % ptype)
92
93         self._add_plugindir(os.path.join(self.plugin_dir, ptype))
94         self._load_all()
95
96         return pluginbase.get_plugins(ptype)
97
98 pluginmgr = PluginMgr()