ATLAS Offline Software
pyroot.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 "exec" "`which python`" "-tt" "$0" "$@"
4 
5 # File: pyroot.py
6 # Author: Sebastien Binet (binet@cern.ch)
7 
8 # This script allows you to run ROOT from python.
9 # It has been heavily based (err... stolen) on athena.py from Wim
10 
11 from __future__ import print_function
12 
13 __author__ = 'Sebastien Binet (binet@cern.ch)'
14 __doc__ = 'For details about pyroot.py, run "less `which pyroot.py`"'
15 
16 import sys, os
17 import getopt, string
18 
19 
20 
21 _useropts = "bidc:hv"
22 _userlongopts = [ "batch", "interactive", "no-display", "debug=", "command=",
23  "help", "version",]
24 
25 
26 
27 def _help_and_exit( reason = None ):
28  print ("""Accepted command line options:
29  -b, --batch ... batch mode
30  -i, --interactive ... interactive mode [default]
31  --no-display prompt, but no graphics display
32  -c, --command ... one-liner, runs before any scripts
33  -h, --help ... print this help message
34  -,-- [arg1,...] ... additional arguments passed directly
35  to user scripts (left untouched)
36  [<file1>.py [<file2>.py [...]]] ... scripts to run""")
37 
38  sys.exit( 1 )
39 
40 
41 
42 scripts,opts = [],[]
43 runBatch = 0 # batch mode is NOT the default
44 display = None # only useful in interactive mode: no display
45 command = "" # optional one-line command
46 userOpts = [] # left-over opts after '-'
47 
48 
49 
50 for arg in sys.argv[1:]:
51  if arg[-3:] == ".py":
52  scripts.append( arg )
53  elif arg in ('-','--'): # rest are user opts, save and done
54  userOpts += sys.argv[ sys.argv.index( arg )+1: ]
55  break
56  else:
57  opts.append( arg )
58 
59 
60 try:
61  optlist, args = getopt.getopt( opts, _useropts, _userlongopts )
62 except getopt.error:
63  print (sys.exc_value)
65 
66 if args:
67  print ("Unhandled arguments:", args)
69 
70 for opt, arg in optlist:
71  if opt in ("-b", "--batch"):
72  runBatch = 1
73  elif opt in ("-i", "--interactive"):
74  runBatch = 0
75  defOptions = ""
76  if display is None: display = 1
77  elif opt in ("--no-display",):
78  display = 0
79  elif opt in ("-c", "--command"):
80  command = string.strip( arg )
81  elif opt in ("-h", "--help"):
83 
84 if optlist: del opt, arg
85 del args, optlist, opts
86 del _useropts, _userlongopts, string, getopt
87 
88 
89 if not display and '-b' not in sys.argv:
90  sys.argv = sys.argv[:1] + ['-b'] + sys.argv[1:]
91 del display
92 
93 
94 
95 if not os.getcwd() in sys.path:
96  sys.path = [ os.getcwd() ] + sys.path
97 
98 if '' not in sys.path:
99  sys.path = [ '' ] + sys.path
100 
101 sys.ps1 = 'pyroot> '
102 fhistory = os.path.expanduser( '~/.pyroot.history' )
103 
104 
105 if runBatch:
106  # in batch there is no need for stdin
107  if os.isatty( sys.stdin.fileno() ):
108  os.close( sys.stdin.fileno() )
109  if 'PYTHONINSPECT' in os.environ:
110  os.unsetenv('PYTHONINSPECT')
111 else:
112  os.environ['PYTHONINSPECT'] = '1'
113  # readline support
114  import rlcompleter, readline # noqa: F401
115 
116  readline.parse_and_bind( 'tab: complete' )
117  readline.parse_and_bind( 'set show-all-if-ambiguous On' )
118 
119  # history support
120  if os.path.exists( fhistory ):
121  readline.read_history_file( fhistory )
122  readline.set_history_length( 1024 )
123 
124  del readline, rlcompleter
125 
126 
127 print (sys.ps1+"loading ROOT...")
128 import ROOT
129 print (sys.ps1+"loading ROOT... [ok]")
130 ROOT.gROOT.SetStyle("Plain")
131 ROOT.gStyle.SetPalette(1) # less ugly palette colors
132 ROOT.gStyle.SetOptStat(111111) #
133 print (sys.ps1+"loaded pyroot style... [ok]")
134 
135 
136 if not runBatch:
137  import atexit, readline
138 
139  # history support
140  atexit.register( readline.write_history_file, fhistory )
141  del readline, atexit
142 
143 del fhistory
144 
145 if command:
146  print (sys.ps1+'executing CLI (-c) command: "%s"' % command)
147  exec (command)
148 del command
149 
150 from past.builtins import execfile
151 for script in scripts:
152  try:
153  execfile( script )
154  except Exception as e:
155  if isinstance(e,SystemExit):
156  raise
157 
158  import traceback
159  traceback.print_exc()
160 
161  if runBatch:
162  import sys
163  sys.exit(2)
164 
165  # for interactive: drop into prompt
166  break
167 
168 else:
169 
170  del scripts
171 
172 
173  if runBatch:
174  import sys
175  sys.exit(0)
176  else:
177  # done, back to the user
178  print (sys.ps1+"entering interactive session...")
179  pass
pyroot._help_and_exit
def _help_and_exit(reason=None)
explanation of the options --------------------------------------------—
Definition: pyroot.py:27