Misc s/func/certmaster/ replacements
[certmaster.git] / certmaster / minion / module_loader.py
1 ## func
2 ##
3 ## Copyright 2007, Red Hat, Inc
4 ## See AUTHORS
5 ##
6 ## This software may be freely redistributed under the terms of the GNU
7 ## general public license.
8 ##
9 ## You should have received a copy of the GNU General Public License
10 ## along with this program; if not, write to the Free Software
11 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
12 ##
13 ##
14
15
16 import distutils.sysconfig
17 import os
18 import sys
19 from gettext import gettext
20 _ = gettext
21
22 from func import logger
23 logger = logger.Logger().logger
24
25 from inspect import isclass
26 from func.minion.modules import func_module
27
28 def module_walker(topdir):
29 module_files = []
30 for root, dirs, files in os.walk(topdir):
31 # we should get here for each subdir
32 for filename in files:
33 # ASSUMPTION: all module files will end with .py, .pyc, .pyo
34 if filename[-3:] == ".py" or filename[-4:] == ".pyc" or filename[-4:] == ".pyo":
35 # the normpath is important, since we eventually replace /'s with .'s
36 # in the module name, and foo..bar doesnt work -akl
37 module_files.append(os.path.normpath("%s/%s" % (root, filename)))
38
39
40 return module_files
41
42 def load_modules(blacklist=None):
43
44 module_file_path="%s/func/minion/modules/" % distutils.sysconfig.get_python_lib()
45 mod_path="%s/func/minion/" % distutils.sysconfig.get_python_lib()
46
47 sys.path.insert(0, mod_path)
48 mods = {}
49 bad_mods = {}
50
51 filenames = module_walker(module_file_path)
52
53 # FIXME: this is probably more complicated than it needs to be -akl
54 for fn in filenames:
55 # aka, everything after the module_file_path
56 module_name_part = fn[len(module_file_path):]
57 dirname, basename = os.path.split(module_name_part)
58
59 if basename[:8] == "__init__":
60 modname = dirname
61 dirname = ""
62 elif basename[-3:] == ".py":
63 modname = basename[:-3]
64 elif basename[-4:] in [".pyc", ".pyo"]:
65 modname = basename[:-4]
66
67 pathname = modname
68 if dirname != "":
69 pathname = "%s/%s" % (dirname, modname)
70
71 mod_imp_name = pathname.replace("/", ".")
72
73 if mods.has_key(mod_imp_name):
74 # If we've already imported mod_imp_name, don't import it again
75 continue
76
77 # ignore modules that we've already determined aren't valid modules
78 if bad_mods.has_key(mod_imp_name):
79 continue
80
81 try:
82 # Auto-detect and load all FuncModules
83 blip = __import__("modules.%s" % ( mod_imp_name), globals(), locals(), [mod_imp_name])
84 for obj in dir(blip):
85 attr = getattr(blip, obj)
86 if isclass(attr) and issubclass(attr, func_module.FuncModule):
87 logger.debug("Loading %s module" % attr)
88 mods[mod_imp_name] = attr()
89
90 except ImportError, e:
91 # A module that raises an ImportError is (for now) simply not loaded.
92 errmsg = _("Could not load %s module: %s")
93 logger.warning(errmsg % (mod_imp_name, e))
94 bad_mods[mod_imp_name] = True
95 continue
96 except:
97 errmsg = _("Could not load %s module")
98 logger.warning(errmsg % (mod_imp_name))
99 bad_mods[mod_imp_name] = True
100 continue
101
102 return mods
103
104
105 if __name__ == "__main__":
106
107 module_file_path = "/usr/lib/python2.5/site-packages/func/minion/modules/"
108 bar = module_walker(module_file_path)
109 print bar
110 for f in bar:
111 print f
112 print os.path.basename(f)
113 print os.path.split(f)
114 g = f[len(module_file_path):]
115 print g
116 print os.path.split(g)
117
118 print load_modules()