3 Generate _confDb.py files for ConfigurableUser classes.
12 from pprint
import pformat
16 logging.VERBOSE = (logging.INFO + logging.DEBUG) / 2
17 logging.addLevelName(logging.VERBOSE,
"VERBOSE")
18 logging.verbose =
lambda msg, *args, **kwargs: \
19 apply(logging.log, (logging.VERBOSE, msg) + args, kwargs)
23 Check if the class name 'basename' is anywhere in the base classes of the
25 If 'derived' _is_ 'basename', returns False.
27 for b
in derived.__bases__:
28 if b.__name__ == basename:
37 Equivalent to GaudiKernel.ConfigurableDb.loadConfigurableDb(), but does a
38 deep search and executes the '*_confDb.py' files instead of importing them.
42 for f
in [f
for f
in glob(os.path.join(p,
'*',
'*_confDb.py'))
43 if 'merged' not in f
and os.path.isfile(f)]:
44 logging.verbose(
'Loading %s', f)
56 Find in the module 'modulename' all the classes that derive from ConfigurableUser.
57 Return the list of the names.
58 The flag mayNotExist is used to choose the level of the logging message in case
59 the requested module does not exist.
62 oldpath = list(sys.path)
64 moduleelements = modulename.split(
'.')
65 if len(moduleelements) > 1:
66 moddir = os.sep.join([root] + moduleelements[:-1])
70 shortmodname = moduleelements[-1]
72 if not os.path.exists(os.path.join(moddir, shortmodname) +
".py"):
73 msg =
"Module %s does not exist" % modulename
81 sys.path.insert(0, moddir)
82 logging.verbose(
"sys.path prepended with %r", sys.path[0])
84 logging.info(
"Looking for ConfigurableUser in %r", modulename)
87 logging.verbose(
"importing %s", shortmodname)
88 exec
"import %s as mod" % shortmodname
in g, l
91 logging.verbose(
"restoring old sys.path")
94 if "__all__" in dir(mod)
and mod.__all__:
97 all = [ n
for n
in dir(mod)
if not n.startswith(
"_")]
100 cfg = cfgDb.get(name)
101 if cfg
and cfg[
"module"] != modulename:
103 logging.verbose(
"Object %r already found in module %r", name, cfg[
"module"])
105 t = getattr(mod, name)
106 if isinstance(t, type)
and _inheritsfrom(t,
"ConfigurableUser"):
108 logging.verbose(
"Found %r", result)
112 from optparse
import OptionParser
113 parser = OptionParser(prog = os.path.basename(sys.argv[0]),
114 usage =
"%prog [options] <PackageName> [<Module1> ...]")
115 parser.add_option(
"-o",
"--output", action=
"store", type=
"string",
116 help=
"output file for confDb data [default = '../genConf/<PackageName>_user_confDb.py'].")
117 parser.add_option(
"-r",
"--root", action=
"store", type=
"string",
118 help=
"root directory of the python modules [default = '../python'].")
119 parser.add_option(
"-v",
"--verbose", action=
"store_true",
120 help=
"print some debugging information")
121 parser.add_option(
"--debug", action=
"store_true",
122 help=
"print more debugging information")
123 parser.add_option(
"--lockerpath", action=
"store",
125 help=
"directory where to find the module 'locker'")
126 parser.set_defaults(root = os.path.join(
"..",
"python"))
128 opts, args = parser.parse_args()
131 log_level = logging.DEBUG
133 log_level = logging.VERBOSE
135 log_level = logging.INFO
136 logging.basicConfig(format =
"%(levelname)s: %(message)s",
141 parser.error(
"PackageName is required")
143 package_name = args.pop(0)
145 usingConvention =
False
148 args = [package_name +
".Configuration"]
149 usingConvention =
True
151 genConfDir = os.path.join(
"..", os.environ.get(
"CMTCONFIG",
""),
"genConf")
152 if not os.path.exists(genConfDir):
153 genConfDir = os.path.join(
"..",
"genConf")
156 outputfile = os.path.join(genConfDir, package_name +
'_user_confDb.py')
158 outputfile = opts.output
164 if "GAUDI_BUILD_LOCK" in os.environ:
166 sys.path.append(opts.lockerpath)
170 from locker
import LockFile
172 def LockFile(*args, **kwargs):
175 dbLock = LockFile(os.environ[
"GAUDI_BUILD_LOCK"], temporary =
True)
180 import Gaudi.Configurables
181 Gaudi.Configurables.ignoreMissingConfigurables =
True
190 sys.path.insert(0, genConfDir)
191 sys.path.insert(0, os.path.join(
"..",
"python"))
192 localConfDb = os.path.join(genConfDir, package_name, package_name +
'_confDb.py')
193 if os.path.exists(localConfDb):
194 execfile(localConfDb, {}, {})
196 package_module = __import__(package_name)
197 package_module.__path__.insert(0, os.path.join(genConfDir, package_name))
210 logging.error(
"Cannot import module %r:\n%s", mod,
211 traceback.format_exc().rstrip())
217 cfgDb.add(configurable = m,
221 elif not usingConvention:
222 logging.warning(
"Specified module %r does not contain ConfigurableUser specializations", mod)
225 logging.info(
"ConfigurableUser found:\n%s", pformat(cus))
227 output =
"""## -*- python -*-
228 # db file automatically generated by %s on: %s
229 ## insulates outside world against anything bad that could happen
230 ## also prevents global scope pollution
232 from GaudiKernel.Proxy.ConfigurableDb import CfgDb
234 # get a handle on the repository of Configurables
237 # populate the repository with informations on Configurables
238 """ % (parser.prog, time.asctime())
243 cfgDb.add( configurable = '%s',
246 lib = 'None' )""" % (cu, package_name, mod)
253 # fill cfgDb at module import...
258 except Exception,err:
259 print "Py:ConfigurableDb ERROR Problem with [%%s] content!" %% __name__
260 print "Py:ConfigurableDb ERROR",err
261 print "Py:ConfigurableDb ERROR ==> culprit is package [%s] !"
263 elif usingConvention:
264 logging.info(
"No ConfigurableUser found")
265 output = (
"# db file automatically generated by %s on: %s\n"
266 "# No ConfigurableUser specialization in %s\n") % (parser.prog, time.asctime(), package_name)
268 logging.error(
"No ConfigurableUser specialization found")
272 output_dir = os.path.dirname(outputfile)
273 if not os.path.exists(output_dir):
274 logging.info(
"Creating directory %r", output_dir)
275 os.makedirs(output_dir, 0755)
278 logging.verbose(
"Writing confDb data to %r", outputfile)
279 open(outputfile,
"w").write(output)
282 if __name__ ==
'__main__':