| # Gaudi Framework, version v21r6 |

Home |  | Generated: 11 Nov 2009

# gaudirun.py

Go to the documentation of this file.

    00001 #!/usr/bin/env python
    00002
    00003 #---------------------------------------------------------------------
    00004 if __name__ == "__main__":
    00005     import os, sys
    00006     from optparse import OptionParser
    00007     parser = OptionParser(usage = "%prog [options] <opts_file> ...")
    00008     parser.add_option("-n","--dry-run", action="store_true",
    00009                       help="do not run the application, just parse option files")
    00010     parser.add_option("-p","--pickle-output", action="store", type="string",
    00011                       metavar = "FILE",
    00012                       help="DEPRECATED: use '--output file.pkl' instead. Write "
    00013                            "the parsed options as a pickle file (static option "
    00014                            "file)")
    00015     parser.add_option("-v","--verbose", action="store_true",
    00016                       help="print the parsed options")
    00017     parser.add_option("--old-opts", action="store_true",
    00018                       help="format printed options in old option files style")
    00019     parser.add_option("--all-opts", action="store_true",
    00020                       help="print all the option (even if equal to default)")
    00021     parser.add_option("--parallel", action="store", dest="ncpus", type="int",
    00022                       help="start the application in parallel mode using NCPUS processes")
    00023
    00024     def option_cb(option, opt, value, parser):
    00025         """Add the option line to a list together with its position in the
    00026         argument list.
    00027         """
    00028         parser.values.options.append((len(parser.largs), value))
    00029     parser.add_option("--option", action="callback", callback=option_cb,
    00030                       type = "string", nargs = 1,
    00031                       help="add a single line (Python) option to the configuration." +
    00032                            "All options lines are executed, one after the other, in " +
    00033                            "the same context.")
    00034     parser.add_option("--no-conf-user-apply", action="store_true",
    00035                       help="disable the automatic application of configurable "
    00036                            "users (for backward compatibility)")
    00037     parser.add_option("-o", "--output", action = "store", type = "string",
    00038                       help ="dump the configuration to a file. The format of "
    00039                             "the options is determined by the extension of the "
    00040                             "file name: .pkl = pickle, .py = python, .opts = "
    00041                             "old style options. The python format cannot be "
    00042                             "used to run the application and it contains the "
    00043                             "same dictionary printed with -v")
    00044     parser.add_option("--post-option", action="append", type="string",
    00045                       dest="post_options",
    00046                       help="Python options to be executed after the ConfigurableUser "
    00047                            "are applied."
    00048                            "All options lines are executed, one after the other, in " +
    00049                            "the same context.")
    00050     parser.add_option("--debug", action="store_true",
    00051                       help="enable some debug print-out")
    00052     parser.add_option("--printsequence", action="store_true",
    00053                       help="print the sequence")
    00054     if not sys.platform.startswith("win"):
    00055         # This option can be used only on unix platforms
    00056         parser.add_option("-T", "--tcmalloc", action="store_true",
    00057                           help="Use the Google malloc replacement. The environment "
    00058                                "variable TCMALLOCLIB can be used to specify a different "
    00059                                "name for the library (the default is libtcmalloc.so)")
    00060     parser.set_defaults(options = [],
    00061                         tcmalloc = False,
    00062                         ncpus = None)
    00063
    00064     opts, args = parser.parse_args()
    00065
    00066     # Check consistency of options
    00067     if opts.ncpus is not None and opts.ncpus < 1:
    00068         parser.error("Invalid value for --parallel: must be >= 1")
    00069
    00070     # configure the logging
    00071     import logging
    00072     from GaudiKernel.ProcessJobOptions import InstallRootLoggingHandler
    00073
    00074     if opts.old_opts: prefix = "// "
    00075     else: prefix = "# "
    00076     level = logging.INFO
    00077     if opts.debug:
    00078         level = logging.DEBUG
    00079     InstallRootLoggingHandler(prefix, level = level)
    00080     root_logger = logging.getLogger()
    00081
    00082     # tcmalloc support
    00083     if opts.tcmalloc:
    00084         libname = os.environ.get("TCMALLOCLIB", "libtcmalloc.so")
    00085         preload = os.environ.get("LD_PRELOAD", "")
    00086         if libname not in preload:
    00087             if preload:
    00088                 preload += " "
    00089             preload += libname
    00090             os.environ["LD_PRELOAD"] = preload
    00091             logging.info("Restarting with LD_PRELOAD='%s'", preload)
    00092             # remove the --tcmalloc option from the arguments
    00093             args = [ a for a in sys.argv if a != '-T' and not '--tcmalloc'.startswith(a) ]
    00094             os.execv(sys.executable, [sys.executable] + args)
    00095         else:
    00096             logging.warning("Option --tcmalloc ignored because the library %s is "
    00097                             " already in LD_PRELOAD.", libname)
    00098
    00099     if opts.pickle_output:
    00100         if opts.output:
    00101             root_logger.error("Conflicting options: use only --pickle-output or --output")
    00102             sys.exit(1)
    00103         else:
    00104             root_logger.warning("--pickle-output is deprecated, use --output instead")
    00105             opts.output = opts.pickle_output
    00106
    00107     from Gaudi.Main import gaudimain
    00108     c = gaudimain()
    00109
    00110     # Prepare the "configuration script" to parse (like this it is easier than
    00111     # having a list with files and python commands, with an if statements that
    00112     # decides to do importOptions or exec)
    00113     options = [ "importOptions(%r)" % f for f in args ]
    00114     # The option lines are inserted into the list of commands using their
    00115     # position on the command line
    00116     optlines = list(opts.options)
    00117     optlines.reverse() # this allows to avoid to have to care about corrections of the positions
    00118     for pos, l in optlines:
    00119         options.insert(pos,l)
    00120
    00121     # "execute" the configuration script generated (if any)
    00122     if options:
    00123         g = {}
    00124         l = {}
    00125         exec "from Gaudi.Configuration import *" in g, l
    00126         for o in options:
    00127             logging.debug(o)
    00128             exec o in g, l
    00129
    00130     import GaudiKernel.Proxy.Configurable
    00131     if opts.no_conf_user_apply:
    00132         logging.info("Disabling automatic apply of ConfigurableUser")
    00133         # pretend that they have been already applied
    00134         GaudiKernel.Proxy.Configurable._appliedConfigurableUsers_ = True
    00135
    00136     # This need to be done before dumping
    00137     from GaudiKernel.Proxy.Configurable import applyConfigurableUsers
    00138     applyConfigurableUsers()
    00139
    00140     # Options to be processed after applyConfigurableUsers
    00141     if opts.post_options:
    00142         g = {}
    00143         l = {}
    00144         exec "from Gaudi.Configuration import *" in g, l
    00145         for o in opts.post_options:
    00146             logging.debug(o)
    00147             exec o in g, l
    00148
    00149     if opts.verbose:
    00150         c.printconfig(opts.old_opts, opts.all_opts)
    00151     if opts.output:
    00152         c.writeconfig(opts.output, opts.all_opts)
    00153     if opts.printsequence:
    00154         c.printsequence()
    00155
    00156     if not opts.dry_run:
    00157         # Do the real processing
    00158         sys.exit(c.run(opts.ncpus))