U
    @vgQ                     @   sV  d Z ddlZddl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 ddlmZ ddlmZmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ e'rddl,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 ddl
m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddlm>Z> e	re2de-e?e.e? ge.e? f e@dddZAn8e2de-e?e?gdf e-e?e.e? ge.e? f e@e@dddZAeBeCZDdd  ZEd!d" ZFd#d$ ZGd5d%d&ZHd'd( ZId)d* ZJd6d+d,ZKd7d-d.ZLd/d0 ZMd1d2 ZNG d3d4 d4eOZPdS )8z)Prepares a distribution for installation
    N)requests)PY2))make_distribution_for_install_requirement)InstalledDistribution)DirectoryUrlHashUnsupportedHashMismatchHashUnpinnedInstallationErrorPreviousBuildDirErrorVcsHashUnsupported)copy2_fixed)MissingHashes)
indent_log)write_delete_marker_file)ask_path_exists
backup_dirdisplay_pathhide_urlpath_to_displayrmtree)TempDirectory)MYPY_CHECK_RUNNING)unpack_file)vcs)CallableListOptionalTuple)	TypedDict)AbstractDistribution)PackageFinder)Link)
Downloader)InstallRequirement)RequirementTracker)HashesCopytreeKwargsignoresymlinksF)total)copy_functionr(   ignore_dangling_symlinksr)   c              	   C   s.   t | }||  ||| W 5 Q R X |S )z-Prepare a distribution for installation.
    )r   trackprepare_distribution_metadata)reqreq_trackerfinderbuild_isolationabstract_dist r4   D/tmp/pip-unpacked-wheel-gw11q0wt/pip/_internal/operations/prepare.py_get_prepared_distributionT   s    	r6   c                 C   s0   t | j}|d k	st|j|t| jd d S )N)url)r   get_backend_for_schemeschemeAssertionErrorunpackr   r7   )linklocationvcs_backendr4   r4   r5   unpack_vcs_linkc   s    r?   c                 C   s   d}t j||j}t j|rtdt|d}|dkrBd}nj|dkrft	dt| t 
| nF|dkrt|}t	d	t|t| t|| n|d
krtd |rt| | tdt| d S )NTz8The file {} exists. (i)gnore, (w)ipe, (b)ackup, (a)abort)iwbar@   FrA   zDeleting %srB   zBacking up %s to %srC   zSaved %s)ospathjoinfilenameexistsr   formatr   loggerwarningremover   shutilmovesysexitcopyinfo)rH   r=   r<   rR   download_locationresponse	dest_filer4   r4   r5   
_copy_filej   s6    
rW   c           	      C   s\   t ddd}d }|r t| ||}|r8|}t|d }nt| ||j|\}}t||| |S )Nr;   T)kindglobally_managedr   )r   _check_download_dir	mimetypes
guess_type_download_http_urlrF   r   )	r<   r=   
downloaderdownload_dirhashestemp_diralready_downloaded_path	from_pathcontent_typer4   r4   r5   unpack_http_url   s&         re   c              
   C   sT   zt | | W n@ tjk
rN } z tdt|t| t| W 5 d}~X Y nX dS )zCopying special files is not supported, but as a convenience to users
    we skip errors copying them. This supports tools that may create e.g.
    socket files in the project source directory.
    z>Ignoring special file error '%s' encountered copying %s to %s.N)r   rN   SpecialFileErrorrK   rL   strr   )srcdester4   r4   r5   _copy2_ignoring_special_files   s    rk   c                    s8    fdd}t |dd}ts$t|d< tj |f| d S )Nc                    s   |  krddgS g S )Nz.toxz.noxr4   )dnamessourcer4   r5   r(      s    z!_copy_source_tree.<locals>.ignoreTr'   r+   )dictr   rk   rN   copytree)ro   targetr(   kwargsr4   rn   r5   _copy_source_tree   s
    rt   c                 C   s~   | j }|  r0tj|r"t| t|| dS d}|rDt| ||}|rN|}n|}|r`|| t	
|d }t||| |S )zUnpack link into location.
    Nr   )	file_pathis_existing_dirrE   rF   isdirr   rt   rZ   check_against_pathr[   r\   r   )r<   r=   r_   r`   	link_pathrb   rc   rd   r4   r4   r5   unpack_file_url   s*    	
  
rz   c                 C   s@   | j rt| | dS | jr*t| |||dS t| ||||dS dS )a_  Unpack link into location, downloading if required.

    :param hashes: A Hashes object, one of whose embedded hashes must match,
        or HashMismatch will be raised. If the Hashes is empty, no matches are
        required, and unhashable types of requirements (like VCS ones, which
        would ordinarily raise HashUnsupported) are allowed.
    Nr`   )is_vcsr?   is_filerz   re   )r<   r=   r^   r_   r`   r4   r4   r5   
unpack_url   s    
r~   c              	   C   sf   || }t j||j}t|d}|jD ]}|| q*W 5 Q R X |rR|| ||jj	
ddfS )z6Download link url into temp_dir using provided sessionwbzcontent-type )rE   rF   rG   rH   openchunkswriterx   rU   headersget)r<   r^   ra   r`   downloadru   content_filechunkr4   r4   r5   r]   #  s    

r]   c                 C   sp   t j|| j}t j|s dS td| |rlz|| W n, tk
rj   t	d| t 
| Y dS X |S )z Check download_dir for previously downloaded file with correct hash
        If a correct file is found return its path else None
    NzFile was already downloaded %sz;Previously-downloaded file %s has bad hash. Re-downloading.)rE   rF   rG   rH   rI   rK   rS   rx   r   rL   unlink)r<   r_   r`   download_pathr4   r4   r5   rZ   8  s    
rZ   c                       sD   e Zd ZdZ fddZedd Zdd Zdd	 Zd
d Z	  Z
S )RequirementPreparerzPrepares a Requirement
    c                    sN   t t|   || _|| _|| _|| _|| _|| _|| _	|| _
|	| _|
| _d S )N)superr   __init__src_dir	build_dirr0   r^   r1   r_   wheel_download_dirr2   require_hashesuse_user_site)selfr   r_   r   r   r2   r0   r^   r1   r   r   	__class__r4   r5   r   V  s    zRequirementPreparer.__init__c                 C   s:   | j s
dS tj| j rdS td td| j d S )NFTz!Could not find download directoryz0Could not find or access download directory '{}')r_   rE   rF   rI   rK   criticalr	   rJ   )r   r4   r4   r5   _download_should_save  s    
z)RequirementPreparer._download_should_savec           
      C   s   |j s
t|j }|jdkr2|j}tdt| ntd|jp@| t  |j	dks\t|
| j tjtj|j	drtd||j	| jr|jrt n| rt |js|jst |j| j d}| jr|st }| j}|jr| jr| j}zt||j	| j ||d}W nF t!j"k
r^ } z$t#d	|| t$d
|||W 5 d}~X Y nX |rl||_%|jr|rd}nd}nd}|rt&|j	 t'|| j(| j)| j*}	|r| rtd n,|rtjtj||j+st,||| | j-r|jr|.| j W 5 Q R X |	S )zCPrepare a requirement that would be obtained from req.link
        filezProcessing %szCollecting %sNzsetup.pyzpip can't proceed with requirements '{}' due to a pre-existing build directory ({}). This is likely due to a previous installation that failed. pip is being responsible and not assuming it can delete this. Please delete it and try again.)trust_internetr{   z4Could not install requirement %s because of error %szDCould not install requirement {} because of HTTP error {} for URL {}TFz*Link is a directory, ignoring download_dir)/r<   r:   r9   ru   rK   rS   r   r/   r   
source_dirensure_has_source_dirr   rE   rF   rI   rG   r
   rJ   r   r|   r   rv   r   original_link	is_pinnedr   r`   r   r_   is_wheelr   r~   r^   r   	HTTPErrorr   r	   local_file_pathr   r6   r0   r1   r2   rH   rW   r   archive)
r   r/   r<   rF   r`   r_   
local_pathexcautodelete_unpackedr3   r4   r4   r5   prepare_linked_requirement  s    


 
   
  
   
z.RequirementPreparer.prepare_linked_requirementc              	   C   s   |j stdtd| t f | jr6td||| j	 |
| j  t|| j| j| j}| jrv|| j || j W 5 Q R X |S )z(Prepare an editable requirement
        z-cannot prepare a non-editable req as editablezObtaining %szoThe editable requirement {} cannot be installed when requiring hashes, because there is no single file to hash.)editabler:   rK   rS   r   r   r	   rJ   r   r   update_editabler   r6   r0   r1   r2   r   r_   check_if_existsr   )r   r/   r3   r4   r4   r5   prepare_editable_requirement  s*       z0RequirementPreparer.prepare_editable_requirementc              	   C   sh   |j std|dk	s&td|j td|||j j t  | jrRtd t	|}W 5 Q R X |S )z1Prepare an already-installed requirement
        z(req should have been satisfied but isn'tNzAdid not get skip reason skipped but req.satisfied_by is set to {}zRequirement %s: %s (%s)zSince it is already installed, we are trusting this package without checking its hash. To ensure a completely repeatable environment, install into an empty virtualenv.)
satisfied_byr:   rJ   rK   rS   versionr   r   debugr   )r   r/   skip_reasonr3   r4   r4   r5   prepare_installed_requirement4  s&    
  z1RequirementPreparer.prepare_installed_requirement)__name__
__module____qualname____doc__r   propertyr   r   r   r   __classcell__r4   r4   r   r5   r   R  s   -
 r   )NN)NN)NN)Qr   loggingr[   rE   rN   rP   pip._vendorr   Zpip._vendor.sixr   pip._internal.distributionsr   %pip._internal.distributions.installedr   pip._internal.exceptionsr   r   r   r	   r
   r   pip._internal.utils.filesystemr   pip._internal.utils.hashesr   pip._internal.utils.loggingr    pip._internal.utils.marker_filesr   pip._internal.utils.miscr   r   r   r   r   r   pip._internal.utils.temp_dirr   pip._internal.utils.typingr   pip._internal.utils.unpackingr   pip._internal.vcsr   typingr   r   r   r   Zmypy_extensionsr   r   "pip._internal.index.package_finderr    pip._internal.models.linkr!   pip._internal.network.downloadr"   pip._internal.req.req_installr#   pip._internal.req.req_trackerr$   r%   rg   boolr&   	getLoggerr   rK   r6   r?   rW   re   rk   rt   rz   r~   r]   rZ   objectr   r4   r4   r4   r5   <module>   s     	
#  
   
2  
#