172    from optparse import OptionParser
  173 
  174    parser = OptionParser(
  175        prog=os.path.basename(sys.argv[0]),
  176        usage="%prog [options] <PackageName> [<Module1> ...]",
  177    )
  178    parser.add_option(
  179        "-o",
  180        "--output",
  181        action="store",
  182        type="string",
  183        help="output file for confDb data [default = '../genConfDir/<PackageName>_user_confDb.py'].",
  184    )
  185    parser.add_option(
  186        "-r",
  187        "--root",
  188        action="store",
  189        type="string",
  190        help="root directory of the python modules [default = '../python'].",
  191    )
  192    parser.add_option(
  193        "-v", "--verbose", action="store_true", help="print some debugging information"
  194    )
  195    parser.add_option(
  196        "--debug", action="store_true", help="print more debugging information"
  197    )
  198    parser.add_option(
  199        "--build-dir",
  200        action="store",
  201        help="build directory where to look for .confdb files (search all subdirectories)",
  202    )
  203    parser.add_option(
  204        "--project-name",
  205        action="store",
  206        help="name of the current project (used to exclude spurious versions of the .confdb file for the current project)",
  207    )
  208    parser.set_defaults(root=os.path.join("..", "python"))
  209 
  210    opts, args = parser.parse_args()
  211 
  212    if opts.debug:
  213        log_level = logging.DEBUG
  214    elif opts.verbose:
  215        log_level = logging.VERBOSE
  216    else:
  217        log_level = logging.INFO if os.environ.get("VERBOSE") else logging.WARNING
  218    logging.basicConfig(
  219        format="%(levelname)s: %(message)s", stream=sys.stdout, level=log_level
  220    )
  221 
  222    if len(args) < 1:
  223        parser.error("PackageName is required")
  224 
  225    package_name = args.pop(0)
  226 
  227    usingConvention = False
  228    if not args:
  229        
  230        args = [package_name + ".Configuration"]
  231        usingConvention = True
  232 
  233    genConfDir = os.path.join("..", os.environ.get("CMTCONFIG", ""), "genConfDir")
  234    if not os.path.exists(genConfDir):
  235        genConfDir = os.path.join("..", "genConfDir")
  236 
  237    if not opts.output:
  238        outputfile = os.path.join(genConfDir, package_name + "_user.confdb")
  239    else:
  240        outputfile = opts.output
  241 
  242    
  243    
  244    try:
  245        import Gaudi.Configurables
  246 
  247        Gaudi.Configurables.ignoreMissingConfigurables = True
  248    except ImportError:
  249        pass
  250    
  251    loadConfigurableDb(opts.build_dir, opts.project_name)
  252    
  253    try:
  254        
  255        
  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)
  261            
  262            package_module = __import__(package_name)
  263            package_module.__path__.insert(0, os.path.join(genConfDir, package_name))
  264    except Exception:
  265        pass  
  266 
  267    
  268    cus = {}
  269    for mod in args:
  270        lst = None
  271        try:
  272            lst = getConfigurableUsers(mod, root=opts.root, mayNotExist=usingConvention)
  273        except ImportError:
  274            import traceback
  275 
  276            logging.error(
  277                "Cannot import module %r:\n%s", mod, traceback.format_exc().rstrip()
  278            )  
  279            return 2
  280        if lst:
  281            cus[mod] = lst
  282            
  283            for m in lst:
  284                cfgDb.add(configurable=m, package="None", module="None", lib="None")
  285        elif not usingConvention:
  286            logging.warning(
  287                "Specified module %r does not contain ConfigurableUser specializations",
  288                mod,
  289            )
  290 
  291    if cus:
  292        logging.info("ConfigurableUser found:\n%s", pformat(cus))
  293        
  294        output = """##  -*- ascii -*-
  295# db file automatically generated by %s on: %s
  296""" % (
  297            parser.prog,
  298            time.asctime(),
  299        )
  300 
  301        for mod in cus:
  302            for cu in cus[mod]:
  303                output += "%s %s %s\n" % (mod, "None", cu)
  304 
  305        
  306        output += "## %s\n" % package_name
  307    elif usingConvention:
  308        logging.info("No ConfigurableUser found")
  309        output = (
  310            "# db file automatically generated by %s on: %s\n"
  311            "# No ConfigurableUser specialization in %s\n"
  312        ) % (parser.prog, time.asctime(), package_name)
  313    else:
  314        logging.error("No ConfigurableUser specialization found")
  315        return 1
  316 
  317    
  318    output_dir = os.path.dirname(outputfile)
  319    try:
  320        logging.info("Creating directory %r", output_dir)
  321        os.makedirs(output_dir, 0o755)
  322    except OSError as err:
  323        import errno
  324 
  325        if err.errno == errno.EEXIST:
  326            
  327            pass
  328        else:
  329            raise
  330 
  331    
  332    logging.verbose("Writing confDb data to %r", outputfile)
  333    open(outputfile, "w").write(output)
  334    return 0
  335 
  336