@@ -23,7 +23,7 @@ def __init__(self, message):
23
23
24
24
def unique (seq , keepstr = True ):
25
25
t = type (seq )
26
- if t in ( unicode , str ) :
26
+ if t is str :
27
27
t = (list , t ('' ).join )[bool (keepstr )]
28
28
try :
29
29
remaining = set (seq )
@@ -90,43 +90,60 @@ def unsetDebug( self ):
90
90
def setProxy ( self , proxy ):
91
91
self ._proxy = proxy
92
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
- """
93
+ def _getCerts ( self ) -> str :
94
+ cert_path = os .getenv ('X509_USER_CERT' , '' )
95
+ key_path = os .getenv ('X509_USER_KEY' , '' )
100
96
101
- userAgent = "User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" % ( sys .version_info [ :3 ] + ( pycurl .version_info ()[ 1 ], ) )
97
+ certs = ""
98
+ if cert_path :
99
+ certs += f' --cert { cert_path } '
100
+ else :
101
+ logging .warning ("No certificate, nor proxy provided for Tier0 access" )
102
+ if key_path :
103
+ certs += f' --key { key_path } '
104
+ return certs
105
+
106
+ def _curlQueryTier0 ( self , url :str , force_debug :bool = False ):
107
+ userAgent = "User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" \
108
+ % ( sys .version_info [ :3 ] + ( pycurl .version_info ()[ 1 ], ) )
109
+ debug = "-v" if self ._debug or force_debug else "-s -S"
102
110
103
111
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 )
112
+ certs = ""
113
+ if self ._proxy :
114
+ proxy = f"--proxy { self ._proxy } "
115
+ else :
116
+ certs = self ._getCerts ()
117
+
118
+ cmd = '/usr/bin/curl -k -L --user-agent "%s" %s --connect-timeout %i --retry %i %s %s %s' \
119
+ % (userAgent , proxy , self ._timeOut , self ._retries , debug , url , certs )
110
120
111
121
# time the curl to understand if re-tries have been carried out
112
122
start = time .time ()
113
123
process = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
114
124
(stdoutdata , stderrdata ) = process .communicate ()
115
- retcode = process .returncode
116
125
end = time .time ()
126
+ return process .returncode , stdoutdata , stderrdata , end - start
127
+
128
+ def _queryTier0DataSvc ( self , url ):
129
+ """
130
+ Queries Tier0DataSvc.
131
+ url: Tier0DataSvc URL.
132
+ @returns: dictionary, from whence the required information must be retrieved according to the API call.
133
+ Raises if connection error, bad response, or timeout after retries occur.
134
+ """
135
+
136
+ retcode , stdoutdata , stderrdata , query_time = self ._curlQueryTier0 (url )
117
137
118
138
if retcode != 0 or stderrdata :
119
139
120
140
# if the first curl has failed, logg its stderror and prepare and independent retry
121
- msg = "looks like curl returned an error: retcode=%s and took %s seconds" % (retcode ,( end - start ) ,)
141
+ msg = "looks like curl returned an error: retcode=%s and took %s seconds" % (retcode , query_time ,)
122
142
msg += ' msg = "' + str (stderrdata )+ '"'
123
143
logging .error (msg )
124
144
125
145
time .sleep (10 )
126
- 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 )
127
- process = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
128
- (stdoutdata , stderrdata ) = process .communicate ()
129
- retcode = process .returncode
146
+ retcode , stdoutdata , stderrdata , query_time = self ._curlQueryTier0 (url , force_debug = True )
130
147
if retcode != 0 :
131
148
msg = "looks like curl returned an error for the second time: retcode=%s" % (retcode ,)
132
149
msg += ' msg = "' + str (stderrdata )+ '"'
0 commit comments