1 from __future__
import print_function
14 tier0Url = os.getenv(
'TIER0_API_URL',
'https://cmsweb.cern.ch/t0wmadatasvc/prod/')
27 t = (list,
t(
'').join)[
bool(keepstr)]
31 return t(c
for c
in seq
if (c
in remaining
and not remaining.remove(c)))
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]))
39 return t(c
for c
in seq
if not (c
in seen
or seen.append(c)))
44 def __init__( self, curl, response, proxy, timeout, maxTime ):
45 super( ResponseError, self ).
__init__( response )
46 self.
args += ( curl, proxy )
51 errStr = f
'Wrong response for curl connection to Tier0DataSvc'\
52 f
' from URL "{self.args[1].getinfo(self.args[1].EFFECTIVE_URL)}"' 54 errStr += f
' using proxy "{str(self.args[-1])}"' 55 errStr += f
' with connection-timeout "{self.timeout}", max-time "{self.maxtime}"'\
56 f
' with error code "{self.args[1].getinfo(self.args[1].RESPONSE_CODE)}".' 57 if '<p>' in self.
args[0]:
58 full_response = self.
args[0].
partition(
'<p>')[-1].rpartition(
'</p>')[0]
59 errStr += f
'\nFull response: "{full_response}".' 61 errStr += f
'\nFull response: "{self.args[0]}".' 69 raise ResponseError( curl, response, proxy, timeout, maxTime )
73 def __init__( self, uri, timeOut, maxTime, retries, retryPeriod, proxy, debug ):
76 uri: Tier0DataSvc URI; 77 timeOut: time out for connection of Tier0DataSvc HTTPS calls [seconds]; 78 maxTime: maximum time for Tier0DataSvc HTTPS calls (including data transfer) [seconds]; 79 retries: maximum retries for Tier0DataSvc HTTPS calls; 80 retryPeriod: sleep time between two Tier0DataSvc HTTPS calls [seconds]; 81 proxy: HTTP proxy for accessing Tier0DataSvc HTTPS calls; 82 debug: if set to True, enables debug information. 102 cert_path = os.getenv(
'X509_USER_CERT',
'')
103 key_path = os.getenv(
'X509_USER_KEY',
'')
107 certs += f
' --cert {cert_path}' 109 logging.warning(
"No certificate provided for Tier0 access, use X509_USER_CERT and" 110 " optionally X509_USER_KEY env variables to specify the path to the cert" 111 " (and the key unless included in the cert file)")
113 certs += f
' --key {key_path}' 116 def _curlQueryTier0( self, url:str, force_debug:bool =
False, force_cert:bool =
False):
117 userAgent =
"User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" \
118 % ( sys.version_info[ :3 ] + ( pycurl.version_info()[ 1 ], ) )
119 debug =
"-v" if self.
_debug or force_debug
else "-s -S" 121 proxy = f
"--proxy {self._proxy}" if self.
_proxy else "" 124 cmd = f
'/usr/bin/curl -k -L --user-agent "{userAgent}" {proxy}'\
125 f
' --connect-timeout {self._timeOut} --max-time {self._maxTime} --retry {self._retries}'\
126 f
' {debug} {url} {certs}' 130 process = subprocess.Popen(cmd, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
131 (stdoutdata, stderrdata) = process.communicate()
133 return process.returncode, stdoutdata, stderrdata, end-start
137 Queries Tier0DataSvc. 138 url: Tier0DataSvc URL. 139 @returns: dictionary, from whence the required information must be retrieved according to the API call. 140 Raises if connection error, bad response, or timeout after retries occur. 143 retcode, stdoutdata, stderrdata, query_time = self.
_curlQueryTier0(url)
145 if retcode != 0
or stderrdata:
148 msg =
"looks like curl returned an error: retcode=%s and took %s seconds" % (retcode, query_time,)
149 msg +=
' msg = "'+
str(stderrdata)+
'"' 152 logging.info(
"before assumed proxy provides authentication, now trying with both proxy and certificate")
155 retcode, stdoutdata, stderrdata, query_time = self.
_curlQueryTier0(url, force_debug=
True, force_cert=
True)
157 msg =
"looks like curl returned an error for the second time: retcode=%s" % (retcode,)
158 msg +=
' msg = "'+
str(stderrdata)+
'"' 162 msg =
"curl returned ok upon the second try" 164 resp = json.loads(
''.
join(stdoutdata.decode()).
replace(
"'",
'"').
replace(
' None',
' "None"') )
170 Queries Tier0DataSvc to get the first condition safe run. 172 @returns: integer, the run number. 173 Raises if connection error, bad response, timeout after retries occur, or if the run number is not available. 175 firstConditionSafeRunAPI =
"firstconditionsaferun" 177 if safeRunDict
is None:
178 errStr =
"""First condition safe run is not available in Tier0DataSvc from URL \"%s\"""" \
179 %( os.path.join( self.
_uri, firstConditionSafeRunAPI ), )
181 errStr +=
""" using proxy \"%s\".""" %(
str( self.
_proxy ), )
183 return int(safeRunDict[
'result'][0])
187 Queries Tier0DataSvc to get the most recent Global Tag for a given workflow. 189 config: Tier0DataSvc API call for the workflow to be looked for; 190 @returns: a string with the Global Tag name. 191 Raises if connection error, bad response, timeout after retries occur, or if no Global Tags are available. 194 gtnames = sorted(
unique( [
str( di[
'global_tag'] )
for di
in data[
'result']
if di[
'global_tag']
is not None ] ))
196 recentGT = gtnames[-1]
199 errStr =
"""No Global Tags for \"%s\" are available in Tier0DataSvc from URL \"%s\"""" \
200 %( config, os.path.join( self.
_uri, config ) )
202 errStr +=
""" using proxy \"%s\".""" %(
str( self.
_proxy ), )
209 print(
' fcsr = %s (%s)' % (t0.getFirstSafeRun(), type(t0.getFirstSafeRun()) ))
210 print(
' reco_config = %s' % t0.getGlobalTag(
'reco_config'))
211 print(
' express_config = %s' % t0.getGlobalTag(
'express_config'))
215 if __name__ ==
'__main__':
def _raise_http_error(curl, response, proxy, timeout, maxTime)
def getFirstSafeRun(self)
def __init__(self, message)
def replace(string, replacements)
def __init__(self, uri, timeOut, maxTime, retries, retryPeriod, proxy, debug)
def _queryTier0DataSvc(self, url)
def __init__(self, curl, response, proxy, timeout, maxTime)
def getGlobalTag(self, config)
def unique(seq, keepstr=True)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
static std::string join(char **cmd)
def setProxy(self, proxy)