3 ## Copyright 2007, Red Hat, Inc
6 ## This software may be freely redistributed under the terms of the GNU
7 ## general public license.
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.
16 import distutils
.sysconfig
19 from gettext
import gettext
22 from func
import logger
23 logger
= logger
.Logger().logger
25 from inspect
import isclass
26 from func
.minion
.modules
import func_module
28 def module_walker(topdir
):
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
)))
42 def load_modules(blacklist
=None):
44 module_file_path
="%s/func/minion/modules/" % distutils
.sysconfig
.get_python_lib()
45 mod_path
="%s/func/minion/" % distutils
.sysconfig
.get_python_lib()
47 sys
.path
.insert(0, mod_path
)
51 filenames
= module_walker(module_file_path
)
53 # FIXME: this is probably more complicated than it needs to be -akl
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
)
59 if basename
[:8] == "__init__":
62 elif basename
[-3:] == ".py":
63 modname
= basename
[:-3]
64 elif basename
[-4:] in [".pyc", ".pyo"]:
65 modname
= basename
[:-4]
69 pathname
= "%s/%s" % (dirname
, modname
)
71 mod_imp_name
= pathname
.replace("/", ".")
73 if mods
.has_key(mod_imp_name
):
74 # If we've already imported mod_imp_name, don't import it again
77 # ignore modules that we've already determined aren't valid modules
78 if bad_mods
.has_key(mod_imp_name
):
82 # Auto-detect and load all FuncModules
83 blip
= __import__("modules.%s" % ( mod_imp_name
), globals(), locals(), [mod_imp_name
])
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()
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
97 errmsg
= _("Could not load %s module")
98 logger
.warning(errmsg
% (mod_imp_name
))
99 bad_mods
[mod_imp_name
] = True
105 if __name__
== "__main__":
107 module_file_path
= "/usr/lib/python2.5/site-packages/func/minion/modules/"
108 bar
= module_walker(module_file_path
)
112 print os
.path
.basename(f
)
113 print os
.path
.split(f
)
114 g
= f
[len(module_file_path
):]
116 print os
.path
.split(g
)