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.add_option(
"--lockerpath", action=
"store",
138 help=
"directory where to find the module 'locker'")
139 parser.set_defaults(root = os.path.join(
"..",
"python"))
141 opts, args = parser.parse_args()
144 log_level = logging.DEBUG
146 log_level = logging.VERBOSE
148 log_level = logging.INFO
149 logging.basicConfig(format =
"%(levelname)s: %(message)s",
154 parser.error(
"PackageName is required")
156 package_name = args.pop(0)
158 usingConvention =
False
161 args = [package_name +
".Configuration"]
162 usingConvention =
True
164 genConfDir = os.path.join(
"..", os.environ.get(
"CMTCONFIG",
""),
"genConf")
165 if not os.path.exists(genConfDir):
166 genConfDir = os.path.join(
"..",
"genConf")
169 outputfile = os.path.join(genConfDir, package_name +
'_user.confdb')
171 outputfile = opts.output
177 if "GAUDI_BUILD_LOCK" in os.environ:
179 sys.path.append(opts.lockerpath)
183 from locker
import LockFile
185 def LockFile(*args, **kwargs):
188 dbLock = LockFile(os.environ[
"GAUDI_BUILD_LOCK"], temporary =
True)
193 import Gaudi.Configurables
194 Gaudi.Configurables.ignoreMissingConfigurables =
True
203 sys.path.insert(0, genConfDir)
204 sys.path.insert(0, os.path.join(
"..",
"python"))
205 localConfDb = os.path.join(genConfDir, package_name, package_name +
'.confdb')
206 if os.path.exists(localConfDb):
207 cfgDb._loadModule(localConfDb)
209 package_module = __import__(package_name)
210 package_module.__path__.insert(0, os.path.join(genConfDir, package_name))
223 logging.error(
"Cannot import module %r:\n%s", mod,
224 traceback.format_exc().rstrip())
230 cfgDb.add(configurable = m,
234 elif not usingConvention:
235 logging.warning(
"Specified module %r does not contain ConfigurableUser specializations", mod)
238 logging.info(
"ConfigurableUser found:\n%s", pformat(cus))
240 output =
"""## -*- ascii -*-
241 # db file automatically generated by %s on: %s
242 """ % (parser.prog, time.asctime())
246 output +=
"%s %s %s\n" % (mod,
'None', cu)
249 output +=
"## %s\n" % package_name
250 elif usingConvention:
251 logging.info(
"No ConfigurableUser found")
252 output = (
"# db file automatically generated by %s on: %s\n"
253 "# No ConfigurableUser specialization in %s\n") % (parser.prog, time.asctime(), package_name)
255 logging.error(
"No ConfigurableUser specialization found")
259 output_dir = os.path.dirname(outputfile)
261 logging.info(
"Creating directory %r", output_dir)
262 os.makedirs(output_dir, 0755)
265 if err.errno == errno.EEXIST:
272 logging.verbose(
"Writing confDb data to %r", outputfile)
273 open(outputfile,
"w").write(output)
276 if __name__ ==
'__main__':
def loadConfigurableDb
Helper function to load all ConfigurableDb files holding informations.