CMS 3D CMS Logo

tier0.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 #-toDo: move this to common?
4 
5 import logging
6 import json
7 import os
8 import sys
9 import time
10 import subprocess
11 
12 import pycurl
13 
14 tier0Url = 'https://cmsweb.cern.ch/t0wmadatasvc/prod/'
15 
17  '''Tier0 exception.
18  '''
19 
20  def __init__(self, message):
21  self.args = (message, )
22 
23 
24 def unique(seq, keepstr=True):
25  t = type(seq)
26  if t in (unicode, str):
27  t = (list, t('').join)[bool(keepstr)]
28  try:
29  remaining = set(seq)
30  seen = set()
31  return t(c for c in seq if (c in remaining and not remaining.remove(c)))
32  except TypeError: # hashing didn't work, see if seq is sortable
33  try:
34  from itertools import groupby
35  s = sorted(enumerate(seq),key=lambda i_v1:(i_v1[1],i_v1[0]))
36  return t(next(g) for k,g in groupby(s, lambda i_v: i_v[1]))
37  except: # not sortable, use brute force
38  seen = []
39  return t(c for c in seq if not (c in seen or seen.append(c)))
40 
42 
43  def __init__( self, curl, response, proxy, timeout ):
44  super( ResponseError, self ).__init__( response )
45  self.args += ( curl, proxy )
46  self.timeout = timeout
47 
48  def __str__( self ):
49  errStr = """Wrong response for curl connection to Tier0DataSvc from URL \"%s\"""" %( self.args[1].getinfo( self.args[1].EFFECTIVE_URL ), )
50  if self.args[ -1 ]:
51  errStr += """ using proxy \"%s\"""" %( str( self.args[ -1 ] ), )
52  errStr += """ with timeout \"%d\" with error code \"%d\".""" %( self.timeout, self.args[1].getinfo( self.args[1].RESPONSE_CODE) )
53  if self.args[0].find( '<p>' ) != -1:
54  errStr += """\nFull response: \"%s\".""" %( self.args[0].partition('<p>')[-1].rpartition('</p>')[0], )
55  else:
56  errStr += """\nFull response: \"%s\".""" %( self.args[0], )
57  return errStr
58 
59 #TODO: Add exceptions for each category of HTTP error codes
60 #TODO: check response code and raise corresponding exceptions
61 
62 def _raise_http_error( curl, response, proxy, timeout ):
63  raise ResponseError( curl, response, proxy, timeout )
64 
66 
67  def __init__( self, uri, timeOut, retries, retryPeriod, proxy, debug ):
68  """
69  Parameters:
70  uri: Tier0DataSvc URI;
71  timeOut: time out for Tier0DataSvc HTTPS calls;
72  retries: maximum retries for Tier0DataSvc HTTPS calls;
73  retryPeriod: sleep time between two Tier0DataSvc HTTPS calls;
74  proxy: HTTP proxy for accessing Tier0DataSvc HTTPS calls;
75  debug: if set to True, enables debug information.
76  """
77  self._uri = uri
78  self._timeOut = timeOut
79  self._retries = retries
80  self._retryPeriod = retryPeriod
81  self._proxy = proxy
82  self._debug = debug
83 
84  def setDebug( self ):
85  self._debug = True
86 
87  def unsetDebug( self ):
88  self._debug = False
89 
90  def setProxy( self, proxy ):
91  self._proxy = proxy
92 
93  def _queryTier0DataSvc( self, url ):
94  """
95  Queries Tier0DataSvc.
96  url: Tier0DataSvc URL.
97  @returns: dictionary, from whence the required information must be retrieved according to the API call.
98  Raises if connection error, bad response, or timeout after retries occur.
99  """
100 
101  userAgent = "User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" % ( sys.version_info[ :3 ] + ( pycurl.version_info()[ 1 ], ) )
102 
103  proxy = ""
104  if self._proxy: proxy = ' --proxy=%s ' % self._proxy
105 
106  debug = " -s -S "
107  if self._debug: debug = " -v "
108 
109  cmd = '/usr/bin/curl -k -L --user-agent "%s" %s --connect-timeout %i --retry %i %s %s ' % (userAgent, proxy, self._timeOut, self._retries, debug, url)
110 
111  # time the curl to understand if re-tries have been carried out
112  start = time.time()
113  process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
114  (stdoutdata, stderrdata) = process.communicate()
115  retcode = process.returncode
116  end = time.time()
117 
118  if retcode != 0 or stderrdata:
119  # if the first curl has failed, logg its stderror and prepare and independent retry
120  msg = "looks like curl returned an error: retcode=%s and took %s seconds" % (retcode,(end-start),)
121  msg += ' msg = "'+str(stderrdata)+'"'
122  logging.error(msg)
123 
124  time.sleep(10)
125  cmd = '/usr/bin/curl -k -L --user-agent "%s" %s --connect-timeout %i --retry %i %s %s ' % (userAgent, proxy, self._timeOut, self._retries, "-v", url)
126  process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
127  (stdoutdata, stderrdata) = process.communicate()
128  retcode = process.returncode
129  if retcode != 0:
130  msg = "looks like curl returned an error for the second time: retcode=%s" % (retcode,)
131  msg += ' msg = "'+str(stderrdata)+'"'
132  logging.error(msg)
133  raise Tier0Error(msg)
134  else :
135  msg = "curl returned ok upon the second try"
136  logging.info(msg)
137 
138  return json.loads( ''.join(stdoutdata).replace( "'", '"').replace(' None', ' "None"') )
139 
140 
141  def getFirstSafeRun( self ):
142  """
143  Queries Tier0DataSvc to get the first condition safe run.
144  Parameters:
145  @returns: integer, the run number.
146  Raises if connection error, bad response, timeout after retries occur, or if the run number is not available.
147  """
148  firstConditionSafeRunAPI = "firstconditionsaferun"
149  safeRunDict = self._queryTier0DataSvc( os.path.join( self._uri, firstConditionSafeRunAPI ) )
150  if safeRunDict is None:
151  errStr = """First condition safe run is not available in Tier0DataSvc from URL \"%s\"""" %( os.path.join( self._uri, firstConditionSafeRunAPI ), )
152  if self._proxy:
153  errStr += """ using proxy \"%s\".""" %( str( self._proxy ), )
154  raise Tier0Error( errStr )
155  return int(safeRunDict['result'][0])
156 
157  def getGlobalTag( self, config ):
158  """
159  Queries Tier0DataSvc to get the most recent Global Tag for a given workflow.
160  Parameters:
161  config: Tier0DataSvc API call for the workflow to be looked for;
162  @returns: a string with the Global Tag name.
163  Raises if connection error, bad response, timeout after retries occur, or if no Global Tags are available.
164  """
165  data = self._queryTier0DataSvc( os.path.join( self._uri, config ) )
166  gtnames = sorted(unique( [ str( di[ 'global_tag' ] ) for di in data['result'] if di[ 'global_tag' ] is not None ] ))
167  try:
168  recentGT = gtnames[-1]
169  return recentGT
170  except IndexError:
171  errStr = """No Global Tags for \"%s\" are available in Tier0DataSvc from URL \"%s\"""" %( config, os.path.join( self._uri, config ) )
172  if self._proxy:
173  errStr += """ using proxy \"%s\".""" %( str( self._proxy ), )
174  raise Tier0Error( errStr )
175 
176 
177 def test( url ):
178  t0 = Tier0Handler( url, 1, 1, 1, None, debug=False)
179 
180  print(' fcsr = %s (%s)' % (t0.getFirstSafeRun(), type(t0.getFirstSafeRun()) ))
181  print(' reco_config = %s' % t0.getGlobalTag('reco_config'))
182  print(' express_config = %s' % t0.getGlobalTag('express_config'))
183  print('\n')
184 
185 
186 if __name__ == '__main__':
187  test( tier0Url )
188 
electrons_cff.bool
bool
Definition: electrons_cff.py:393
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
tier0._raise_http_error
def _raise_http_error(curl, response, proxy, timeout)
Definition: tier0.py:62
tier0.Tier0Handler
Definition: tier0.py:65
tier0.Tier0Handler.setDebug
def setDebug(self)
Definition: tier0.py:84
tier0.Tier0Handler.getGlobalTag
def getGlobalTag(self, config)
Definition: tier0.py:157
tier0.Tier0Error.args
args
Definition: tier0.py:21
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
tier0.ResponseError
Definition: tier0.py:41
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
tier0.ResponseError.__str__
def __str__(self)
Definition: tier0.py:48
tier0.Tier0Error.__init__
def __init__(self, message)
Definition: tier0.py:20
tier0.Tier0Handler._retries
_retries
Definition: tier0.py:79
test
Definition: SmallWORMDict.h:13
str
#define str(s)
Definition: TestProcessor.cc:51
tier0.Tier0Handler.setProxy
def setProxy(self, proxy)
Definition: tier0.py:90
tier0.Tier0Handler.__init__
def __init__(self, uri, timeOut, retries, retryPeriod, proxy, debug)
Definition: tier0.py:67
tier0.Tier0Handler._uri
_uri
Definition: tier0.py:77
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
tier0.Tier0Handler.getFirstSafeRun
def getFirstSafeRun(self)
Definition: tier0.py:141
Exception
createfilelist.int
int
Definition: createfilelist.py:10
tier0.Tier0Handler._proxy
_proxy
Definition: tier0.py:81
tier0.unique
def unique(seq, keepstr=True)
Definition: tier0.py:24
tier0.Tier0Handler.unsetDebug
def unsetDebug(self)
Definition: tier0.py:87
tier0.Tier0Handler._debug
_debug
Definition: tier0.py:82
tier0.ResponseError.timeout
timeout
Definition: tier0.py:46
tier0.Tier0Handler._timeOut
_timeOut
Definition: tier0.py:78
tier0.test
def test(url)
Definition: tier0.py:177
tier0.Tier0Handler._retryPeriod
_retryPeriod
Definition: tier0.py:80
tier0.ResponseError.__init__
def __init__(self, curl, response, proxy, timeout)
Definition: tier0.py:43
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
tier0.Tier0Handler._queryTier0DataSvc
def _queryTier0DataSvc(self, url)
Definition: tier0.py:93
tier0.Tier0Error
Definition: tier0.py:16
GetRecoTauVFromDQM_MC_cff.next
next
Definition: GetRecoTauVFromDQM_MC_cff.py:31
python.rootplot.root2matplotlib.replace
def replace(string, replacements)
Definition: root2matplotlib.py:444