3
v,hJ'                 @   s  d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	m
Z
mZ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rd dlmZ e	eeef  ZdZeeeef ed	d
dZee	e ef ed	ddZee	e ef e	e d	ddZee	e ef ee e	e eedddZdee	e ef e ee deee  ee ee
eef  eee  ee ee  ee  edddZ!eed dddZ"dS )     N)TYPE_CHECKINGAnyCallableIterableListMappingOptionalUnion)SpinnerInterfaceopen_spinner)InstallationSubprocessError)VERBOSEsubprocess_logger)
HiddenText)Literalz(----------------------------------------)argsreturnc              G   s6   g }x,| D ]$}t |tr$|j| q
|j| q
W |S )z&
    Create a CommandArgs object.
    )
isinstancelistextendappend)r   Zcommand_argsarg r   ?/tmp/pip-install-dn9_u1zy/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   s0   | ](}t |trtjt|ntj|V  qd S )N)r   r   shlexquotestr).0r   r   r   r   	<genexpr><   s   z&format_command_args.<locals>.<genexpr>)join)r   r   r   r   format_command_args2   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S r   )r   r   Zsecret)r   r   r   r   r   
<listcomp>E   s    z'reveal_command_args.<locals>.<listcomp>r   )r   r   r   r   reveal_command_argsA   s    r$   )cmd_argscwdlinesexit_statusr   c             C   s0   t | }dj|}dj|||t||td}|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.
     zCommand errored out with exit status {exit_status}:
 command: {command_display}
     cwd: {cwd_display}
Complete output ({line_count} lines):
{output}{divider})r(   Zcommand_displayZcwd_displayZ
line_countoutputZdivider)r"   r!   formatlenLOG_DIVIDER)r%   r&   r'   r(   commandr*   msgr   r   r   make_subprocess_output_errorH   s    
r0   FraiseTz"Literal["raise", "warn", "ignore"])cmdshow_stdoutr&   on_returncodeextra_ok_returncodescommand_descextra_environunset_environspinnerlog_failed_cmdstdout_onlyr   c             C   s  |dkrg }|dkrg }|r*t j}tj}n
t j}t}t j |k}| oL|dk	}|dkr^t| }|d| tj	j
 }|r|j| x|D ]}|j|d qW y0tjt| tjtj|
stjntj||dd}W n8 tk
r } z|	rt jd||  W Y dd}~X nX g }|
s|jst|js&t|jj  xN|jj }|sDP |j }|j|d  || |r2|srt|j  q2W z|j  W d|jr|jj  X dj|}n\|j \}}x|j D ]}|| qW |j| x|j D ]}|| qW |j| |}|j o|j |k}|rH|s,t|r>|j!d n
|j!d	 |r|d
kr| r|	rt"| |||j d}t j#| t$|j |n8|dkrt j%d||j | n|dkrnt&d||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backslashreplace)stdinstdoutstderrr&   enverrorsz#Error %s while executing command %s
r)   errordoner1   )r%   r&   r'   r(   warnz$Command "%s" had error code %s in %signorezInvalid value: on_returncode=)'r   infologgingINFOverboser   getEffectiveLevelr"   osenvironcopyupdatepop
subprocessPopenr$   PIPESTDOUT	Exceptioncriticalr>   AssertionErrorr=   closereadlinerstripr   spinwaitr!   communicate
splitlines
returncodefinishr0   rC   r   warning
ValueError)r2   r3   r&   r4   r5   r6   r7   r8   r9   r:   r;   Zlog_subprocessZ
used_levelZshowing_subprocessZuse_spinnerr@   nameprocexc
all_outputliner*   outerrZout_lineZerr_lineZproc_had_errorr/   r   r   r   call_subprocessk   s    















rj   .)messager   c                s2   dt t tt ttttf  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.
    N)r2   r&   r7   r   c                s(   t  }t| |||d W d Q R X d S )N)r&   r7   r9   )r   rj   )r2   r&   r7   r9   )rk   r   r   runner  s    
z+runner_with_spinner_message.<locals>.runner)NN)r   r   r   r   r   )rk   rl   r   )rk   r   runner_with_spinner_message  s    	 	rm   )
FNr1   NNNNNTF).N)#rH   rL   r   rQ   typingr   r   r   r   r   r   r   r	   Zpip._internal.cli.spinnersr
   r   pip._internal.exceptionsr   pip._internal.utils.loggingr   r   pip._internal.utils.miscr   r   r   ZCommandArgsr-   r   r"   r$   intr0   boolrj   rm   r   r   r   r   <module>   sB   (           T 