
(hr*                 @   sz  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z m	 Z	 d  d l
 m Z d  d l m Z m Z d  d l m Z d  d l m Z m Z d  d	 l m Z e r
d  d
 l m Z m Z m Z m Z m Z m Z m Z m Z e e e e f Z  d Z! d d   Z" d d   Z# d d   Z$ d d   Z% d d d d d d d d d d d d 
 Z& d d   Z' d S)    )absolute_importN)shlex_quote)SpinnerInterfaceopen_spinner)InstallationSubprocessError)console_to_strstr_to_display)subprocess_logger)
HiddenTextpath_to_display)MYPY_CHECK_RUNNING)AnyCallableIterableListMappingOptionalTextUnionz(----------------------------------------c              G   sG   g  } x: |  D]2 } t  | t  r2 | j |  q | j |  q W| S)z&
    Create a CommandArgs object.
    )
isinstancelistextendappend)argsZcommand_argsarg r   =/tmp/pip-build-kmztzv_d/pip/pip/_internal/utils/subprocess.pymake_command   s    r   c             C   s   d j  d d   |  D  S)z/
    Format command arguments for display.
     c             s   s<   |  ]2 } t  | t  r* t t |   n	 t |  Vq d  S)N)r   r
   r   str).0r   r   r   r   	<genexpr>6   s   z&format_command_args.<locals>.<genexpr>)join)r   r   r   r   format_command_args+   s    
	r#   c             C   s   d d   |  D S)z=
    Return the arguments in their raw, unredacted form.
    c             S   s.   g  |  ]$ } t  | t  r$ | j n |  q Sr   )r   r
   Zsecret)r    r   r   r   r   
<listcomp>A   s   	 z'reveal_command_args.<locals>.<listcomp>r   )r   r   r   r   reveal_command_args;   s    r%   c       	      C   ss   t  |   } t | d d } t |  } d j |  } d j d | d | d | d t |  d	 | d
 t  } | S)z
    Create and return the error message to use to log a subprocess error
    with command output.

    :param lines: A list of lines, each ending with a newline.
    desczcommand bytes zCommand errored out with exit status {exit_status}:
 command: {command_display}
     cwd: {cwd_display}
Complete output ({line_count} lines):
{output}{divider}exit_statuscommand_displaycwd_displayZ
line_countoutputZdivider)r#   r   r   r"   formatlenLOG_DIVIDER)	cmd_argscwdlinesr(   commandr)   r*   r+   msgr   r   r   make_subprocess_output_errorE   s    		r4   FraiseTc             C   s  | d k r g  } | d k r$ g  } | r? t  j } t j } n t  j } t j } t  j   | k } | os | d k	 } | d k r t |   } | d |  t j	 j
   } | r | j |  x | D] } | j | d  q WyO t j t |   d t j d t j d |
 st j n t j d | d | } WnA t k
 rp} z! |	 r[t  j d | |    WYd d } ~ Xn Xg  } |
 sH| j st  | j st  | j j   xc t | j j    } | sP| j   } | j | d	  | |  | r| s t  | j   qWz | j   Wd | j r5| j j   Xd
 j |  } n | j   \ } } t |  } x | j    D] } | |  qsW| j |  t |  } x | j    D] } | |  qW| j |  | } | j! o| j! | k } | r!| st  | r| j" d  n | j" d  | r| d k r| rq|	 rqt# d |  d | d | d | j!  } t  j$ |  t% | j! |   nL | d k rt  j& d | | j! |  n$ | d k rn t' d j( |    | S)a  
    Args:
      show_stdout: if true, use INFO to log the subprocess's stderr and
        stdout streams.  Otherwise, use DEBUG.  Defaults to False.
      extra_ok_returncodes: an iterable of integer return codes that are
        acceptable, in addition to 0. Defaults to None, which means [].
      unset_environ: an iterable of environment variable names to unset
        prior to calling subprocess.Popen().
      log_failed_cmd: if false, failed commands are not logged, only raised.
      stdout_only: if true, return only stdout, else return both. When true,
        logging of both stdout and stderr occurs when the subprocess has
        terminated, else logging occurs as subprocess output is produced.
    NzRunning command %sstdinstdoutstderrr0   envz#Error %s while executing command %s
r'   errordoner5   r/   r1   r(   warnz$Command "%s" had error code %s in %signorez!Invalid value: on_returncode={!r}))r	   infologgingINFOdebugDEBUGgetEffectiveLevelr#   osenvironcopyupdatepop
subprocessPopenr%   PIPESTDOUT	Exceptioncriticalr7   AssertionErrorr6   closer   readlinerstripr   spinwaitr"   communicate
splitlines
returncodefinishr4   r;   r   warning
ValueErrorr,   )cmdshow_stdoutr0   on_returncodeZextra_ok_returncodescommand_descextra_environZunset_environspinnerZlog_failed_cmdZstdout_onlyZlog_subprocessZ
used_levelZshowing_subprocessZuse_spinnerr9   nameprocexc
all_outputliner+   Z	out_bytesZ	err_bytesoutZout_lineerrZerr_lineZproc_had_errorr3   r   r   r   call_subprocesso   s    					
	
	ri   c                s   d d   f d d  } | S)zProvide a subprocess_runner that shows a spinner message.

    Intended for use with for pep517's Pep517HookCaller. Thus, the runner has
    an API that matches what's expected by Pep517HookCaller.subprocess_runner.
    Nc                s6   t     # } t |  d | d | d | Wd  QRXd  S)Nr0   r`   ra   )r   ri   )r\   r0   r`   ra   )messager   r   runner  s    z+runner_with_spinner_message.<locals>.runnerr   )rj   rk   r   )rj   r   runner_with_spinner_message  s    
rl   )(
__future__r   r@   rE   rJ   Zpip._vendor.six.movesr   Zpip._internal.cli.spinnersr   r   Zpip._internal.exceptionsr   Zpip._internal.utils.compatr   r   Zpip._internal.utils.loggingr	   Zpip._internal.utils.miscr
   r   Zpip._internal.utils.typingr   Ztypingr   r   r   r   r   r   r   r   r   ZCommandArgsr.   r   r#   r%   r4   ri   rl   r   r   r   r   <module>   s:   :
,