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 any of the class names in 'basenames' is anywhere in the base 25 classes of the class 'derived'. 26 If 'derived' _is_ one in 'basenames', returns False. 28 'basenames' can be a string or an iterable (of strings). 30 if isinstance(basenames, basestring):
31 basenames = (basenames,)
32 for b
in derived.__bases__:
33 if b.__name__
in basenames:
42 Equivalent to GaudiKernel.ConfigurableDb.loadConfigurableDb(), but does a 43 deep search and executes the '*.confdb' files instead of importing them. 45 log = GaudiKernel.ConfigurableDb.log
46 from os.path
import join
as path_join
51 confDbFiles += [f
for f
in glob(path_join(path,
'*',
'*.confdb'))
54 pathlist = os.getenv(
"LD_LIBRARY_PATH",
"").split(os.pathsep)
55 for path
in filter(os.path.isdir, pathlist):
56 confDbFiles += [f
for f
in [path_join(path, f)
for f
in os.listdir(path)
57 if f.endswith(
'.confdb')]]
59 for confDb
in confDbFiles:
60 log.debug(
"\t-loading [%s]..." % confDb )
62 cfgDb._loadModule( confDb )
63 except Exception, err:
66 log.warning(
"Could not load file [%s] !", confDb )
67 log.warning(
"Reason: %s", err )
73 Find in the module 'modulename' all the classes that derive from ConfigurableUser. 74 Return the list of the names. 75 The flag mayNotExist is used to choose the level of the logging message in case 76 the requested module does not exist. 79 oldpath = list(sys.path)
81 moduleelements = modulename.split(
'.')
82 if len(moduleelements) > 1:
83 moddir = os.sep.join([root] + moduleelements[:-1])
87 shortmodname = moduleelements[-1]
89 if not os.path.exists(os.path.join(moddir, shortmodname) +
".py"):
90 msg =
"Module %s does not exist" % modulename
98 sys.path.insert(0, moddir)
99 logging.verbose(
"sys.path prepended with %r", sys.path[0])
101 logging.info(
"Looking for ConfigurableUser in %r", modulename)
104 logging.verbose(
"importing %s", shortmodname)
105 exec
"import %s as mod" % shortmodname
in g, l
108 logging.verbose(
"restoring old sys.path")
111 if "__all__" in dir(mod)
and mod.__all__:
114 all = [ n
for n
in dir(mod)
if not n.startswith(
"_")]
117 cfg = cfgDb.get(name)
118 if cfg
and cfg[
"module"] != modulename:
120 logging.verbose(
"Object %r already found in module %r", name, cfg[
"module"])
122 t = getattr(mod, name)
123 if isinstance(t, type)
and _inheritsfrom(t, (
'ConfigurableUser',
126 logging.verbose(
"Found %r", result)
130 from optparse
import OptionParser
131 parser = OptionParser(prog = os.path.basename(sys.argv[0]),
132 usage =
"%prog [options] <PackageName> [<Module1> ...]")
133 parser.add_option(
"-o",
"--output", action=
"store", type=
"string",
134 help=
"output file for confDb data [default = '../genConf/<PackageName>_user_confDb.py'].")
135 parser.add_option(
"-r",
"--root", action=
"store", type=
"string",
136 help=
"root directory of the python modules [default = '../python'].")
137 parser.add_option(
"-v",
"--verbose", action=
"store_true",
138 help=
"print some debugging information")
139 parser.add_option(
"--debug", action=
"store_true",
140 help=
"print more debugging information")
141 parser.set_defaults(root = os.path.join(
"..",
"python"))
143 opts, args = parser.parse_args()
146 log_level = logging.DEBUG
148 log_level = logging.VERBOSE
150 log_level = logging.INFO
if os.environ.get(
'VERBOSE')
else logging.WARNING
151 logging.basicConfig(format =
"%(levelname)s: %(message)s",
156 parser.error(
"PackageName is required")
158 package_name = args.pop(0)
160 usingConvention =
False 163 args = [package_name +
".Configuration"]
164 usingConvention =
True 166 genConfDir = os.path.join(
"..", os.environ.get(
"CMTCONFIG",
""),
"genConf")
167 if not os.path.exists(genConfDir):
168 genConfDir = os.path.join(
"..",
"genConf")
171 outputfile = os.path.join(genConfDir, package_name +
'_user.confdb')
173 outputfile = opts.output
178 import Gaudi.Configurables
179 Gaudi.Configurables.ignoreMissingConfigurables =
True 188 sys.path.insert(0, genConfDir)
189 sys.path.insert(0, os.path.join(
"..",
"python"))
190 localConfDb = os.path.join(genConfDir, package_name, package_name +
'.confdb')
191 if os.path.exists(localConfDb):
192 cfgDb._loadModule(localConfDb)
194 package_module = __import__(package_name)
195 package_module.__path__.insert(0, os.path.join(genConfDir, package_name))
207 logging.error(
"Cannot import module %r:\n%s", mod,
208 traceback.format_exc().rstrip())
214 cfgDb.add(configurable = m,
218 elif not usingConvention:
219 logging.warning(
"Specified module %r does not contain ConfigurableUser specializations", mod)
222 logging.info(
"ConfigurableUser found:\n%s", pformat(cus))
224 output =
"""## -*- ascii -*- 225 # db file automatically generated by %s on: %s 226 """ % (parser.prog, time.asctime())
230 output +=
"%s %s %s\n" % (mod,
'None', cu)
233 output +=
"## %s\n" % package_name
234 elif usingConvention:
235 logging.info(
"No ConfigurableUser found")
236 output = (
"# db file automatically generated by %s on: %s\n" 237 "# No ConfigurableUser specialization in %s\n") % (parser.prog, time.asctime(), package_name)
239 logging.error(
"No ConfigurableUser specialization found")
243 output_dir = os.path.dirname(outputfile)
245 logging.info(
"Creating directory %r", output_dir)
246 os.makedirs(output_dir, 0755)
249 if err.errno == errno.EEXIST:
256 logging.verbose(
"Writing confDb data to %r", outputfile)
257 open(outputfile,
"w").
write(output)
260 if __name__ ==
'__main__':
def loadConfigurableDb()
Helper function to load all ConfigurableDb files holding informations.
def getConfigurableUsers(modulename, root, mayNotExist=False)
def _inheritsfrom(derived, basenames)
constexpr void apply(const Fun &, Container &, Args...)