U
    P^IO                     @   s  d dl mZ d dlZd dl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m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 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%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l0m1Z1 d dl2m3Z3 e+rd dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: d dl;m<Z< d dl=m>Z> d dl?m@Z@ d dlAmBZB ere<de6eCe7eC ge7eC f eDdddZEn8e<de6eCeCgdf e6eCe7eC ge7eC f eDeDdddZEdddd d!d"d#gZFeGeHZId=d$dZJeKd%ejLZMd&d ZNd'd( ZOd)d* ZPd+d, ZQd-d. ZRd>d0d ZSd1d2 ZTd3d4 ZUd?d5dZVd@d6d!ZWd7d# ZXd8d" ZYd9d: ZZd;d< Z[dS )A    )absolute_importN)requests)CONTENT_CHUNK_SIZEResponse)PY2)parse)HashMismatchInstallationError)PyPI)
PipSession)auto_decode)copy2_fixed)	ask_path_exists
backup_dirconsumedisplay_pathformat_sizehide_urlpath_to_displayrmtreesplitext)TempDirectory)MYPY_CHECK_RUNNING)DownloadProgressProvider)unpack_file)get_url_scheme)vcs)IOCallableListOptionalTextTuple)	TypedDict)Link)Hashes)VersionControlCopytreeKwargsignoresymlinksF)total)copy_functionr)   ignore_dangling_symlinksr*   get_file_contentunpack_vcs_linkunpack_file_urlunpack_http_url
unpack_urlparse_content_dispositionsanitize_content_filenamec           
   
   C   s6  |dkrt dt| }|dkr>|| }|  |j|jfS |dkr|rd|drdtd|| f | ddd }|	d	d
}t
|}|r|dd |ddd  }t|}|d
rd
|d
 }|} z&t| d}t| }W 5 Q R X W n4 tk
r, }	 ztdt|	 W 5 d}	~	X Y nX | |fS )a*  Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.

    :param url:         File path or url.
    :param comes_from:  Origin description of requirements.
    :param session:     Instance of pip.download.PipSession.
    NzAget_file_content() missing 1 required keyword argument: 'session')httphttpsfiler5   z6Requirements file %s references URL %s, which is local:   \/|rbz$Could not open requirements file: %s)	TypeErrorr   getraise_for_statusurltext
startswithr	   splitreplace_url_slash_drive_rematchgroupurllib_parseunquotelstripopenr   readIOErrorstr)
rA   
comes_fromsessionschemeresppathrG   fcontentexc rX   7/tmp/pip-install-220asx0h/pip/pip/_internal/download.pyr.   U   sB    	




z/*([a-z])\|c                 C   s,   t | }|d k	st|j|t| jd d S )N)rA   )_get_used_vcs_backendAssertionErrorunpackr   rA   )linklocationvcs_backendrX   rX   rY   r/      s    c                 C   s$   t jD ]}| j|jkr|  S qdS )z1
    Return a VersionControl object or None.
    N)r   backendsrR   schemes)r]   r_   rX   rX   rY   rZ      s    

rZ   c                 O   s   | S NrX   )iterableargskwargsrX   rX   rY   _progress_indicator   s    rf   c              
      s6  zt jd }W n tttfk
r0   d}Y nX tdd}t tj	krRd}n&|r\d}n|dkrjd}n|std}nd}|j
}fdd}	 fd	d
}
t}|jtjkr|}n|j}|rt||d}|rtd|t| ntd| n|rtd| ntd| |
||	tt}|r*|| nt| d S )Nzcontent-lengthr   
from_cacheFi@  Tc                 3   sT   z  j j| ddD ]
}|V  qW n. tk
rN    j | }|sBqJ|V  q0Y nX d S )NF)decode_content)rawstreamAttributeErrorrM   )
chunk_sizechunk)rS   rX   rY   	resp_read   s    
z _download_url.<locals>.resp_readc                 3   s   | D ]}  | |V  qd S rb   )write)chunksrm   )content_filerX   rY   written_chunks   s    
z%_download_url.<locals>.written_chunks)maxzDownloading %s (%s)zDownloading %szUsing cached %s)intheaders
ValueErrorKeyErrorr>   getattrloggergetEffectiveLevelloggingINFOshow_urlrf   netlocr
   url_without_fragmentr   infor   r   check_against_chunksr   )rS   r]   rq   hashesprogress_bartotal_lengthcached_respshow_progressr}   rn   rr   progress_indicatorrA   downloaded_chunksrX   )rq   rS   rY   _download_url   sP    
%r   c                 C   s   d}t j||j}t j|rtdt| d}|dkr@d}nj|dkrdt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 %s exists. (i)gnore, (w)ipe, (b)ackup, (a)abort)iwbar   Fr   zDeleting %sr   zBacking up %s to %sr   zSaved %s)osrT   joinfilenameexistsr   r   ry   warningremover   shutilmovesysexitcopyr   )r   r^   r]   r   download_locationresponse	dest_filerX   rX   rY   
_copy_file  s6    
r   onc           
   	   C   s   |d krt dtddv}d }|r0t| ||}|rH|}t|d }	nt| ||j||\}}	t|||	 |r~|s~t|||  |st	
| W 5 Q R X d S )Nz@unpack_http_url() missing 1 required keyword argument: 'session'r\   )kindr   )r>   r   _check_download_dir	mimetypes
guess_type_download_http_urlrT   r   r   r   unlink)
r]   r^   download_dirrQ   r   r   temp_diralready_downloaded_path	from_pathcontent_typerX   rX   rY   r1     s2    	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   r   SpecialFileErrorry   r   rO   r   )srcdesterX   rX   rY   _copy2_ignoring_special_filesJ  s    r   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.noxrX   )dnamessourcerX   rY   r)   a  s    z!_copy_source_tree.<locals>.ignoreTr(   r,   )dictr   r   r   copytree)r   targetr)   re   rX   r   rY   _copy_source_tree_  s
    r   c                 C   s   | j }|  r>tj|r"t| t|| |r:td dS |rL|	| d}|r`t
| ||}|rj|}n|}t|d }t||| |r|st|||  dS )zUnpack link into location.

    If download_dir is provided and link points to a file, make a copy
    of the link file inside download_dir.
    z*Link is a directory, ignoring download_dirNr   )	file_pathZis_existing_dirr   rT   isdirr   r   ry   r   check_against_pathr   r   r   r   r   )r]   r^   r   r   	link_pathr   r   r   rX   rX   rY   r0   r  s.    


c                 C   sP   | j rt| | n:| jr*t| |||d n"|dkr8t }t| |||||d dS )av  Unpack link.
       If link is a VCS link:
         if only_download, export into download_dir and ignore location
          else unpack into location
       for other types of link:
         - unpack into location
         - if download_dir, copy the file into download_dir
         - if only_download, mark location for deletion

    :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.
    )r   N)r   r   )Zis_vcsr/   is_filer0   r   r1   )r]   r^   r   rQ   r   r   rX   rX   rY   r2     s    c                 C   s   t j| S )zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )r   rT   basename)r   rX   rX   rY   r4     s    c                 C   s,   t | \}}|d}|r$t|}|p*|S )z
    Parse the "filename" value from a Content-Disposition header, and
    return the default filename if the result is empty.
    r   )cgiparse_headerr?   r4   )content_dispositionZdefault_filename_typeparamsr   rX   rX   rY   r3     s
    
c              
   C   s*  | j ddd }z |j|ddidd}|  W n8 tjk
rj } ztd|jj	|   W 5 d	}~X Y nX |j
d
d}| j}	|j
d}
|
rt|
|	}	t|	d }|st|}|r|	|7 }	|s| j |j krtj|j d }|r|	|7 }	tj||	}t|d}t|| ||| W 5 Q R X ||fS )z6Download link url into temp_dir using provided session#r9   r   zAccept-EncodingidentityT)ru   rj   zHTTP error %s while getting %sNzcontent-type zcontent-dispositionwb)rA   rD   r?   r@   r   	HTTPErrorry   criticalr   status_coderu   r   r3   r   r   guess_extensionr   rT   r   rL   r   )r]   rQ   r   r   r   
target_urlrS   rW   r   r   r   extr   rq   rX   rX   rY   r     sB    	  

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.)r   rT   r   r   r   ry   r   r   r   r   r   )r]   r   r   download_pathrX   rX   rY   r   +  s    
r   )NN)NNNr   )NN)NNNr   )\
__future__r   r   r{   r   r   rer   r   pip._vendorr   Zpip._vendor.requests.modelsr   r   pip._vendor.sixr   pip._vendor.six.moves.urllibr   rI   pip._internal.exceptionsr   r	   pip._internal.models.indexr
   Zpip._internal.network.sessionr   pip._internal.utils.encodingr   pip._internal.utils.filesystemr   pip._internal.utils.miscr   r   r   r   r   r   r   r   r   pip._internal.utils.temp_dirr   pip._internal.utils.typingr   pip._internal.utils.uir   Zpip._internal.utils.unpackingr   Zpip._internal.utils.urlsr   pip._internal.vcsr   typingr   r   r   r    r!   r"   Zmypy_extensionsr#   pip._internal.models.linkr$   pip._internal.utils.hashesr%   Z pip._internal.vcs.versioncontrolr&   rO   boolr'   __all__	getLogger__name__ry   r.   compileIrF   r/   rZ   rf   r   r   r1   r   r   r0   r2   r4   r3   r   r   rX   rX   rX   rY   <module>   s   , 	  

0d    
-  
8    
.?