U
    «ÃT_Â&  ã                	   @   s  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 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 erÈd d
lmZmZmZmZmZmZmZmZ d dl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„Z&dd„ Z'dS )é    )Úabsolute_importN)Úshlex_quote)ÚInstallationError)Úconsole_to_strÚstr_to_display)Úsubprocess_logger)Ú
HiddenTextÚpath_to_display)ÚMYPY_CHECK_RUNNING)Úopen_spinner)ÚAnyÚCallableÚIterableÚListÚMappingÚOptionalÚTextÚUnion)ÚSpinnerInterfacez(----------------------------------------c                  G   s2   g }| D ]$}t |tƒr"| |¡ q| |¡ q|S )z&
    Create a CommandArgs object.
    )Ú
isinstanceÚlistÚextendÚappend)ÚargsÚcommand_argsÚarg© r   úB/tmp/pip-unpacked-wheel-8u7dkzi1/pip/_internal/utils/subprocess.pyÚmake_command   s    
r   c                 C   s   d  dd„ | D ƒ¡S )z/
    Format command arguments for display.
    ú c                 s   s,   | ]$}t |tƒrtt|ƒƒnt|ƒV  qd S )N)r   r   r   Ústr©Ú.0r   r   r   r   Ú	<genexpr>;   s   ÿz&format_command_args.<locals>.<genexpr>)Újoin©r   r   r   r   Úformat_command_args1   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   Úsecretr!   r   r   r   Ú
<listcomp>F   s    z'reveal_command_args.<locals>.<listcomp>r   r%   r   r   r   Úreveal_command_argsA   s    ÿr)   c           	      C   sD   t | ƒ}t|dd�}t|ƒ}d |¡}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.
    zcommand bytes)ÚdescÚ zŸCommand 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_displayÚ
line_countÚoutputÚdivider)r&   r   r	   r$   ÚformatÚlenÚLOG_DIVIDER)	Úcmd_argsÚcwdÚlinesr,   Úcommandr-   r.   r0   Úmsgr   r   r   Úmake_subprocess_output_errorK   s    
ü	òr:   FÚraiseTc
              
   C   s4  |dkrg }|dkrg }|r*t j}
tj}nt j}
tj}t  ¡ |k}| oN|dk	}|dkr`t| ƒ}|
d|ƒ tj	 
¡ }|r‚| |¡ |D ]}| |d¡ q†z.tjt| ƒtjtjtj||d�}|j ¡  W n6 tk
rü } z|	rêt  d||¡ ‚ W 5 d}~X Y nX g }t|j ¡ ƒ}|�s�qJ| ¡ }| |d ¡ |
|ƒ |�r| ¡  �qz| ¡  W 5 |j�rj|j ¡  X |j�o||j|k}|�r |�r–| d¡ n
| d¡ |�r*|dk�rð|�sØ|	�rØt| |||jd	�}t   |¡ d
 !|j|¡}t"|ƒ‚n:|dk�rt  #d||j|¡ n|dk�rnt$d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.
    NzRunning command %s)ÚstderrÚstdinÚstdoutr6   Úenvz#Error %s while executing command %sÚ
ÚerrorÚdoner;   )r5   r6   r7   r,   zSCommand errored out with exit status {}: {} Check the logs for full command output.Úwarnz$Command "%s" had error code %s in %sÚignorezInvalid value: on_returncode=%sr+   )'r   ÚinfoÚloggingÚINFOÚdebugÚDEBUGÚgetEffectiveLevelr&   ÚosÚenvironÚcopyÚupdateÚpopÚ
subprocessÚPopenr)   ÚSTDOUTÚPIPEr=   ÚcloseÚ	ExceptionÚcriticalr   r>   ÚreadlineÚrstripr   ÚspinÚwaitÚ
returncodeÚfinishr:   rA   r2   r   ÚwarningÚ
ValueErrorÚreprr$   )ÚcmdÚshow_stdoutr6   Úon_returncodeÚextra_ok_returncodesÚcommand_descÚextra_environÚunset_environÚspinnerÚlog_failed_cmdÚlog_subprocessÚ
used_levelÚshowing_subprocessÚuse_spinnerr?   ÚnameÚprocÚexcÚ
all_outputÚlineÚproc_had_errorr9   Úexc_msgr   r   r   Úcall_subprocessu   sª    


   ü  ÿÿ

ü
ÿ ý

  þ
ÿrt   c                    s   d‡ fdd„	}|S )zÜProvide 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              	      s(   t ˆ ƒ�}t| |||d� W 5 Q R X d S )N)r6   re   rg   )r   rt   )r`   r6   re   rg   ©Úmessager   r   Úrunner  s    
üz+runner_with_spinner_message.<locals>.runner)NNr   )rv   rw   r   ru   r   Úrunner_with_spinner_message   s    
  ýrx   )	FNr;   NNNNNT)(Ú
__future__r   rF   rK   rP   Zpip._vendor.six.movesr   Úpip._internal.exceptionsr   Úpip._internal.utils.compatr   r   Úpip._internal.utils.loggingr   Úpip._internal.utils.miscr   r	   Úpip._internal.utils.typingr
   Úpip._internal.utils.uir   Útypingr   r   r   r   r   r   r   r   r   r    ZCommandArgsr4   r   r&   r)   r:   rt   rx   r   r   r   r   Ú<module>   s>   (
,         ö
 