ATLAS Offline Software
Loading...
Searching...
No Matches
athena.py
Go to the documentation of this file.
1#!/bin/sh
2# Emacs, this is mostly -*-Python-*-
3#
4# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5#
6# athena.py is born as shell script to preload some optional libraries.
7#
8"""date"
9
10# defaults
11export USETCMALLOC=0
12export USEJEMALLOC=0
13export USEIMF=0
14export USEEXCTRACE=0
15export USEEXCABORT=1
16otherargs=()
17# but use tcmalloc by default if TCMALLOCDIR is defined
18if [ -n "$TCMALLOCDIR" ]; then
19 export USETCMALLOC=1
20fi
21
22# parse LD_PRELOAD related command line arguments
23for a in "$@"
24do
25 case "$a" in
26 --stdcmalloc) USETCMALLOC=0;USEJEMALLOC=0;;
27 --tcmalloc) USETCMALLOC=1;USEJEMALLOC=0;;
28 --jemalloc) USETCMALLOC=0;USEJEMALLOC=1;;
29 --stdcmath) USEIMF=0;;
30 --imf) USEIMF=1;;
31 --exctrace) USEEXCTRACE=1;;
32 --no-excabort) USEEXCABORT=0;;
33 --preloadlib*) export ATHENA_ADD_PRELOAD=${a#*=};;
34 --drop-and-reload) ATHENA_DROP_RELOAD=1;;
35 *) otherargs+=("$a");;
36 esac
37done
38
39
40# Do the actual preloading via LD_PRELOAD and save the original value
41export LD_PRELOAD_ORIG=${LD_PRELOAD}
42source `which athena_preload.sh `
43
44# Now resurrect ourselves as python script
45python_path=`which python`
46"exec" "$python_path" "-tt" "$0" "$@";
47
48"""
49
50# File: athena.py
51# Author: Wim Lavrijsen (WLavrijsen@lbl.gov)
52# "
53# This script allows you to run Athena from python.
54#
55# Debugging is supported with the '-d' option (hook debugger after running
56# all user scripts, and just before calling initialize) and the --debug
57# option (requires "conf", "init", or "exec" and will hook just before that
58# stage). The hook will give you the gdb prompt, from where you can set
59# break points, load additional shared libraries, or drop into interactive
60# athena mode (if -i specified on the cli). Alternatively, you can start
61# with gdb, like so:
62#
63# $ gdb python
64# (gdb) run `which athena.py` [options] [<file1>.py [<file2>.py ...
65#
66# Usage of valgrind is supported, but it requires full paths and explicit
67# arguments in its run, like so:
68#
69# $ valgrind `which python` `which athena.py` [options] [<file1>.py ...
70#
71# or, alternatively (valgrind 3.2.0 and up):
72#
73# $ valgrind --trace-children=yes `which athena.py` [options] [<file1>.py ...
74#
75# Note that any error messages/leaks that valgrind reports on python can be
76# ignored, as valgrind is wrong (see the file Misc/README.valgrind in the
77# python installation).
78#
79
80__author__ = 'Wim Lavrijsen (WLavrijsen@lbl.gov)'
81__doc__ = 'For details about athena.py, run "less `which athena.py`"'
82
83import sys, os
84
85
86import AthenaCommon.AthOptionsParser as aop
87aop.enable_athenaCLI()
88opts = aop.parse(legacy_args=True)
89
90
91from PyUtils.Helpers import ROOTSetup
92ROOTSetup(batch = not opts.interactive)
93
94
95if opts.scripts:
96 from AthenaCommon.Utils.unixtools import FindFile
97 path_list = ['./'] + os.environ.get('PYTHONPATH', '').split(os.pathsep)
98 file_path = FindFile( os.path.expanduser( os.path.expandvars(opts.scripts[0]) ),
99 path_list, os.R_OK )
100
101 if file_path is not None:
102 with open(file_path) as f:
103 if f.readline().startswith('#!'): # shebang means CA mode
104 opts.CA = True
105
106 if opts.CA:
107 sys.argv.remove(opts.scripts[0]) # drop script path from args
108 opts.scripts[0] = file_path # replace with resolved path
109
110elif opts.fromdb:
111 import pickle
112 with open(opts.fromdb, 'rb') as f:
113 try:
114 acc = pickle.load(f)
115 opts.CA = not isinstance(acc, dict) # legacy pkl is a dict
116 if not opts.CA:
117 del acc # legacy pkl is loaded in Execution.py
118 except ModuleNotFoundError:
119 pass # in case ComponentAccumulator class is not available in release
120
121
122if 'LD_PRELOAD_ORIG' in os.environ:
123 os.environ['LD_PRELOAD'] = os.getenv('LD_PRELOAD_ORIG')
124 os.unsetenv('LD_PRELOAD_ORIG')
125
126
127from AthenaCommon.Debugging import DbgStage
128DbgStage.value = opts.debug
129
130
131if not os.getcwd() in sys.path:
132 sys.path = [ os.getcwd() ] + sys.path
133
134if '' not in sys.path:
135 sys.path = [ '' ] + sys.path
136
137
138
139sys.ps1 = 'athena> '
140sys.ps2 = '. ... '
141
142try:
143 import ctypes
144 from ctypes.util import find_library as ctypes_find_library
145 libc = ctypes.cdll.LoadLibrary( ctypes_find_library('c') )
146 libc.prctl( 15, b'athena.py', 0, 0, 0 )
147except Exception:
148 pass # don't worry about it failing ...
149
150
151
152if not (opts.interactive or opts.debug):
153 # in batch there is no need for stdin
154 if sys.stdin and os.isatty( sys.stdin.fileno() ):
155 os.close( sys.stdin.fileno() )
156else:
157 # Make sure ROOT gets initialized early, so that it shuts down last.
158 # Otherwise, ROOT can get shut down before Gaudi, leading to crashes
159 # when Athena components dereference ROOT objects that have been deleted.
160 import ROOT # noqa: F401
161
162
163if opts.CA:
164 from AthenaCommon import ExitCodes
165 exitcode = 0
166 try:
167 if opts.scripts: # CA script
168 if not opts.tracelevel:
169 import runpy
170 runpy.run_path( opts.scripts[0], run_name='__main__' )
171 else:
172 from AthenaCommon.Debugging import traceExecution
173 traceExecution( opts.scripts[0], opts.tracelevel )
174
175 elif opts.fromdb: # pickle
176 from AthenaCommon.AthOptionsParser import configureCAfromArgs
177 configureCAfromArgs( acc, opts )
178 sys.exit(acc.run(opts.evtMax).isFailure())
179
180 except SystemExit as e:
181 # Failure in ComponentAccumulator.run() is very likely an algorithm error
182 exitcode = ExitCodes.EXE_ALG_FAILURE if e.code==1 else e.code
183
184 from AthenaCommon import Logging
185 Logging.log.info( 'leaving with code %d: "%s"',
186 exitcode, ExitCodes.what(exitcode) )
187 sys.exit( exitcode )
188
189
190else:
191 # logging and messages
192 from AthenaCommon.Logging import logging, log
193 _msg = log
194
195 # test and set log level
196 try:
197 _msg.setLevel (getattr(logging, opts.loglevel))
198 except Exception:
199 aop._help_and_exit()
200
201 # start profiler, if requested
202 if opts.profile_python:
203 import cProfile
204 # profiler is created and controlled programmatically b/c a CLI profiling of
205 # athena.py doesn't work (globals are lost from include() execfile() calls),
206 # and because this allows easy excluding of the (all C++) Gaudi run
207 cProfile._athena_python_profiler = cProfile.Profile()
208 cProfile._athena_python_profiler.enable()
209
210 # Fill athena job properties
211 aop.fill_athenaCommonFlags(opts)
212
213 # file inclusion and tracing
214 from AthenaCommon.Include import include
215 include.setShowIncludes(opts.showincludes)
216
217 # pre-execution step
218 include( "AthenaCommon/Preparation.py" )
219
220 # execution of user script and drop into batch or interactive mode ---------
221 include( "AthenaCommon/Execution.py" )
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179