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(
"GAUDI_PLUGIN_PATH",
"").split(os.pathsep) + os.getenv(
77 for path
in filter(os.path.isdir, pathlist):
81 path_join(path, f)
for f
in os.listdir(path)
if f.endswith(
".confdb")
85 ignored_files = set(os.environ.get(
"CONFIGURABLE_DB_IGNORE",
"").split(
","))
87 for confDb
in set(confDbFiles):
88 if confDb
in ignored_files
or (
90 and os.path.basename(confDb) == (
"{}.confdb".
format(project_name))
93 log.debug(
"\t-ignoring [%s]", confDb)
95 ignored_files.add(confDb)
97 log.debug(
"\t-loading [%s]...", confDb)
99 cfgDb._loadModule(confDb)
100 except Exception
as err:
103 log.warning(
"Could not load file [%s] !", confDb)
104 log.warning(
"Reason: %s", err)
107 os.environ[
"CONFIGURABLE_DB_IGNORE"] =
",".join(ignored_files)
113 Find in the module 'modulename' all the classes that derive from ConfigurableUser.
114 Return the list of the names.
115 The flag mayNotExist is used to choose the level of the logging message in case
116 the requested module does not exist.
119 oldpath = list(sys.path)
121 moduleelements = modulename.split(
".")
122 if len(moduleelements) > 1:
123 moddir = os.sep.join([root] + moduleelements[:-1])
127 shortmodname = moduleelements[-1]
129 if not os.path.exists(os.path.join(moddir, shortmodname) +
".py"):
130 msg =
"Module %s does not exist" % modulename
138 sys.path.insert(0, moddir)
139 logging.verbose(
"sys.path prepended with %r", sys.path[0])
141 logging.info(
"Looking for ConfigurableUser in %r", modulename)
144 logging.verbose(
"importing %s", shortmodname)
145 exec(
"import %s as mod" % shortmodname, g, l)
148 logging.verbose(
"restoring old sys.path")
151 if "__all__" in dir(mod)
and mod.__all__:
154 all = [n
for n
in dir(mod)
if not n.startswith(
"_")]
157 cfg = cfgDb.get(name)
158 if cfg
and cfg[
"module"] != modulename:
160 logging.verbose(
"Object %r already found in module %r", name, cfg[
"module"])
162 t = getattr(mod, name)
164 t, (
"ConfigurableUser",
"SuperAlgorithm")
167 logging.verbose(
"Found %r", result)
172 from optparse
import OptionParser
174 parser = OptionParser(
175 prog=os.path.basename(sys.argv[0]),
176 usage=
"%prog [options] <PackageName> [<Module1> ...]",
183 help=
"output file for confDb data [default = '../genConfDir/<PackageName>_user_confDb.py'].",
190 help=
"root directory of the python modules [default = '../python'].",
193 "-v",
"--verbose", action=
"store_true", help=
"print some debugging information"
196 "--debug", action=
"store_true", help=
"print more debugging information"
201 help=
"build directory where to look for .confdb files (search all subdirectories)",
206 help=
"name of the current project (used to exclude spurious versions of the .confdb file for the current project)",
208 parser.set_defaults(root=os.path.join(
"..",
"python"))
210 opts, args = parser.parse_args()
213 log_level = logging.DEBUG
215 log_level = logging.VERBOSE
217 log_level = logging.INFO
if os.environ.get(
"VERBOSE")
else logging.WARNING
219 format=
"%(levelname)s: %(message)s", stream=sys.stdout, level=log_level
223 parser.error(
"PackageName is required")
225 package_name = args.pop(0)
227 usingConvention =
False
230 args = [package_name +
".Configuration"]
231 usingConvention =
True
233 genConfDir = os.path.join(
"..", os.environ.get(
"CMTCONFIG",
""),
"genConfDir")
234 if not os.path.exists(genConfDir):
235 genConfDir = os.path.join(
"..",
"genConfDir")
238 outputfile = os.path.join(genConfDir, package_name +
"_user.confdb")
240 outputfile = opts.output
245 import Gaudi.Configurables
247 Gaudi.Configurables.ignoreMissingConfigurables =
True
256 sys.path.insert(0, genConfDir)
257 sys.path.insert(0, os.path.join(
"..",
"python"))
258 localConfDb = os.path.join(genConfDir, package_name, package_name +
".confdb")
259 if os.path.exists(localConfDb):
260 cfgDb._loadModule(localConfDb)
262 package_module = __import__(package_name)
263 package_module.__path__.insert(0, os.path.join(genConfDir, package_name))
277 "Cannot import module %r:\n%s", mod, traceback.format_exc().rstrip()
284 cfgDb.add(configurable=m, package=
"None", module=
"None", lib=
"None")
285 elif not usingConvention:
287 "Specified module %r does not contain ConfigurableUser specializations",
292 logging.info(
"ConfigurableUser found:\n%s", pformat(cus))
294 output =
"""## -*- ascii -*-
295 # db file automatically generated by %s on: %s
303 output +=
"%s %s %s\n" % (mod,
"None", cu)
306 output +=
"## %s\n" % package_name
307 elif usingConvention:
308 logging.info(
"No ConfigurableUser found")
310 "# db file automatically generated by %s on: %s\n"
311 "# No ConfigurableUser specialization in %s\n"
312 ) % (parser.prog, time.asctime(), package_name)
314 logging.error(
"No ConfigurableUser specialization found")
318 output_dir = os.path.dirname(outputfile)
320 logging.info(
"Creating directory %r", output_dir)
321 os.makedirs(output_dir, 0o755)
322 except OSError
as err:
325 if err.errno == errno.EEXIST:
332 logging.verbose(
"Writing confDb data to %r", outputfile)
333 open(outputfile,
"w").write(output)
337 if __name__ ==
"__main__":