ATLAS Offline Software
Tools/PyUtils/python/Helpers.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
3 # @author: Sebastien Binet <binet@cern.ch>
4 # @date: March 2007
5 #
6 #
7 
8 import re
9 import os
10 import sys
11 from functools import cache
12 from tempfile import NamedTemporaryFile
13 # !!!
14 # Do not add any Athena dependencies at the module-level as some of the code here
15 # is used during the build (see athena!62739).
16 # !!!
17 
18 
19 def ROOT6Setup(batch=False):
20  from AthenaCommon.Logging import log
21  log.info('executing ROOT6Setup')
22  import builtins as builtin_mod
23  oldimporthook = builtin_mod.__import__
24  autoload_var_name = 'ROOT6_NamespaceAutoloadHook'
25  batch_mode = bool(batch)
26 
27  def root6_importhook(name, globals={}, locals={}, fromlist=[], level=0):
28  nonlocal batch_mode
29  isroot = False
30  bm = batch_mode
31  if name=='ROOT' or (name[0:4]=='ROOT' and name!='ROOT.pythonization'):
32  isroot = True
33  batch_mode = None # only set it on first ROOT import
34 
35  m = oldimporthook(name, globals, locals, fromlist, level)
36 
37  if m and isroot:
38  log.debug('Python import module=%s, fromlist=%s', name, fromlist)
39  if bm is not None:
40  log.debug('Setting ROOT batch mode to %s', bm)
41  m.gROOT.SetBatch(bm)
42 
43  if fromlist:
44  # in this case 'm' is the final nested module already, don't walk the full 'name'
45  vars = [ '.'.join(['', fl, autoload_var_name]) for fl in fromlist]
46  else:
47  vars = [ '.'.join([name, autoload_var_name]) ]
48 
49  for v in vars:
50  try:
51  mm = m
52  # walk the module chain and try to touch 'autoload_var_name' to trigger ROOT autoloading of namespaces
53  for comp in v.split('.')[1:]:
54  mm = getattr(mm, comp)
55  except Exception:
56  pass
57 
58  return m
59 
60  builtin_mod.__import__ = root6_importhook
61 
62 
63 class ShutUp(object):
64  """
65  A little helper class to keep ROOT silent...
66  """
67  DefaultFilter = [
68  re.compile("Warning in <TClass::TClass>: no dictionary for class."),
69  re.compile("Warning in <TEnvRec::ChangeValue>: duplicate entry."),
70  re.compile("Error in <TStreamerInfo::BuildOld>:."),
71  ]
72  def __init__(self, filters = DefaultFilter):
73  self._dummy = False # if dummy, we don't really shut-up ROOT...
74  if os.environ.get('PYUTILS_SHUTUP_DEBUG', '0') == '1':
75  self._dummy = True
76 
77  self.save_err = open( '/dev/null', 'w' )
78  self.save_out = open( '/dev/null', 'w' )
79  try:
80  self.quiet_err = NamedTemporaryFile( suffix = ".msg.log" )
81  self.quiet_out = NamedTemporaryFile( suffix = ".msg.log" )
82  except OSError:
83  # load problem ?
84  # retry a bit later... (in 2 seconds)
85  import time
86  time.sleep(2) #
87  try:
88  self.quiet_err = NamedTemporaryFile( suffix = ".msg.log" )
89  self.quiet_out = NamedTemporaryFile( suffix = ".msg.log" )
90  except OSError:
91  # then (implicitly) fallback on sys.stderr
92  self._dummy = True
93 
94  self.filters = filters
95 
96  if not self._dummy:
97  os.dup2( sys.stderr.fileno(), self.save_err.fileno() )
98  os.dup2( sys.stdout.fileno(), self.save_out.fileno() )
99  return
100 
101  def __del__ (self):
102  self.save_err.close()
103  self.save_out.close()
104  return
105 
106  def mute(self):
107  if not self._dummy:
108  os.dup2( self.quiet_err.fileno(), sys.stderr.fileno() )
109  os.dup2( self.quiet_out.fileno(), sys.stdout.fileno() )
110  return
111 
112  def unMute(self):
113  if not self._dummy:
114  os.dup2( self.save_err.fileno(), sys.stderr.fileno() )
115  os.dup2( self.save_out.fileno(), sys.stdout.fileno() )
117  self.quiet_err.seek(0)
119  self.quiet_out.seek(0)
120  return
121 
122  def __filterRootMessages(self, fd):
123  fd.seek(0)
124  for l in fd.readlines():
125  l = l.decode()
126  printOut = True
127  for filter in self.filters:
128  if re.match(filter, l):
129  printOut = False
130  if printOut:
131  print ("PyRoot:",l.replace("\n",""))
132  pass
133  return
134 
135  # context-manager protocol
136  def __enter__(self):
137  return self.mute()
138  def __exit__(self,exc_type, exc_val, exc_tb):
139  return self.unMute()
140 
141 
142 @cache
144  """Returns information about the current release based on ReleaseData"""
145 
146  import configparser
147  d = {
148  'project name': '?',
149  'release': '?',
150  'base release': '?',
151  'nightly release': '?',
152  'nightly name': '?',
153  'date': '?',
154  'platform': '?',
155  }
156 
157  for cmake_path in os.environ['CMAKE_PREFIX_PATH'].split(os.pathsep):
158  release_data = os.path.join(cmake_path, 'ReleaseData')
159  if os.path.exists(release_data):
160  d1=d
161  cfg = configparser.ConfigParser()
162  try:
163  cfg.read( release_data )
164  if cfg.has_section( 'release_metadata' ):
165  d1.update( dict( cfg.items( 'release_metadata' ) ) )
166  d1['platform'] = os.getenv( '%s_PLATFORM' % d1['project name'],
167  '?' )
168  release = d1['release'].split('.')
169  base_release = d1['base release'].split('.')
170  if len(release)>=3 or len(base_release)>=3:
171  return d1
172  except Exception:
173  pass
174  return d
python.Helpers.release_metadata
def release_metadata()
Definition: Tools/PyUtils/python/Helpers.py:143
python.Helpers.ShutUp.save_err
save_err
Definition: Tools/PyUtils/python/Helpers.py:77
python.Helpers.ShutUp
Definition: Tools/PyUtils/python/Helpers.py:63
python.Helpers.ShutUp.quiet_err
quiet_err
Definition: Tools/PyUtils/python/Helpers.py:80
python.Helpers.ShutUp.filters
filters
Definition: Tools/PyUtils/python/Helpers.py:94
python.Helpers.ROOT6Setup
def ROOT6Setup(batch=False)
Definition: Tools/PyUtils/python/Helpers.py:19
python.Helpers.ShutUp.__filterRootMessages
def __filterRootMessages(self, fd)
Definition: Tools/PyUtils/python/Helpers.py:122
python.Helpers.ShutUp.quiet_out
quiet_out
Definition: Tools/PyUtils/python/Helpers.py:81
python.Helpers.ShutUp.mute
def mute(self)
Definition: Tools/PyUtils/python/Helpers.py:106
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.Helpers.ShutUp.save_out
save_out
Definition: Tools/PyUtils/python/Helpers.py:78
python.Helpers.ShutUp._dummy
_dummy
Definition: Tools/PyUtils/python/Helpers.py:73
Trk::open
@ open
Definition: BinningType.h:40
python.Helpers.ShutUp.__init__
def __init__(self, filters=DefaultFilter)
Definition: Tools/PyUtils/python/Helpers.py:72
python.Helpers.ShutUp.__del__
def __del__(self)
Definition: Tools/PyUtils/python/Helpers.py:101
pickleTool.object
object
Definition: pickleTool.py:30
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.Helpers.ShutUp.unMute
def unMute(self)
Definition: Tools/PyUtils/python/Helpers.py:112
python.Helpers.ShutUp.__enter__
def __enter__(self)
Definition: Tools/PyUtils/python/Helpers.py:136
python.Helpers.ShutUp.__exit__
def __exit__(self, exc_type, exc_val, exc_tb)
Definition: Tools/PyUtils/python/Helpers.py:138