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)
    25     Check if any of the class names in 'basenames' is anywhere in the base    26     classes of the class 'derived'.    27     If 'derived' _is_ one in 'basenames', returns False.    29     'basenames' can be a string or an iterable (of strings).    31     if isinstance(basenames, basestring):
    32         basenames = (basenames,)
    33     for b 
in derived.__bases__:
    34         if b.__name__ 
in basenames:
    44     Equivalent to GaudiKernel.ConfigurableDb.loadConfigurableDb(), but does a    45     deep search and executes the '*.confdb' files instead of importing them.    47     log = GaudiKernel.ConfigurableDb.log
    48     from os.path 
import join 
as path_join
    53         confDbFiles += [f 
for f 
in glob(path_join(path, 
'*', 
'*.confdb'))
    56     pathlist = os.getenv(
"LD_LIBRARY_PATH", 
"").split(os.pathsep)
    57     for path 
in filter(os.path.isdir, pathlist):
    58         confDbFiles += [f 
for f 
in [path_join(path, f) 
for f 
in os.listdir(path)
    59                                     if f.endswith(
'.confdb')]]
    61     for confDb 
in confDbFiles:
    62         log.debug(
"\t-loading [%s]..." % confDb)
    64             cfgDb._loadModule(confDb)
    65         except Exception, err:
    68             log.warning(
"Could not load file [%s] !", confDb)
    69             log.warning(
"Reason: %s", err)
    76     Find in the module 'modulename' all the classes that derive from ConfigurableUser.    77     Return the list of the names.    78     The flag mayNotExist is used to choose the level of the logging message in case    79     the requested module does not exist.    82     oldpath = list(sys.path)
    84     moduleelements = modulename.split(
'.')
    85     if len(moduleelements) > 1:
    86         moddir = os.sep.join([root] + moduleelements[:-1])
    90     shortmodname = moduleelements[-1]
    92     if not os.path.exists(os.path.join(moddir, shortmodname) + 
".py"):
    93         msg = 
"Module %s does not exist" % modulename
   101     sys.path.insert(0, moddir)
   102     logging.verbose(
"sys.path prepended with %r", sys.path[0])
   104     logging.info(
"Looking for ConfigurableUser in %r", modulename)
   107         logging.verbose(
"importing %s", shortmodname)
   108         exec 
"import %s as mod" % shortmodname 
in g, l
   111         logging.verbose(
"restoring old sys.path")
   114     if "__all__" in dir(mod) 
and mod.__all__:
   117         all = [n 
for n 
in dir(mod) 
if not n.startswith(
"_")]
   120         cfg = cfgDb.get(name)
   121         if cfg 
and cfg[
"module"] != modulename:
   124                 "Object %r already found in module %r", name, cfg[
"module"])
   126         t = getattr(mod, name)
   127         if isinstance(t, type) 
and _inheritsfrom(t, (
'ConfigurableUser',
   130     logging.verbose(
"Found %r", result)
   135     from optparse 
import OptionParser
   136     parser = OptionParser(prog=os.path.basename(sys.argv[0]),
   137                           usage=
"%prog [options] <PackageName> [<Module1> ...]")
   138     parser.add_option(
"-o", 
"--output", action=
"store", type=
"string",
   139                       help=
"output file for confDb data [default = '../genConf/<PackageName>_user_confDb.py'].")
   140     parser.add_option(
"-r", 
"--root", action=
"store", type=
"string",
   141                       help=
"root directory of the python modules [default = '../python'].")
   142     parser.add_option(
"-v", 
"--verbose", action=
"store_true",
   143                       help=
"print some debugging information")
   144     parser.add_option(
"--debug", action=
"store_true",
   145                       help=
"print more debugging information")
   146     parser.set_defaults(root=os.path.join(
"..", 
"python"))
   148     opts, args = parser.parse_args()
   151         log_level = logging.DEBUG
   153         log_level = logging.VERBOSE
   155         log_level = logging.INFO 
if os.environ.get(
   156             'VERBOSE') 
else logging.WARNING
   157     logging.basicConfig(format=
"%(levelname)s: %(message)s",
   162         parser.error(
"PackageName is required")
   164     package_name = args.pop(0)
   166     usingConvention = 
False   169         args = [package_name + 
".Configuration"]
   170         usingConvention = 
True   172     genConfDir = os.path.join(
"..", os.environ.get(
"CMTCONFIG", 
""), 
"genConf")
   173     if not os.path.exists(genConfDir):
   174         genConfDir = os.path.join(
"..", 
"genConf")
   177         outputfile = os.path.join(genConfDir, package_name + 
'_user.confdb')
   179         outputfile = opts.output
   184         import Gaudi.Configurables
   185         Gaudi.Configurables.ignoreMissingConfigurables = 
True   194         sys.path.insert(0, genConfDir)
   195         sys.path.insert(0, os.path.join(
"..", 
"python"))
   196         localConfDb = os.path.join(
   197             genConfDir, package_name, package_name + 
'.confdb')
   198         if os.path.exists(localConfDb):
   199             cfgDb._loadModule(localConfDb)
   201             package_module = __import__(package_name)
   202             package_module.__path__.insert(
   203                 0, os.path.join(genConfDir, package_name))
   213                 mod, root=opts.root, mayNotExist=usingConvention)
   216             logging.error(
"Cannot import module %r:\n%s", mod,
   217                           traceback.format_exc().rstrip())  
   223                 cfgDb.add(configurable=m,
   227         elif not usingConvention:
   229                 "Specified module %r does not contain ConfigurableUser specializations", mod)
   232         logging.info(
"ConfigurableUser found:\n%s", pformat(cus))
   234         output = 
"""##  -*- ascii -*-   235 # db file automatically generated by %s on: %s   236 """ % (parser.prog, time.asctime())
   240                 output += 
"%s %s %s\n" % (mod, 
'None', cu)
   243         output += 
"## %s\n" % package_name
   244     elif usingConvention:
   245         logging.info(
"No ConfigurableUser found")
   246         output = (
"# db file automatically generated by %s on: %s\n"   247                   "# No ConfigurableUser specialization in %s\n") % (parser.prog, time.asctime(), package_name)
   249         logging.error(
"No ConfigurableUser specialization found")
   253     output_dir = os.path.dirname(outputfile)
   255         logging.info(
"Creating directory %r", output_dir)
   256         os.makedirs(output_dir, 0755)
   259         if err.errno == errno.EEXIST:
   266     logging.verbose(
"Writing confDb data to %r", outputfile)
   267     open(outputfile, 
"w").
write(output)
   271 if __name__ == 
'__main__':
 
def getConfigurableUsers(modulename, root, mayNotExist=False)
def _inheritsfrom(derived, basenames)