@@ -144,20 +144,26 @@ def create_proc():
144144 bearer_token_file = Config .BEARER_TOKEN_FILE
145145
146146 tokens = [
147- 'htgettoken' ,
147+ #'python',
148+ #'-u',
149+ 'hwdb-htgettoken' ,
148150 '-q' ,
149151 f'--configdir={ config_dir } ' ,
150152 f'--vaulttokenfile={ vault_token_file } ' ,
151153 f'--outfile={ bearer_token_file } ' ,
152- f'--vaultserver=htvaultprod.fnal.gov' ,
153- f'--issuer=fermilab'
154+ '--vaultserver=htvaultprod.fnal.gov' ,
155+ '--issuer=fermilab' ,
156+ #'--web-open-command=',
154157 ]
155158
159+ #tokens = " ".join(tokens)
160+
156161 try :
157162 proc = subprocess .Popen (
158163 tokens ,
159164 stdout = subprocess .PIPE ,
160- stderr = subprocess .PIPE )
165+ stderr = subprocess .PIPE ,
166+ )
161167 except FileNotFoundError :
162168 msg = ("htgettoken not found. Have you installed it? "
163169 "(try 'pip install htgettoken')" )
@@ -199,7 +205,10 @@ def read_all(self):
199205 except subprocess .TimeoutExpired :
200206 outs , errs = proc .communicate ()
201207 #.....................................................................
202- def display_message (msg ):
208+ def display_message (msg , time_elapsed ):
209+ outer_width = 80
210+ inner_width = 78
211+
203212 if threading .current_thread ().name == "MainThread" :
204213 erase_msg = "" .join ([
205214 Style .cursor_abs_horizontal (1 ),
@@ -210,11 +219,17 @@ def display_message(msg):
210219 sys .stdout .flush ()
211220
212221 msg = ' \n ' .join ([Style .info (s ) for s in msg .split ('\n ' )])
213- inner = MessageBox (msg , width = 66 , outer_border = 'normal' , border_color = Style .info ._fg )
222+ inner = MessageBox (
223+ msg ,
224+ width = inner_width ,
225+ outer_border = 'normal' ,
226+ border_color = Style .info ._fg )
214227
215228 info = ' \n ' .join ([
216229 '' ,
217- Style .warning ('The call to htgettoken is taking longer than expected.' ),
230+ Style .warning (
231+ #f'The call to htgettoken is taking longer than expected. ({time_elapsed})'),
232+ f'The call to htgettoken is taking longer than expected.' ),
218233 '' ,
219234 'htgettoken may have attempted to open a browser window. Use this' ,
220235 'window to complete your authentication.' ,
@@ -228,7 +243,11 @@ def display_message(msg):
228243
229244 msg2 = '\n ' .join ([info , inner ])
230245
231- outer = MessageBox (msg2 , width = 72 , outer_border = 'strong' , border_color = Style .warning ._fg )
246+ outer = MessageBox (
247+ msg2 ,
248+ width = outer_width ,
249+ outer_border = 'strong' ,
250+ border_color = Style .warning ._fg )
232251 print (outer )
233252 #.....................................................................
234253
@@ -241,41 +260,81 @@ def display_message(msg):
241260
242261 proc = create_proc ()
243262
244- osf = output_so_far (proc .stdout )
263+ osf_stdout = output_so_far (proc .stdout )
264+ osf_stderr = output_so_far (proc .stderr )
245265
246- finished = False
247- displayed_response = False
248- interval = 5
266+ timed_out = None
267+ # displayed_response = False
268+ interval = 10
249269 total_time = 0
250270 max_time = 120
251271
272+ last_stdout = ""
273+ last_stderr = ""
274+
252275 while True :
253276 timed_out = False
254277 try :
255278 proc .wait (interval )
256- finished = True
279+ timed_out = False
257280 except subprocess .TimeoutExpired as err :
258281 timed_out = True
259282
260283 if not timed_out :
261284 break
262285
263286 total_time += interval
264-
265- if not displayed_response :
266- outb = osf .read ()
267- if len (outb ) > 0 :
268- outs = outb .decode ('utf-8' )
269- display_message (outs )
270- displayed_response = True
271-
272287 if total_time >= max_time :
273288 break
274289
290+ current_stdout = osf_stdout .read ().decode ('utf-8' )
291+ current_stderr = osf_stderr .read ().decode ('utf-8' )
292+
293+ if current_stdout != last_stdout :
294+ display_message (current_stdout , total_time )
295+ last_stdout = current_stdout
296+ if current_stderr != last_stderr :
297+ display_message (current_stderr , total_time )
298+ last_stderr = current_stderr
299+
300+
301+ if timed_out :
302+ proc .kill ()
303+ current_stdout = osf_stdout .read_all ().decode ('utf-8' )
304+ current_stderr = osf_stderr .read_all ().decode ('utf-8' )
305+
306+ #if current_stdout != last_stdout:
307+ # display_message(current_stdout)
308+ # last_stdout = current_stdout
309+ #if current_stderr != last_stderr:
310+ # display_message(current_stderr)
311+ # last_stderr = current_stderr
312+
275313 if proc .returncode is None :
276- raise RuntimeError ("The call to htgettoken timed out." )
314+ err = "The call to htgettoken timed out."
315+ msg = "" .join (
316+ [
317+ err , '\n ' ,
318+ f"stdout: { current_stdout } " ,
319+ f"stderr: { current_stderr } " ,
320+ ])
321+ logger .error (msg )
322+ raise RuntimeError (err )
277323 elif proc .returncode != 0 :
278- raise RuntimeError ("The call to htgettoken failed." )
324+ err = "The call to htgettoken failed."
325+ msg = "" .join (
326+ [
327+ err , '\n ' ,
328+ f"stdout: { current_stdout } " ,
329+ f"stderr: { current_stderr } " ,
330+ ])
331+ logger .error (msg )
332+ raise RuntimeError (err )
333+
334+ msg = ("The call to htgettoken succeeded. "
335+ f"stdout: { current_stdout } "
336+ f"stderr: { current_stderr } " )
337+ logger .info (msg )
279338
280339 _refresh_required = False
281340
0 commit comments