4 if __name__ ==
"__main__":
6 from optparse
import OptionParser
7 parser = OptionParser(usage =
"%prog [options] <opts_file> ...")
8 parser.add_option(
"-n",
"--dry-run", action=
"store_true",
9 help=
"do not run the application, just parse option files")
10 parser.add_option(
"-p",
"--pickle-output", action=
"store", type=
"string",
12 help=
"DEPRECATED: use '--output file.pkl' instead. Write "
13 "the parsed options as a pickle file (static option "
15 parser.add_option(
"-v",
"--verbose", action=
"store_true",
16 help=
"print the parsed options")
17 parser.add_option(
"--old-opts", action=
"store_true",
18 help=
"format printed options in old option files style")
19 parser.add_option(
"--all-opts", action=
"store_true",
20 help=
"print all the option (even if equal to default)")
26 parser.add_option(
"--ncpus", action=
"store", type=
"int", default=0,
27 help=
"start the application in parallel mode using NCPUS processes. "
28 "0 => serial mode (default), -1 => use all CPUs")
31 """Add the option line to a list together with its position in the
34 parser.values.options.append((len(parser.largs), value))
35 parser.add_option(
"--option", action=
"callback", callback=option_cb,
36 type =
"string", nargs = 1,
37 help=
"add a single line (Python) option to the configuration. "
38 "All options lines are executed, one after the other, in "
40 parser.add_option(
"--no-conf-user-apply", action=
"store_true",
41 help=
"disable the automatic application of configurable "
42 "users (for backward compatibility)")
43 parser.add_option(
"-o",
"--output", action =
"store", type =
"string",
44 help =
"dump the configuration to a file. The format of "
45 "the options is determined by the extension of the "
46 "file name: .pkl = pickle, .py = python, .opts = "
47 "old style options. The python format cannot be "
48 "used to run the application and it contains the "
49 "same dictionary printed with -v")
50 parser.add_option(
"--post-option", action=
"append", type=
"string",
52 help=
"Python options to be executed after the ConfigurableUser "
54 "All options lines are executed, one after the other, in "
56 parser.add_option(
"--debug", action=
"store_true",
57 help=
"enable some debug print-out")
58 parser.add_option(
"--printsequence", action=
"store_true",
59 help=
"print the sequence")
60 if not sys.platform.startswith(
"win"):
62 parser.add_option(
"-T",
"--tcmalloc", action=
"store_true",
63 help=
"Use the Google malloc replacement. The environment "
64 "variable TCMALLOCLIB can be used to specify a different "
65 "name for the library (the default is libtcmalloc.so)")
66 parser.add_option(
"--preload", action=
"append",
67 help=
"Allow pre-loading of special libraries (e.g. Google "
68 "profiling libraries).")
69 parser.set_defaults(options = [],
74 opts, args = parser.parse_args()
80 from multiprocessing
import cpu_count
81 sys_cpus = cpu_count()
82 if opts.ncpus > sys_cpus:
83 s =
"Invalid value : --ncpus : only %i cpus available" % sys_cpus
85 elif opts.ncpus < -1 :
86 s =
"Invalid value : --ncpus must be integer >= -1"
96 if opts.old_opts: prefix =
"// "
100 level = logging.DEBUG
102 root_logger = logging.getLogger()
106 opts.preload.insert(0, os.environ.get(
"TCMALLOCLIB",
"libtcmalloc.so"))
109 preload = os.environ.get(
"LD_PRELOAD",
"")
111 preload = preload.replace(
" ",
":").split(
":")
114 for libname
in set(preload).intersection(opts.preload):
115 logging.warning(
"Ignoring preload of library %s because it is "
116 "already in LD_PRELOAD.", libname)
118 for libname
in opts.preload
119 if libname
not in set(preload)]
122 preload =
":".join(preload)
123 os.environ[
"LD_PRELOAD"] = preload
124 logging.info(
"Restarting with LD_PRELOAD='%s'", preload)
127 args = [ a
for a
in sys.argv
if a !=
'-T' and not '--tcmalloc'.startswith(a) ]
128 os.execv(sys.executable, [sys.executable] + args)
130 if opts.pickle_output:
132 root_logger.error(
"Conflicting options: use only --pickle-output or --output")
135 root_logger.warning(
"--pickle-output is deprecated, use --output instead")
136 opts.output = opts.pickle_output
144 options = [
"importOptions(%r)" % f
for f
in args ]
147 optlines = list(opts.options)
149 for pos, l
in optlines:
150 options.insert(pos,l)
158 sys.modules[
"GaudiPython"] =
FakeModule(RuntimeError(
"GaudiPython cannot be used in option files"))
164 exec
"from Gaudi.Configuration import *" in g, l
169 import GaudiKernel.Proxy.Configurable
170 if opts.no_conf_user_apply:
171 logging.info(
"Disabling automatic apply of ConfigurableUser")
173 GaudiKernel.Proxy.Configurable._appliedConfigurableUsers_ =
True
176 from GaudiKernel.Proxy.Configurable
import applyConfigurableUsers
180 if opts.post_options:
183 exec
"from Gaudi.Configuration import *" in g, l
184 for o
in opts.post_options:
189 c.printconfig(opts.old_opts, opts.all_opts)
191 c.writeconfig(opts.output, opts.all_opts)
193 c.printsequence = opts.printsequence
194 if opts.printsequence:
196 logging.warning(
"--printsequence not supported with --ncpus: ignored")
198 logging.warning(
"--printsequence not supported with --dry-run: ignored")
201 del sys.modules[
"GaudiPython"]
205 sys.exit(c.run(opts.ncpus))