4 Generate _confDb.py files for ConfigurableUser classes. 13 from pprint
import pformat
17 logging.VERBOSE = (logging.INFO + logging.DEBUG) / 2
18 logging.addLevelName(logging.VERBOSE,
"VERBOSE")
19 logging.verbose =
lambda msg, *args, **kwargs: \
20 apply(logging.log, (logging.VERBOSE, msg) + args, kwargs)
24 Check if the class name 'basename' is anywhere in the base classes of the 26 If 'derived' _is_ 'basename', returns False. 28 for b
in derived.__bases__:
29 if b.__name__ == basename:
38 Equivalent to GaudiKernel.ConfigurableDb.loadConfigurableDb(), but does a 39 deep search and executes the '*.confdb' files instead of importing them. 41 log = GaudiKernel.ConfigurableDb.log
42 from os.path
import join
as path_join
47 confDbFiles += [f
for f
in glob(path_join(path,
'*',
'*.confdb'))
50 pathlist = os.getenv(
"LD_LIBRARY_PATH",
"").split(os.pathsep)
51 for path
in filter(os.path.isdir, pathlist):
52 confDbFiles += [f
for f
in [path_join(path, f)
for f
in os.listdir(path)
53 if f.endswith(
'.confdb')]]
55 for confDb
in confDbFiles:
56 log.debug(
"\t-loading [%s]..." % confDb )
58 cfgDb._loadModule( confDb )
59 except Exception, err:
62 log.warning(
"Could not load file [%s] !", confDb )
63 log.warning(
"Reason: %s", err )
69 Find in the module 'modulename' all the classes that derive from ConfigurableUser. 70 Return the list of the names. 71 The flag mayNotExist is used to choose the level of the logging message in case 72 the requested module does not exist. 75 oldpath = list(sys.path)
77 moduleelements = modulename.split(
'.')
78 if len(moduleelements) > 1:
79 moddir = os.sep.join([root] + moduleelements[:-1])
83 shortmodname = moduleelements[-1]
85 if not os.path.exists(os.path.join(moddir, shortmodname) +
".py"):
86 msg =
"Module %s does not exist" % modulename
94 sys.path.insert(0, moddir)
95 logging.verbose(
"sys.path prepended with %r", sys.path[0])
97 logging.info(
"Looking for ConfigurableUser in %r", modulename)
100 logging.verbose(
"importing %s", shortmodname)
101 exec
"import %s as mod" % shortmodname
in g, l
104 logging.verbose(
"restoring old sys.path")
107 if "__all__" in dir(mod)
and mod.__all__:
110 all = [ n
for n
in dir(mod)
if not n.startswith(
"_")]
113 cfg = cfgDb.get(name)
114 if cfg
and cfg[
"module"] != modulename:
116 logging.verbose(
"Object %r already found in module %r", name, cfg[
"module"])
118 t = getattr(mod, name)
119 if isinstance(t, type)
and _inheritsfrom(t,
"ConfigurableUser"):
121 logging.verbose(
"Found %r", result)
125 from optparse
import OptionParser
126 parser = OptionParser(prog = os.path.basename(sys.argv[0]),
127 usage =
"%prog [options] <PackageName> [<Module1> ...]")
128 parser.add_option(
"-o",
"--output", action=
"store", type=
"string",
129 help=
"output file for confDb data [default = '../genConf/<PackageName>_user_confDb.py'].")
130 parser.add_option(
"-r",
"--root", action=
"store", type=
"string",
131 help=
"root directory of the python modules [default = '../python'].")
132 parser.add_option(
"-v",
"--verbose", action=
"store_true",
133 help=
"print some debugging information")
134 parser.add_option(
"--debug", action=
"store_true",
135 help=
"print more debugging information")
136 parser.set_defaults(root = os.path.join(
"..",
"python"))
138 opts, args = parser.parse_args()
141 log_level = logging.DEBUG
143 log_level = logging.VERBOSE
145 log_level = logging.INFO
if os.environ.get(
'VERBOSE')
else logging.WARNING
146 logging.basicConfig(format =
"%(levelname)s: %(message)s",
151 parser.error(
"PackageName is required")
153 package_name = args.pop(0)
155 usingConvention =
False 158 args = [package_name +
".Configuration"]
159 usingConvention =
True 161 genConfDir = os.path.join(
"..", os.environ.get(
"CMTCONFIG",
""),
"genConf")
162 if not os.path.exists(genConfDir):
163 genConfDir = os.path.join(
"..",
"genConf")
166 outputfile = os.path.join(genConfDir, package_name +
'_user.confdb')
168 outputfile = opts.output
173 import Gaudi.Configurables
174 Gaudi.Configurables.ignoreMissingConfigurables =
True 183 sys.path.insert(0, genConfDir)
184 sys.path.insert(0, os.path.join(
"..",
"python"))
185 localConfDb = os.path.join(genConfDir, package_name, package_name +
'.confdb')
186 if os.path.exists(localConfDb):
187 cfgDb._loadModule(localConfDb)
189 package_module = __import__(package_name)
190 package_module.__path__.insert(0, os.path.join(genConfDir, package_name))
202 logging.error(
"Cannot import module %r:\n%s", mod,
203 traceback.format_exc().rstrip())
209 cfgDb.add(configurable = m,
213 elif not usingConvention:
214 logging.warning(
"Specified module %r does not contain ConfigurableUser specializations", mod)
217 logging.info(
"ConfigurableUser found:\n%s", pformat(cus))
219 output =
"""## -*- ascii -*- 220 # db file automatically generated by %s on: %s 221 """ % (parser.prog, time.asctime())
225 output +=
"%s %s %s\n" % (mod,
'None', cu)
228 output +=
"## %s\n" % package_name
229 elif usingConvention:
230 logging.info(
"No ConfigurableUser found")
231 output = (
"# db file automatically generated by %s on: %s\n" 232 "# No ConfigurableUser specialization in %s\n") % (parser.prog, time.asctime(), package_name)
234 logging.error(
"No ConfigurableUser specialization found")
238 output_dir = os.path.dirname(outputfile)
240 logging.info(
"Creating directory %r", output_dir)
241 os.makedirs(output_dir, 0755)
244 if err.errno == errno.EEXIST:
251 logging.verbose(
"Writing confDb data to %r", outputfile)
252 open(outputfile,
"w").
write(output)
255 if __name__ ==
'__main__':
def loadConfigurableDb()
Helper function to load all ConfigurableDb files holding informations.
def _inheritsfrom(derived, basename)
def getConfigurableUsers(modulename, root, mayNotExist=False)