U
    
Haa                     @   s  d Z 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
 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mZ dd	lmZ dd
lmZ ddlm Z  ddl!m"Z"m#Z# ddl$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/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ eAeBZCdd ZDdd ZEG dd dZFd-dd ZGd!d" ZHd#d$ ZId.d%d&ZJd/d'd(ZKd)d* ZLG d+d, d,ZMdS )0z)Prepares a distribution for installation
    N)DictIterableListOptionalTuple)canonicalize_name)Distribution))make_distribution_for_install_requirement)InstalledDistribution)DirectoryUrlHashUnsupportedHashMismatchHashUnpinnedInstallationErrorNetworkConnectionErrorPreviousBuildDirErrorVcsHashUnsupported)PackageFinder)Link)Wheel)BatchDownloader
Downloader)HTTPRangeRequestUnsupporteddist_from_wheel_url)
PipSession)InstallRequirement)RequirementTracker)
deprecated)copy2_fixed)HashesMissingHashes)
indent_log)display_pathhide_urlis_installable_dirrmtree)TempDirectory)unpack_file)vcsc              	   C   s2   t | }||  ||| W 5 Q R X | S )z(Prepare a distribution for installation.)r	   trackprepare_distribution_metadataget_pkg_resources_distribution)reqreq_trackerfinderbuild_isolationabstract_dist r0   D/tmp/pip-unpacked-wheel-tx790h60/pip/_internal/operations/prepare.py_get_prepared_distribution2   s    r2   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"   r3   )linklocationvcs_backendr0   r0   r1   unpack_vcs_link@   s    r;   c                   @   s   e Zd Zdd ZdS )Filec                 C   s*   || _ |d kr t|d | _n|| _d S )Nr   )path	mimetypes
guess_typecontent_type)selfr=   r@   r0   r0   r1   __init__I   s    zFile.__init__N)__name__
__module____qualname__rB   r0   r0   r0   r1   r<   G   s   r<   c                 C   sV   t ddd}d }|r t| ||}|r.|}d }n|| |j\}}|rL|| t||S )Nr7   Tkindglobally_managed)r%   _check_download_dirr=   check_against_pathr<   )r8   downloaddownload_dirhashestemp_diralready_downloaded_path	from_pathr@   r0   r0   r1   get_http_urlR   s      
rQ   c              
   C   sL   zt | | W n8 tjk
rF } ztd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   shutilSpecialFileErrorloggerwarningstr)srcdester0   r0   r1   _copy2_ignoring_special_filesm   s    rZ   c                    sL   t j|}t j|t j| fdd}tj ||dtd d S )Nc                    s6   g }|  kr|ddg7 }t j| kr2|g7 }|S )Nz.toxz.nox)osr=   abspath)dnamesskippedsourceZtarget_basenameZtarget_dirnamer0   r1   ignore   s    
z!_copy_source_tree.<locals>.ignoreT)rb   symlinkscopy_function)r[   r=   r\   basenamedirnamerR   copytreerZ   )ra   targetZtarget_abspathrb   r0   r`   r1   _copy_source_tree   s    ri   c                 C   s<   d}|rt | ||}|r|}n| j}|r2|| t|dS )z,Get file and optionally check its hash.
    N)rI   	file_pathrJ   r<   )r8   rL   rM   rO   rP   r0   r0   r1   get_file_url   s    	  
rk   c                 C   s   | j rt| | dS |  rPtddddd tj|r@t| t| j	| dS | j
rft| ||d}nt| |||d}| jst|j||j |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.
    NzA future pip version will change local packages to be built in-place without first copying to a temporary directory. We recommend you use --use-feature=in-tree-build to test your packages with this new behavior before it becomes the default.
z21.3i  )replacementgone_inissue)rM   )is_vcsr;   is_existing_dirr   r[   r=   isdirr$   ri   rj   is_filerk   rQ   is_wheelr&   r@   )r8   r9   rK   rL   rM   filer0   r0   r1   
unpack_url   s2    

	ru   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.)r[   r=   joinfilenameexistsrT   inforJ   r   rU   unlink)r8   rL   rM   download_pathr0   r0   r1   rI      s    
rI   c                       s~   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d ZdddZ	dddZ
dddZdd Zdd Zdd Zdd Z  ZS )RequirementPreparerzPrepares a Requirement
    c                    st   t    || _|| _|| _|| _t||| _t||| _	|| _
|| _|| _|	| _|
| _|| _|| _i | _d| _d S )N) r}   )superrB   src_dir	build_dirr,   _sessionr   	_downloadr   _batch_downloadr-   rL   r.   require_hashesuse_user_siteuse_lazy_wheelin_tree_build_downloaded_previous_requirement_header)rA   r   rL   r   r.   r,   sessionprogress_barr-   r   r   Z
lazy_wheelr   	__class__r0   r1   rB     s     
zRequirementPreparer.__init__c              	   C   s   |j jr$|js$d}tt|j j}nd}t|jp2|}||f| jkrZ||f| _t	|| |jrt
  t	d|j j W 5 Q R X dS )z3Provide context for the requirement being prepared.zProcessing %szCollecting %szUsing cached %sN)r8   rr   Zoriginal_link_is_in_wheel_cacherV   r!   rj   r+   r   rT   ry   r    rw   )rA   r+   messageZinformationr0   r0   r1   _log_preparing_linkO  s    
z'RequirementPreparer._log_preparing_linkc                 C   sj   |j jrdS |jdkst|j  r8| jr8|j j|_dS |j| jd|d t	|jrft
d||jdS )z1Ensure source_dir of a linked InstallRequirement.NT)Z
autodeleteparallel_buildszpip can't proceed with requirements '{}' due to apre-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.)r8   rs   
source_dirr6   rp   r   rj   ensure_has_source_dirr   r#   r   format)rA   r+   r   r0   r0   r1   _ensure_link_req_src_dira  s$    

 z,RequirementPreparer._ensure_link_req_src_dirc                 C   sX   | j s|jddS |jjr t |j r0t |jd krF|jsFt	 |jddpVt
 S )NT)trust_internetF)r   rM   r8   ro   r   rp   r   original_link	is_pinnedr   r   )rA   r+   r0   r0   r1   _get_linked_req_hashes  s    
z*RequirementPreparer._get_linked_req_hashesc                 C   s   | j s
dS | jrtd dS |js*|js:td| dS t|j}t|j	}t
d||j |jddd }zt||| jW S  tk
r   td| Y dS X dS )	z-Fetch metadata using lazy wheel, if possible.Nz3Lazy wheel is not used as hash checking is requiredz>Lazy wheel is not used as %r does not points to a remote wheelz+Obtaining dependency information from %s %s#   r   z"%s does not support range requests)r   r   rT   debugrr   rs   r   rw   r   namery   versionr3   splitr   r   r   )rA   r8   wheelr   r3   r0   r0   r1    _fetch_metadata_using_lazy_wheel  s0    


 z4RequirementPreparer._fetch_metadata_using_lazy_wheelFc           
      C   s   t dddj}i }|D ]}|js$t|||j< q| | |}|D ](\}\}}	td|| || }||_qD|D ]}| 	|| qrdS )z>Download any requirements which were only fetched by metadata.r7   TrF   zDownloading link %s to %sN)
r%   r=   r8   r6   r   keysrT   r   local_file_path_prepare_linked_requirement)
rA   partially_downloaded_reqsr   rN   Zlinks_to_fully_downloadr+   Zbatch_downloadr8   filepath_r0   r0   r1   _complete_partial_requirements  s    	
z2RequirementPreparer._complete_partial_requirementsc              
   C   s   |j s
t|j }| | t  d}| jdk	rP|jrP| |}t|j | j|}|dk	rl|df| j|j j	< n(| 
|}|dk	rd|_|W  5 Q R  S | ||W  5 Q R  S Q R X dS )z3Prepare a requirement to be obtained from req.link.NT)r8   r6   r   r    rL   rs   r   rI   r   r3   r   needs_more_preparationr   )rA   r+   r   r8   rj   rM   Z
wheel_distr0   r0   r1   prepare_linked_requirement  s    



z.RequirementPreparer.prepare_linked_requirementc                 C   s   dd |D }|D ]P}| j dk	r|jjr| |}t|j| j |}|dk	r|df| j|jj< d|_qg }|D ]"}|jr|| ql| 	|| ql| j
||d dS )z,Prepare linked requirements more, if needed.c                 S   s   g | ]}|j r|qS r0   )r   ).0r+   r0   r0   r1   
<listcomp>  s      zHRequirementPreparer.prepare_linked_requirements_more.<locals>.<listcomp>NF)r   )rL   r8   rs   r   rI   r   r3   r   appendr   r   )rA   reqsr   r+   rM   rj   r   r0   r0   r1    prepare_linked_requirements_more  s"    
 z4RequirementPreparer.prepare_linked_requirements_morec           
   
   C   s   |j s
t|j }| || | |}| r:| jr:d }n|j| jkrzt||j	| j
| j|}W q tk
r } ztd|||W 5 d }~X Y qX n(| j|j \}}|r|| t||}|r|j|_t|| j| j| j}	|	S )NzDCould not install requirement {} because of HTTP error {} for URL {})r8   r6   r   r   rp   r   r3   r   ru   r   r   rL   r   r   r   rJ   r<   r=   r   r2   r,   r-   r.   )
rA   r+   r   r8   rM   
local_fileexcrj   r@   distr0   r0   r1   r     sH    

     

   z/RequirementPreparer._prepare_linked_requirementc                 C   s   | j d k	st|jd k	st|j}|js6| rF|jrF|| j  d S | r^td| d S |j	d krld S t
j| j |j}t
j|st|j	| t|}td| d S )NzENot copying link to destination directory since it is a directory: %szSaved %s)rL   r6   r8   ro   rp   editablearchiverT   r   r   r[   r=   rv   rw   rx   rR   copyr!   ry   )rA   r+   r8   download_locationr{   r0   r0   r1   save_linked_requirement?  s&    
z+RequirementPreparer.save_linked_requirementc              	   C   sx   |j stdtd| t N | jr6td||| j	 |
  t|| j| j| 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.)r   r6   rT   ry   r    r   r   r   r   r   update_editabler2   r,   r-   r.   check_if_existsr   )rA   r+   r   r0   r0   r1   prepare_editable_requirementY  s&       z0RequirementPreparer.prepare_editable_requirementc              
   C   st   |j std|dk	s&td|j td|||j j t * | jrRtd t	|
 W  5 Q R  S Q R X d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_byr6   r   rT   ry   r   r    r   r   r
   r*   )rA   r+   skip_reasonr0   r0   r1   prepare_installed_requirementv  s$    
  z1RequirementPreparer.prepare_installed_requirement)F)F)F)rC   rD   rE   __doc__rB   r   r   r   r   r   r   r   r   r   r   r   __classcell__r0   r0   r   r1   r|     s   3#!  
!

%r|   )NN)NN)NN)Nr   loggingr>   r[   rR   typingr   r   r   r   r   pip._vendor.packaging.utilsr   Zpip._vendor.pkg_resourcesr   pip._internal.distributionsr	   %pip._internal.distributions.installedr
   pip._internal.exceptionsr   r   r   r   r   r   r   "pip._internal.index.package_finderr   pip._internal.models.linkr   pip._internal.models.wheelr   pip._internal.network.downloadr   r   Z pip._internal.network.lazy_wheelr   r   pip._internal.network.sessionr   pip._internal.req.req_installr   pip._internal.req.req_trackerr   pip._internal.utils.deprecationr   pip._internal.utils.filesystemr   pip._internal.utils.hashesr   r   pip._internal.utils.loggingr    pip._internal.utils.miscr!   r"   r#   r$   pip._internal.utils.temp_dirr%   pip._internal.utils.unpackingr&   pip._internal.vcsr'   	getLoggerrC   rT   r2   r;   r<   rQ   rZ   ri   rk   ru   rI   r|   r0   r0   r0   r1   <module>   sV   $	
  
!  
"  
?