ATLAS Offline Software
AtlRunQueryAMI.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 #
5 # ----------------------------------------------------------------
6 # Script : AtlRunQueryAMI.py
7 # Project: AtlRunQuery
8 # Purpose: Utility to retrieve information from AMI
9 # Authors: Andreas Hoecker (CERN), Joerg Stelzer (DESY)
10 # Created: Nov 10, 2009
11 # ----------------------------------------------------------------
12 #
13 
14 import re
15 
16 class ARQ_AMI:
17 
18  _amiclient = None
19  store = {}
20  all_periods = None
21 
22 
23  @classmethod
24  def getAmiClient(cls, amiconf = None, verbose = False):
25  """get ami client
26  param amiconf: name of file with AMI user and pw
27 
28  If a valid filename is specified, it tries to read the
29  username and password from there. If that does not succeed or
30  no filename is specified, voms based authentication is tried.
31  """
32  from pyAMI.pyAMI import AMI,pyAMIEndPoint
33  from os import stat,path
34 
35  useConfigFile = False
36  if amiconf:
37  if not path.exists(amiconf):
38  if verbose:
39  print ("WARNING: AMI config file", amiconf, "does not exist. Need to rely on valid voms proxy.")
40  elif stat(amiconf).st_mode & path.stat.S_IRUSR == 0:
41  if verbose:
42  print ("WARNING: AMI config file", amiconf, "exists but is not readable. Need to rely on valid voms proxy.")
43  else:
44  useConfigFile = True
45 
46  if useConfigFile:
47  pyAMIEndPoint.setType("replica")
48  ami=AMI()
49  ami.readConfig(amiconf)
50  if ami.checkAuth():
51  print ("... connecting to CERN AMI replica with user+pw")
52  return ami
53  pyAMIEndPoint.setType("main")
54  ami.certAuth()
55  if ami.checkAuth():
56  print ("... connecting to AMI main server with user+pw")
57  return ami
58 
59  print ("WARNING: Authentication in config file",amiconf,"not valid, check format, user, pw. Need to rely on valid voms proxy.")
60 
61 
62  pyAMIEndPoint.setType("replica")
63  ami=AMI()
64  if ami.checkAuth():
65  print ("... connecting to CERN replica using voms-proxy")
66  return ami
67 
68 
69  pyAMIEndPoint.setType("main")
70  ami.certAuth()
71  if ami.checkAuth():
72  print ("... connecting to main server using voms-proxy")
73  return ami
74 
75 
76  if verbose:
77  print ("WARNING voms-proxy authentication not valid. No access to AMI.")
78  return None
79 
80 
81  @classmethod
82  def amiclient(cls):
83  if cls._amiclient is None:
84  from os import getenv
85  from CoolRunQuery.utils.AtlRunQueryUtils import runsOnServer
86  if runsOnServer():
87  home = "/data/atrqadm/data/"
88  else:
89  home = getenv('HOME')
90  if not home:
91  home = '~'
92  conffilename = home + "/private/AMIConf.txt"
93  cls._amiclient = cls.getAmiClient(conffilename)
94  if cls._amiclient is None:
95  print ("ERROR: voms-proxy authentication not valid and no AMI configuration file",conffilename,"supplied. No access to AMI!")
96  cls._amiclient="No AMI"
97  return cls._amiclient
98 
99  @classmethod
101  try:
102  from pyAMI.pyAMI import AMI
103  amiclient = AMI()
104  amiclient.readConfig("./AMIConf.txt")
105  return amiclient
106  except ImportError:
107  print ('ERROR: could not load pyAMI')
108  return None
109 
110 
111  @classmethod
112  def amiexec(cls, cmdList):
113  try:
114  result = cls.amiclient().execute(cmdList)
115  return result.getDict()
116  except Exception as ex:
117  print ("AMI exception '",type(ex),"' occured")
118  import traceback
119  traceback.print_exc()
120  return {}
121 
122 
123  @classmethod
124  def get_periods_for_run(cls, run):
125  try:
126  run = int(run)
127  except ValueError:
128  return []
129  if run not in cls.store:
130  try:
131  print ('GetDataPeriodsForRun', '-runNumber=%i' % run)
132  #result = cls.amiclient().execute(['GetDataPeriodsForRun', '-runNumber=%i' % run])
133  result = cls.amiexec(['GetDataPeriodsForRun', '-runNumber=%i' % run])
134  #cls.store[run] = sorted([ (int(e['periodLevel']),e['period'],e['project']) for e in result.getDict()['Element_Info'].values() ])
135  cls.store[run] = sorted([ (int(e['periodLevel']),e['period'],e['project']) for e in result['Element_Info'].values() ])
136  except Exception:
137  print ("Exception")
138  cls.store[run] = []
139  return [x[1] for x in cls.store[run]]
140 
141 
142  @classmethod
143  def get_periods(cls, year, level):
144  try:
145  cmd = ['ListDataPeriods', '-createdSince=2009-01-01 00:00:00' ]
146  if year>2000:
147  cmd += [ '-projectName=data%02i%%' % (year-2000) ]
148  if level in [1,2,3]:
149  cmd += [ '-periodLevel=%i' % level ]
150  #result = cls.amiclient().execute(cmd)
151  result = cls.amiexec(cmd)
152  #rows = [ (e['period'], e['projectName']) for e in result.getDict()['Element_Info'].values() ]
153  rows = [ (e['period'], e['projectName']) for e in result['Element_Info'].values() ]
154  return sorted(rows)
155  except Exception:
156  import traceback
157  traceback.printExc()
158  return []
159 
160 
161  @classmethod
162  def get_all_periods(cls):
163  if cls.all_periods is not None:
164  return cls.all_periods
165  cls.all_periods = []
166  p = re.compile(r"(?P<period>(?P<periodletter>[A-Za-z]+)(?P<periodnumber>\d+)?)$")
167  try:
168  result = cls.get_periods(0, 0)
169  for period, projectName in result:
170  m = p.match(period)
171  if not m:
172  continue
173  year = int(projectName[4:6])
174  period_letter = m.group('periodletter')
175  period_number = int(m.group('periodnumber')) if m.group('periodnumber') else 0
176  if len(period_letter)!=1:
177  pc = 0
178  else:
179  pc = 10000*year + 100*(ord(period_letter.upper())-65) + period_number
180  cls.all_periods += [ ((year, period, pc), projectName+".period" + period) ]
181  cls.all_periods.sort()
182  except Exception:
183  import traceback
184  traceback.print_exc()
185  pass
186  return cls.all_periods
187 
188 
189  @classmethod
190  def get_runs(cls, period, year):
191  try:
192  cmd = ['GetRunsForDataPeriod', '-period=%s' % period]
193  if year>2000:
194  cmd += [ '-projectName=data%02i%%' % (year-2000) ]
195  #result = cls.amiclient().execute(cmd)
196  result = cls.amiexec(cmd)
197  #print ("amiCommand",' '.join(cmd) )
198  #r = sorted([ int(e['runNumber']) for e in result.getDict()['Element_Info'].values() ])
199  r = sorted([ int(e['runNumber']) for e in result['Element_Info'].values() ])
200  return r
201  except (ValueError, KeyError):
202  pass
203 
204 
205 
206 # for testing purpose
207 if __name__ == "__main__":
208 
209  choice = 1
210  while choice != 0:
211  print ("\n1 - periods for run")
212  print ("2 - runs for period (and year)")
213  print ("3 - periods (by year and/or level)")
214  print ("4 - all periods (different format)")
215  print ("5 - test AMI authentication")
216  print ("\n0 - exit\n")
217 
218  choice = input(" enter your choice: ")
219  choice = int(choice) if choice.isdigit() else 0
220  if choice==1:
221  run = int(input(" run number: "))
222  print (ARQ_AMI.get_periods_for_run( [run] ))
223  elif choice==2:
224  period = input(" period : ")
225  year = input(" year <RET> = all : ")
226  year = int(year) if year.isdigit() else 0
227  print (', '.join([str(x) for x in ARQ_AMI.get_runs(period, year)]))
228  elif choice==3:
229  year = input(" year <RET> = all : ")
230  year = int(year) if year.isdigit() else 0
231  period = input(" period [1|2|3] <RET> = all : ")
232  period = int(period) if period.isdigit() else 0
233  print (ARQ_AMI.get_periods(year, period))
234  elif choice==4:
235  print (ARQ_AMI.get_all_periods())
236  elif choice==5:
237  ami = ARQ_AMI.amiclient()
238  if ami!="No AMI":
239  print ("Successfully connected to AMI")
240  else:
241  print ("Failed to connect to AMI")
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename R::value_type > sorted(const R &r, PROJ proj={})
Helper function to create a sorted vector from an unsorted range.
python.AtlRunQueryAMI.ARQ_AMI.get_all_periods
def get_all_periods(cls)
Definition: AtlRunQueryAMI.py:162
python.AtlRunQueryAMI.ARQ_AMI.get_periods
def get_periods(cls, year, level)
Definition: AtlRunQueryAMI.py:143
python.AtlRunQueryAMI.ARQ_AMI.getAmiClient
def getAmiClient(cls, amiconf=None, verbose=False)
Definition: AtlRunQueryAMI.py:24
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
python.utils.AtlRunQueryUtils.runsOnServer
def runsOnServer()
Definition: AtlRunQueryUtils.py:47
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:808
LArG4FSStartPointFilterLegacy.execute
execute
Definition: LArG4FSStartPointFilterLegacy.py:20
python.AtlRunQueryAMI.ARQ_AMI.get_runs
def get_runs(cls, period, year)
Definition: AtlRunQueryAMI.py:190
python.AtlRunQueryAMI.ARQ_AMI.all_periods
all_periods
Definition: AtlRunQueryAMI.py:20
beamspotman.stat
stat
Definition: beamspotman.py:264
python.AtlRunQueryAMI.ARQ_AMI
Definition: AtlRunQueryAMI.py:16
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.AtlRunQueryAMI.ARQ_AMI._amiclient
_amiclient
Definition: AtlRunQueryAMI.py:18
python.AtlRunQueryAMI.ARQ_AMI.OpenAMIConnection
def OpenAMIConnection(cls)
Definition: AtlRunQueryAMI.py:100
python.AtlRunQueryAMI.ARQ_AMI.get_periods_for_run
def get_periods_for_run(cls, run)
Definition: AtlRunQueryAMI.py:124
SCT_ConditionsAlgorithms::CoveritySafe::getenv
std::string getenv(const std::string &variableName)
get an environment variable
Definition: SCT_ConditionsUtilities.cxx:17
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.AtlRunQueryAMI.ARQ_AMI.amiexec
def amiexec(cls, cmdList)
Definition: AtlRunQueryAMI.py:112
str
Definition: BTagTrackIpAccessor.cxx:11
python.AtlRunQueryAMI.ARQ_AMI.store
dictionary store
Definition: AtlRunQueryAMI.py:19
python.AtlRunQueryAMI.ARQ_AMI.amiclient
def amiclient(cls)
Definition: AtlRunQueryAMI.py:82