00001
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
00022
00023
00024
00025
00026 parser.add_option("--ncpus", action="store", type="int", default=0,
00027 help="start the application in parallel mode using NCPUS processes. "
00028 "0 => serial mode (default), -1 => use all CPUs")
00029
00030 def option_cb(option, opt, value, parser):
00031 """Add the option line to a list together with its position in the
00032 argument list.
00033 """
00034 parser.values.options.append((len(parser.largs), value))
00035 parser.add_option("--option", action="callback", callback=option_cb,
00036 type = "string", nargs = 1,
00037 help="add a single line (Python) option to the configuration. "
00038 "All options lines are executed, one after the other, in "
00039 "the same context.")
00040 parser.add_option("--no-conf-user-apply", action="store_true",
00041 help="disable the automatic application of configurable "
00042 "users (for backward compatibility)")
00043 parser.add_option("-o", "--output", action = "store", type = "string",
00044 help ="dump the configuration to a file. The format of "
00045 "the options is determined by the extension of the "
00046 "file name: .pkl = pickle, .py = python, .opts = "
00047 "old style options. The python format cannot be "
00048 "used to run the application and it contains the "
00049 "same dictionary printed with -v")
00050 parser.add_option("--post-option", action="append", type="string",
00051 dest="post_options",
00052 help="Python options to be executed after the ConfigurableUser "
00053 "are applied. "
00054 "All options lines are executed, one after the other, in "
00055 "the same context.")
00056 parser.add_option("--debug", action="store_true",
00057 help="enable some debug print-out")
00058 parser.add_option("--printsequence", action="store_true",
00059 help="print the sequence")
00060 if not sys.platform.startswith("win"):
00061
00062 parser.add_option("-T", "--tcmalloc", action="store_true",
00063 help="Use the Google malloc replacement. The environment "
00064 "variable TCMALLOCLIB can be used to specify a different "
00065 "name for the library (the default is libtcmalloc.so)")
00066 parser.set_defaults(options = [],
00067 tcmalloc = False,
00068 ncpus = None)
00069
00070 opts, args = parser.parse_args()
00071
00072
00073
00074
00075 from commands import getstatusoutput as gso
00076 if opts.ncpus != None :
00077
00078 stat, out = gso('cat /proc/cpuinfo | grep processor | wc -l')
00079 if stat :
00080
00081 sys_cpus = 8
00082 else :
00083 sys_cpus = int(out)
00084 if opts.ncpus < -1 :
00085 s = "Invalid value : --ncpus must be integer >= -1"
00086 parser.error( s )
00087 if opts.ncpus > sys_cpus :
00088 s = "Invalid value : --ncpus : only %i cpus available"%(sys_cpus)
00089 parser.error( s )
00090 if opts.ncpus == 0 :
00091
00092 opts.ncpus = None
00093
00094
00095 import logging
00096 from GaudiKernel.ProcessJobOptions import InstallRootLoggingHandler
00097
00098 if opts.old_opts: prefix = "// "
00099 else: prefix = "# "
00100 level = logging.INFO
00101 if opts.debug:
00102 level = logging.DEBUG
00103 InstallRootLoggingHandler(prefix, level = level)
00104 root_logger = logging.getLogger()
00105
00106
00107 if opts.tcmalloc:
00108 libname = os.environ.get("TCMALLOCLIB", "libtcmalloc.so")
00109 preload = os.environ.get("LD_PRELOAD", "")
00110 if libname not in preload:
00111 if preload:
00112 preload += " "
00113 preload += libname
00114 os.environ["LD_PRELOAD"] = preload
00115 logging.info("Restarting with LD_PRELOAD='%s'", preload)
00116
00117 args = [ a for a in sys.argv if a != '-T' and not '--tcmalloc'.startswith(a) ]
00118 os.execv(sys.executable, [sys.executable] + args)
00119 else:
00120 logging.warning("Option --tcmalloc ignored because the library %s is "
00121 " already in LD_PRELOAD.", libname)
00122
00123 if opts.pickle_output:
00124 if opts.output:
00125 root_logger.error("Conflicting options: use only --pickle-output or --output")
00126 sys.exit(1)
00127 else:
00128 root_logger.warning("--pickle-output is deprecated, use --output instead")
00129 opts.output = opts.pickle_output
00130
00131 from Gaudi.Main import gaudimain
00132 c = gaudimain()
00133
00134
00135
00136
00137 options = [ "importOptions(%r)" % f for f in args ]
00138
00139
00140 optlines = list(opts.options)
00141 optlines.reverse()
00142 for pos, l in optlines:
00143 options.insert(pos,l)
00144
00145
00146 class FakeModule(object):
00147 def __init__(self, exception):
00148 self.exception = exception
00149 def __getattr__(self, *args, **kwargs):
00150 raise self.exception
00151 sys.modules["GaudiPython"] = FakeModule(RuntimeError("GaudiPython cannot be used in option files"))
00152
00153
00154 if options:
00155 g = {}
00156 l = {}
00157 exec "from Gaudi.Configuration import *" in g, l
00158 for o in options:
00159 logging.debug(o)
00160 exec o in g, l
00161
00162 import GaudiKernel.Proxy.Configurable
00163 if opts.no_conf_user_apply:
00164 logging.info("Disabling automatic apply of ConfigurableUser")
00165
00166 GaudiKernel.Proxy.Configurable._appliedConfigurableUsers_ = True
00167
00168
00169 from GaudiKernel.Proxy.Configurable import applyConfigurableUsers
00170 applyConfigurableUsers()
00171
00172
00173 if opts.post_options:
00174 g = {}
00175 l = {}
00176 exec "from Gaudi.Configuration import *" in g, l
00177 for o in opts.post_options:
00178 logging.debug(o)
00179 exec o in g, l
00180
00181 if opts.verbose:
00182 c.printconfig(opts.old_opts, opts.all_opts)
00183 if opts.output:
00184 c.writeconfig(opts.output, opts.all_opts)
00185 if opts.printsequence:
00186 c.printsequence()
00187
00188
00189 del sys.modules["GaudiPython"]
00190
00191 if not opts.dry_run:
00192
00193 sys.exit(c.run(opts.ncpus))