
                             S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSK	r	SSK
r
SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKrS rS r " S S\5      rSS jrSS jrSS jrSS jrSS jrg)zpCustomized versions of runners in subprocess.

Some of this is just for python 2 support and can be simplified.
    )absolute_import)division)print_function)unicode_literalsN)utils)json_stream)config)update_manager)filesc                    [         R                  " 5       R                  (       ac  [        R                  R                  U /5      (       a>  [        R                  R                  [         R                  " 5       R                  SU 5      $ g)zFinds the path to a component or install it.

Args:
  component_name: Name of the component.

Returns:
  Path to the component. Returns None if the component can't be found.
binN)	r	   Pathssdk_rootr
   UpdateManagerEnsureInstalledAndRestartospathjoin)component_names    5lib/googlecloudsdk/command_lib/code/run_subprocess.py_FindOrInstallComponentr   %   sS     lln""<<n=MNN77<<//GG	    c                     [        U 5      =(       d    [        R                  " U 5      nU(       d  [        SU -  5      eU$ )zFinds the path to an executable, preferring the gcloud packaged version.

Args:
  exe: Name of the executable.

Returns:
  Path to the executable.
Raises:
  EnvironmentError: The executable can't be found.
zUnable to locate %s.)r   
file_utilsFindExecutableOnPathEnvironmentError)exer   s     r   GetGcloudPreferredExecutabler   5   s7     
!	%	M)H)H)M$	
1C7
88	+r   c                   6    \ rS rSrSr S	S jrS rS rS rSr	g)
_TimeoutThreadF   zA context manager based on threading.Timer.

Pass a function to call after the given time has passed. If you exit before
the timer fires, nothing happens. If you exit after we've had to call the
timer function, we raise TimeoutError at exit time.
c                 6    Xl         X l        X0l        S U l        g N)functimeout_secerror_formattimer)selfr$   r%   r&   s       r   __init___TimeoutThread.__init__N   s     I"$DJr   c                 &    U R                  5         U $ r#   )Resetr(   s    r   	__enter___TimeoutThread.__enter__W   s    JJLKr   c                     U R                   b  U R                   R                  5         [        R                  " U R                  U R
                  5      U l         U R                   R                  5         g r#   )r'   cancel	threadingTimerr%   r$   startr-   s    r   r,   _TimeoutThread.Reset[   sH    zz
jj!1!1499=DJJJr   c                     U R                   R                  R                  5       nU R                   R                  5         U(       a7  [        R
                  " U R                  R                  U R                  S95      eg )N)r%   )	r'   finishedis_setr1   r   TimeoutErrorr&   formatr%   )r(   exc_type	exc_value	traceback	timed_outs        r   __exit___TimeoutThread.__exit__a   sd    

##**,IJJ



"
"t/?/?
"
@B B r   )r&   r$   r%   r'   N)z,Task ran for more than {timeout_sec} seconds)
__name__
__module____qualname____firstlineno____doc__r)   r.   r,   r?   __static_attributes__ r   r   r    r    F   s"     KBr   r    c                 8   [         R                  " [        R                  5       nUnUnSnU(       a  SnSnU(       a  [        R
                  n[        R                  " XXgS9n[        UR                  U5         U(       a&  UR                  [        R                  " U5      5        OUR                  5         SSS5        UR                  S:w  a!  [        R                  " UR                  U 5      e SSS5        g! , (       d  f       NI= f! , (       d  f       g= f)z?Run command and optionally send the output to /dev/null or nul.N)stdoutstderrstdinr   )r   
FileWriterr   devnull
subprocessPIPEPopenr    killcommunicatesixensure_binarywait
returncodeCalledProcessError)	cmdr%   show_outputinptrM   rI   rJ   rK   ps	            r   Runr\   j   s    RZZ(GFFEffooe 	FHA		,		c''-.		 
-
 	||q))!,,<< % )( 
-	, )(s$   AD9>C:79D:
D	D
Dc                    [         R                  " U [         R                  U(       a  S O[         R                  S9n[        UR                  U5         UR                  5       u  pES S S 5        UR                  S:w  a!  [         R                  " UR                  U 5      e[        R                  " W5      $ ! , (       d  f       NU= f)NrI   rJ   r   )
rN   rP   rO   r    rQ   rR   rV   rW   rS   ensure_text)rX   r%   show_stderrr[   rI   _s         r   
_GetStdoutrb      s    	__ Tjoo7! affk*IF +\\Q

'
'c
::		  	 +*s   B44
Cc                 h    [        XUS9nU(       a  UR                  5       nUR                  5       nU$ )aO  Run command and get its stdout as a list of lines.

Args:
  cmd: List of executable and arg strings.
  timeout_sec: Command will be killed if it exceeds this.
  show_stderr: False to suppress stderr from the command.
  strip_output: Strip head/tail whitespace before splitting into lines.

Returns:
  List of lines (without newlines).
r`   )rb   strip
splitlines)rX   r%   r`   strip_outputrI   liness         r   GetOutputLinesri      s1     cK@&\\^F



%	,r   c                 ^    [        XUS9n[        R                  " UR                  5       5      $ )zRun command and get its JSON stdout as a parsed dict.

Args:
  cmd: List of executable and arg strings.
  timeout_sec: Command will be killed if it exceeds this.
  show_stderr: False to suppress stderr from the command.

Returns:
  Parsed JSON.
rd   )rb   jsonloadsre   )rX   r%   r`   rI   s       r   GetOutputJsonrm      s%     cK@&	FLLN	##r   c              #     #    [         R                  " U [         R                  U(       a  SO[         R                  S9n[        UR                  USS9 n[
        R                  " UR                  5       H  nUR                  5         Uv   M     UR                  5         SSS5        UR                  S:w  a!  [         R                  " UR                  U 5      eg! , (       d  f       N@= f7f)a  Run command and get its output streamed as an iterable of dicts.

Args:
  cmd: List of executable and arg strings.
  event_timeout_sec: Command will be killed if we don't get a JSON line for
    this long. (This is not the same as timeout_sec above).
  show_stderr: False to suppress stderr from the command.

Yields:
  Parsed JSON.

Raises:
  CalledProcessError: cmd returned with a non-zero exit code.
  TimeoutError: cmd has timed out.
Nr^   z.No subprocess output for {timeout_sec} seconds)r&   r   )rN   rP   rO   r    rQ   r   ReadJsonStreamrI   r,   rU   rV   rW   )rX   event_timeout_secr`   r[   r'   objs         r   StreamOutputJsonrr      s       	__ Tjoo7! ffCE IN))!((3kkmi 4 FFHE \\Q

'
'c
:: E Es   AC*AC:C*
C'#C*)TN)T)TF) rE   
__future__r   r   r   r   rk   os.pathr   rN   r2   googlecloudsdk.api_lib.computer   googlecloudsdk.command_lib.coder   googlecloudsdk.corer	   googlecloudsdk.core.updaterr
   googlecloudsdk.core.utilr   r   rS   r   r   objectr    r\   rb   ri   rm   rr   rG   r   r   <module>r{      sh   
 '  % '     0 7 & 6 8 
 "!BV !BH=0	!&$;r   