
0c@_M              
   @   s(  d  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 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 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! rkd d l& 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 l0 m1 Z1 d d l2 m3 Z3 d d l4 m5 Z5 d d l6 m7 Z7 d d l m8 Z8 e re, d d e' e9 e( e9 g e( e9 f d e: i d d Z; nU e, d d e' e9 e9 g d f d e' e9 e( e9 g e( e9 f d e: d e: i d d Z; e j< e=  Z> d d    Z? d! d"   Z@ Gd# d$   d$ eA  ZB d d d% d&  ZC d' d(   ZD d) d*   ZE d d d+ d,  ZF d d d- d.  ZG d/ d0   ZH d1 d2   ZI Gd3 d4   d4 eA  ZJ d S)5z)Prepares a distribution for installation
    N)PY2))make_distribution_for_install_requirement)InstalledDistribution)DirectoryUrlHashUnsupportedHashMismatchHashUnpinnedInstallationErrorNetworkConnectionErrorPreviousBuildDirErrorVcsHashUnsupported)copy2_fixed)MissingHashes)
indent_log)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symlinkstotalFcopy_functionignore_dangling_symlinksc          
   C   s7   t  |   } | j |    | j | |  Wd QRX| S)z-Prepare a distribution for installation.
    N)r   trackZprepare_distribution_metadata)reqreq_trackerfinderbuild_isolationabstract_dist r/   ?/tmp/pip-build-jynh7p1z/pip/pip/_internal/operations/prepare.py_get_prepared_distributionP   s    	r1   c             C   sD   t  j |  j  } | d  k	 s$ t  | j | d t |  j  d  S)Nurl)r   Zget_backend_for_schemeschemeAssertionErrorunpackr   r2   )linklocationvcs_backendr/   r/   r0   unpack_vcs_link_   s    r9   c               @   s   e  Z d  Z d d   Z d S)Filec             C   s   | |  _  | |  _ d  S)N)pathcontent_type)selfr;   r<   r/   r/   r0   __init__g   s    	zFile.__init__N)__name__
__module____qualname__r>   r/   r/   r/   r0   r:   f   s   r:   c             C   s   t  d d d d  } d  } | r3 t |  | |  } | rU | } t j |  d } n t |  | | j |  \ } } t | |  S)Nkindr5   Zglobally_managedTr   )r   _check_download_dir	mimetypes
guess_type_download_http_urlr;   r:   )r6   
downloaderdownload_dirhashestemp_diralready_downloaded_path	from_pathr<   r/   r/   r0   get_http_urlm   s    rM   c             C   sh   y t  |  |  WnP t j k
 rc } z- t j d t |  t |   t |   WYd d } ~ Xn Xd 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strr   )srcdester/   r/   r0   _copy2_ignoring_special_files   s    		rV   c                s   t  j j |  } t  j j |   t  j j |       f d d   } t d | d d  } t ss t | d <t j	   | |  d  S)Nc                sK   g  } |    k r" | d d g 7} t  j j |    k rG |  g 7} | S)Nz.toxz.nox)osr;   abspath)dnamesZskipped)sourcetarget_basenametarget_dirnamer/   r0   r$      s    z!_copy_source_tree.<locals>.ignorer$   r%   Tr'   )
rW   r;   rX   basenamedirnamedictr   rV   rN   copytree)r[   targetZtarget_abspathr$   kwargsr/   )r[   r\   r]   r0   _copy_source_tree   s    
rd   c             C   si   d } | r t  |  | |  } | r- | } n	 |  j } | rI | j |  t j |  d } t | |  S)z,Get file and optionally check its hash.
    Nr   )rC   	file_pathcheck_against_pathrD   rE   r:   )r6   rH   rI   rK   rL   r<   r/   r/   r0   get_file_url   s    			rg   c             C   s   |  j  r t |  |  d S|  j   rV t j j |  rB t |  t |  j |  d S|  j	 rw t
 |  | d | } n t |  | | d | } |  j s t | 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.
    NrI   )is_vcsr9   is_existing_dirrW   r;   isdirr   rd   re   is_filerg   rM   is_wheelr   r<   )r6   r7   rG   rH   rI   filer/   r/   r0   
unpack_url   s$    	
			rn   c             C   s   | |   } t  j j | | j  } t | d  ( } x | j D] } | j |  q@ WWd QRX| rq | j |  | | j j	 j
 d d  f S)z6Download link url into temp_dir using provided sessionwbNzcontent-type )rW   r;   joinfilenameopenchunkswriterf   responseheadersget)r6   rG   rJ   rI   downloadre   content_filechunkr/   r/   r0   rF     s    rF   c             C   s   t  j j | |  j  } t  j j |  s. d St j d |  | r y | j |  Wn3 t k
 r t j	 d |  t  j
 |  d SYn 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.)rW   r;   rq   rr   existsrP   inforf   r   rQ   unlink)r6   rH   rI   download_pathr/   r/   r0   rC   #  s    	rC   c                   s   e  Z d  Z d Z   f d d   Z e d d    Z d d   Z d d	   Z d
 d   Z	 d d d  Z
 d d   Z d d   Z   S)RequirementPreparerzPrepares a Requirement
    c                sq   t  t |   j   | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _	 | |  _
 |	 |  _ |
 |  _ d  S)N)superr   r>   src_dir	build_dirr+   rG   r,   rH   wheel_download_dirr-   require_hashesuse_user_site)r=   r   rH   r   r   r-   r+   rG   r,   r   r   )	__class__r/   r0   r>   A  s    									zRequirementPreparer.__init__c             C   sO   |  j  s d St j j |  j   r& d St j d  t d j |  j     d  S)NFTz!Could not find download directoryz0Could not find or access download directory '{}')rH   rW   r;   r|   rP   criticalr   format)r=   r/   r/   r0   _download_should_saven  s    	z)RequirementPreparer._download_should_savec             C   sN   | j  j r1 | j  j } t j d t |   n t j d | j pF |  d S)zLog the way the link prepared.zProcessing %szCollecting %sN)r6   rk   re   rP   r}   r   r*   )r=   r*   r;   r/   r/   r0   _log_preparing_link|  s    z'RequirementPreparer._log_preparing_linkc             C   s   | j  j r d S| j d k s% t  | j |  j d d d | t j j t j j	 | j d   r t
 d j | | j    d S)z1Ensure source_dir of a linked InstallRequirement.NZ
autodeleteTparallel_buildszsetup.pyzpip 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.)r6   rl   
source_dirr4   ensure_has_source_dirr   rW   r;   r|   rq   r
   r   )r=   r*   rH   r   r/   r/   r0   _ensure_link_req_src_dir  s    	$z,RequirementPreparer._ensure_link_req_src_dirc             C   s   |  j  s | j d d  S| j j r. t    | j j   rF t    | j d  k rh | j rh t	    | j d d  p t
   S)Ntrust_internetTF)r   rI   r6   rh   r   ri   r   original_link	is_pinnedr   r   )r=   r*   r/   r/   r0   _get_linked_req_hashes  s    				z*RequirementPreparer._get_linked_req_hashesFc       
      C   s  | j  s t  | j  } |  j |  | j rC |  j rC |  j } n	 |  j } t   \|  j | | |  y. t | | j	 |  j
 | d |  j |  } Wn@ t k
 r } z  t d j | | |    WYd d } ~ Xn X| r | j | _ t | |  j |  j |  j  } | r| j   r,t j d  n_ | rt j j | | j  } t j j |  st j | j |  t |  }	 t j d |	  |  j r| j  r| j! |  j  Wd QRX| S)z3Prepare a requirement to be obtained from req.link.rI   zDCould not install requirement {} because of HTTP error {} for URL {}Nz*Link is a directory, ignoring download_dirzSaved %s)"r6   r4   r   rl   r   rH   r   r   rn   r   rG   r   r	   r   r   r;   Zlocal_file_pathr1   r+   r,   r-   ri   rP   r}   rW   rq   rr   r|   rN   copyr   r   rh   archive)
r=   r*   r   r6   rH   
local_fileexcr.   download_locationr   r/   r/   r0   prepare_linked_requirement  sD    		
%			z.RequirementPreparer.prepare_linked_requirementc             C   s   | j  s t d   t j d |  t    |  j rM t d j |    | j |  j	  | j
 |  j  t | |  j |  j |  j  } |  j r | j |  j  | j |  j  Wd QRX| 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.N)editabler4   rP   r}   r   r   r   r   r   r   update_editabler   r1   r+   r,   r-   r   rH   check_if_existsr   )r=   r*   r.   r/   r/   r0   prepare_editable_requirement  s    
		z0RequirementPreparer.prepare_editable_requirementc          	   C   s   | j  s t d   | d k	 s9 t d j | j     t j d | | | j  j  t   ' |  j ru t j d  t	 |  } Wd QRX| 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_byr4   r   rP   r}   versionr   r   debugr   )r=   r*   skip_reasonr.   r/   r/   r0   prepare_installed_requirement  s    
	z1RequirementPreparer.prepare_installed_requirement)r?   r@   rA   __doc__r>   propertyr   r   r   r   r   r   r   r/   r/   )r   r0   r   =  s   -	!4r   )Kr   loggingrD   rW   rN   Zpip._vendor.sixr   Zpip._internal.distributionsr   Z%pip._internal.distributions.installedr   Zpip._internal.exceptionsr   r   r   r   r	   r
   r   Zpip._internal.utils.filesystemr   Zpip._internal.utils.hashesr   Zpip._internal.utils.loggingr   Zpip._internal.utils.miscr   r   r   r   Zpip._internal.utils.temp_dirr   Zpip._internal.utils.typingr   Zpip._internal.utils.unpackingr   Zpip._internal.vcsr   Ztypingr   r   r   r   Zmypy_extensionsr   r   Z"pip._internal.index.package_finderr   Zpip._internal.models.linkr   Zpip._internal.network.downloadr   Zpip._internal.req.req_installr    Zpip._internal.req.req_trackerr!   r"   rR   boolr#   	getLoggerr?   rP   r1   r9   objectr:   rM   rV   rd   rg   rn   rF   rC   r   r/   r/   r/   r0   <module>   sl   4	""	
""+