13 Generate _confDb.py files for ConfigurableUser classes.
21 from pprint
import pformat
26 logging.VERBOSE = (logging.INFO + logging.DEBUG) // 2
27 logging.addLevelName(logging.VERBOSE,
"VERBOSE")
28 logging.verbose =
lambda msg, *args, **kwargs: logging.log(
29 logging.VERBOSE, msg, *args, **kwargs
35 Check if any of the class names in 'basenames' is anywhere in the base
36 classes of the class 'derived'.
37 If 'derived' _is_ one in 'basenames', returns False.
39 'basenames' can be a string or an iterable (of strings).
41 if isinstance(basenames, str):
42 basenames = (basenames,)
43 for b
in derived.__bases__:
44 if b.__name__
in basenames:
54 Equivalent to GaudiKernel.ConfigurableDb.loadConfigurableDb(), but does a
55 deep search and executes the '*.confdb' files instead of importing them.
57 log = GaudiKernel.ConfigurableDb.log
58 from os.path
import join
as path_join
65 f
for f
in glob(path_join(path,
"*",
"*.confdb"))
if os.path.isfile(f)
69 for root, _, files
in os.walk(build_dir):
71 os.path.join(root, f)
for f
in files
if f.endswith(
".confdb")
74 pathlist = os.getenv(
"LD_LIBRARY_PATH",
"").split(os.pathsep)
75 for path
in filter(os.path.isdir, pathlist):
79 path_join(path, f)
for f
in os.listdir(path)
if f.endswith(
".confdb")
83 ignored_files = set(os.environ.get(
"CONFIGURABLE_DB_IGNORE",
"").split(
","))
85 for confDb
in set(confDbFiles):
86 if confDb
in ignored_files
or (
88 and os.path.basename(confDb) == (
"{}.confdb".
format(project_name))
91 log.debug(
"\t-ignoring [%s]", confDb)
93 ignored_files.add(confDb)
95 log.debug(
"\t-loading [%s]...", confDb)
97 cfgDb._loadModule(confDb)
98 except Exception
as err:
101 log.warning(
"Could not load file [%s] !", confDb)
102 log.warning(
"Reason: %s", err)
105 os.environ[
"CONFIGURABLE_DB_IGNORE"] =
",".join(ignored_files)
111 Find in the module 'modulename' all the classes that derive from ConfigurableUser.
112 Return the list of the names.
113 The flag mayNotExist is used to choose the level of the logging message in case
114 the requested module does not exist.
117 oldpath = list(sys.path)
119 moduleelements = modulename.split(
".")
120 if len(moduleelements) > 1:
121 moddir = os.sep.join([root] + moduleelements[:-1])
125 shortmodname = moduleelements[-1]
127 if not os.path.exists(os.path.join(moddir, shortmodname) +
".py"):
128 msg =
"Module %s does not exist" % modulename
136 sys.path.insert(0, moddir)
137 logging.verbose(
"sys.path prepended with %r", sys.path[0])
139 logging.info(
"Looking for ConfigurableUser in %r", modulename)
142 logging.verbose(
"importing %s", shortmodname)
143 exec(
"import %s as mod" % shortmodname, g, l)
146 logging.verbose(
"restoring old sys.path")
149 if "__all__" in dir(mod)
and mod.__all__:
152 all = [n
for n
in dir(mod)
if not n.startswith(
"_")]
155 cfg = cfgDb.get(name)
156 if cfg
and cfg[
"module"] != modulename:
158 logging.verbose(
"Object %r already found in module %r", name, cfg[
"module"])
160 t = getattr(mod, name)
162 t, (
"ConfigurableUser",
"SuperAlgorithm")
165 logging.verbose(
"Found %r", result)
170 from optparse
import OptionParser
172 parser = OptionParser(
173 prog=os.path.basename(sys.argv[0]),
174 usage=
"%prog [options] <PackageName> [<Module1> ...]",
181 help=
"output file for confDb data [default = '../genConfDir/<PackageName>_user_confDb.py'].",
188 help=
"root directory of the python modules [default = '../python'].",
191 "-v",
"--verbose", action=
"store_true", help=
"print some debugging information"
194 "--debug", action=
"store_true", help=
"print more debugging information"
199 help=
"build directory where to look for .confdb files (search all subdirectories)",
204 help=
"name of the current project (used to exclude spurious versions of the .confdb file for the current project)",
206 parser.set_defaults(root=os.path.join(
"..",
"python"))
208 opts, args = parser.parse_args()
211 log_level = logging.DEBUG
213 log_level = logging.VERBOSE
215 log_level = logging.INFO
if os.environ.get(
"VERBOSE")
else logging.WARNING
217 format=
"%(levelname)s: %(message)s", stream=sys.stdout, level=log_level
221 parser.error(
"PackageName is required")
223 package_name = args.pop(0)
225 usingConvention =
False
228 args = [package_name +
".Configuration"]
229 usingConvention =
True
231 genConfDir = os.path.join(
"..", os.environ.get(
"CMTCONFIG",
""),
"genConfDir")
232 if not os.path.exists(genConfDir):
233 genConfDir = os.path.join(
"..",
"genConfDir")
236 outputfile = os.path.join(genConfDir, package_name +
"_user.confdb")
238 outputfile = opts.output
243 import Gaudi.Configurables
245 Gaudi.Configurables.ignoreMissingConfigurables =
True
254 sys.path.insert(0, genConfDir)
255 sys.path.insert(0, os.path.join(
"..",
"python"))
256 localConfDb = os.path.join(genConfDir, package_name, package_name +
".confdb")
257 if os.path.exists(localConfDb):
258 cfgDb._loadModule(localConfDb)
260 package_module = __import__(package_name)
261 package_module.__path__.insert(0, os.path.join(genConfDir, package_name))
275 "Cannot import module %r:\n%s", mod, traceback.format_exc().rstrip()
282 cfgDb.add(configurable=m, package=
"None", module=
"None", lib=
"None")
283 elif not usingConvention:
285 "Specified module %r does not contain ConfigurableUser specializations",
290 logging.info(
"ConfigurableUser found:\n%s", pformat(cus))
292 output =
"""## -*- ascii -*-
293 # db file automatically generated by %s on: %s
301 output +=
"%s %s %s\n" % (mod,
"None", cu)
304 output +=
"## %s\n" % package_name
305 elif usingConvention:
306 logging.info(
"No ConfigurableUser found")
308 "# db file automatically generated by %s on: %s\n"
309 "# No ConfigurableUser specialization in %s\n"
310 ) % (parser.prog, time.asctime(), package_name)
312 logging.error(
"No ConfigurableUser specialization found")
316 output_dir = os.path.dirname(outputfile)
318 logging.info(
"Creating directory %r", output_dir)
319 os.makedirs(output_dir, 0o755)
320 except OSError
as err:
323 if err.errno == errno.EEXIST:
330 logging.verbose(
"Writing confDb data to %r", outputfile)
331 open(outputfile,
"w").write(output)
335 if __name__ ==
"__main__":