B
    Wc\                 @   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
Z
d dlZd dlZd dlZd dl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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+ d dl,m-Z- d dl.Z.d dl/m0Z0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJ d dlKmLZL d dlMmNZN d dlOmPZP d dlQmRZR d dlSmTZT ePr2d dlUmVZVmWZWmXZXmYZYmZZZm[Z[ d dl\m]Z] d dl^m_Z_ d d lSm`Z` yd dlaZaW n ebk
rX   dZaY nX eadk	pfe-Zcd!d"d#d$d%d&d'd(d)d*d+gZdeeefZgd,d- ZhG d.d/ d/eZiG d0d1 d1eZjG d2d3 d3eZkG d4d5 d5eZlG d6d7 d7ejmZndYd8d!Zoepd9ejqZrepd:ejqZsd;d" Ztd<d# Zud=d$ Zvd>d% Zwd?d& Zxd@dA ZydBd( ZzdCd) Z{dDdE Z|dFdG Z}dHdI Z~dJdK ZdZdMd*Zd[dNd'ZdOdP ZG dQdR dRe&jZd\dTd+ZdUdV ZdWdX ZdS )]    )absolute_importN)requestssixurllib3)CacheControlAdapter)	FileCache)	LockError)BaseAdapterHTTPAdapter)AuthBaseHTTPBasicAuth)CONTENT_CHUNK_SIZEResponse)CaseInsensitiveDict)get_netrc_auth)xmlrpc_client)parse)request)IS_PYOPENSSL)HashMismatchInstallationError)write_delete_marker_file)PyPI)auto_decode)check_path_owner)libc_ver)
indent_log)ARCHIVE_EXTENSIONSask_path_exists
backup_dircall_subprocessconsumedisplay_pathformat_sizeget_installed_versionrmtreesplit_auth_from_netlocsplitextunpack_file)SETUPTOOLS_SHIM)TempDirectory)MYPY_CHECK_RUNNING)DownloadProgressProvider)vcs)OptionalTupleDictIOTextUnion)Link)Hashes)AuthInfoget_file_contentis_urlurl_to_pathpath_to_urlis_archive_fileunpack_vcs_linkunpack_file_url
is_vcs_urlis_file_urlunpack_http_url
unpack_urlc              C   s  dt jdt dt id} | d d dkrBt | d d< n| d d dkrtjjd	krntjd
d }ntj}ddd |D | d d< nB| d d dkrt | d d< n | d d dkrt | d d< tj	drJddl
m} ttdd tdddg| }ttdd tddgt }|r<||d< |rJ|| d< tj	dr|t d r|dt d d| d< t rt | di d< t rt | di d< t rt | d< trtj| d < td!}|d
k	r|| d"< d#j| tj| d$d%d&d'S )(z6
    Return a string representing the user agent.
    pip)nameversionrC   )Z	installerZpythonimplementationrE   ZCPythonrD   ZPyPyfinalN   .c             S   s   g | ]}t |qS  )str).0xrI   rI   5lib/python3.7/site-packages/pip/_internal/download.py
<listcomp>_   s    zuser_agent.<locals>.<listcomp>ZJythonZ
IronPythonZlinuxr   )distroc             S   s   | d S )N   rI   )rL   rI   rI   rM   <lambda>k   s    zuser_agent.<locals>.<lambda>idc             S   s   | d S )NrP   rI   )rL   rI   rI   rM   rQ   o   s    liblibcrO   darwinZmacOSsystemreleaseZcpuZopenssl_versionZ
setuptoolssetuptools_versionz9{data[installer][name]}/{data[installer][version]} {json}),:T)Z
separatorsZ	sort_keys)datajson)rB   __version__platformZpython_versionZpython_implementationsyspypy_version_inforeleaseleveljoin
startswithpip._vendorrO   dictfilterzipZlinux_distributionr   Zmac_verrV   
setdefaultrW   machineHAS_TLSsslZOPENSSL_VERSIONr$   formatr\   dumps)r[   r`   rO   Zdistro_infosrT   rX   rI   rI   rM   
user_agentK   sV    





rn   c               @   s.   e Zd ZdddZdd Zdd Zdd	 Zd
S )MultiDomainBasicAuthTc             C   s   || _ i | _d S )N)	prompting	passwords)selfrp   rI   rI   rM   __init__   s    zMultiDomainBasicAuth.__init__c             C   s   t |j}t|j\}}t |d d |f |dd   |_| j|d\}}|d krb|\}}|d kr|d krt|j}|r|nd\}}|s|r||f| j|< t	|pd|pd|}|
d| j |S )NrP      )NN response)urllib_parseurlparseurlr&   netloc
urlunparserq   getr   r   register_hook
handle_401)rr   reqparsedrz   Zurl_user_passwordusernamepasswordZ
netrc_authrI   rI   rM   __call__   s    &
zMultiDomainBasicAuth.__call__c             K   s   |j dkr|S | js|S t|j}tjd|j }t		d}|sH|rX||f| j
|j< |j |j  t|ppd|pvd|j}|d| j |jj|f|}|j| |S )Ni  zUser for %s: z
Password: ru   rv   )status_coderp   rw   rx   ry   r   Zmovesinputrz   getpassrq   contentrawZrelease_connr   r   r}   warn_on_401Z
connectionsendhistoryappend)rr   respkwargsr   r   r   r   Znew_resprI   rI   rM   r~      s     


zMultiDomainBasicAuth.handle_401c             K   s   |j dkrtd|jj d S )Ni  z)401 Error, Credentials not correct for %s)r   loggerwarningr   ry   )rr   r   r   rI   rI   rM   r      s    
z MultiDomainBasicAuth.warn_on_401N)T)__name__
__module____qualname__rs   r   r~   r   rI   rI   rI   rM   ro      s   
!#ro   c               @   s   e Zd ZdddZdd ZdS )LocalFSAdapterNc          
   C   s   t |j}t }d|_|j|_yt|}	W n. tk
rZ }
 zd|_|
|_W d d }
~
X Y nPX tj	j
|	jdd}t|d p~d}t||	j|d|_t|d|_|jj|_|S )	N   i  T)Zusegmtr   z
text/plain)zContent-TypezContent-LengthzLast-Modifiedrb)r9   ry   r   r   osstatOSErrorr   emailZutilsZ
formatdatest_mtime	mimetypes
guess_typer   st_sizeheadersopenclose)rr   r   streamtimeoutverifycertZproxiespathnamer   ZstatsexcZmodifiedcontent_typerI   rI   rM   r      s$    

zLocalFSAdapter.sendc             C   s   d S )NrI   )rr   rI   rI   rM   r      s    zLocalFSAdapter.close)NNNNN)r   r   r   r   r   rI   rI   rI   rM   r      s    
r   c                   sD   e Zd ZdZ fddZ fddZ fddZ fdd	Z  ZS )
SafeFileCachezw
    A file based cache which is safe to use even when the target directory may
    not be accessible or writable.
    c                s4   t t| j|| t| js0td| j d | _d S )NzThe directory '%s' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.)superr   rs   r   	directoryr   r   )rr   argsr   )	__class__rI   rM   rs   	  s    
zSafeFileCache.__init__c          
      s@   | j d krd S ytt| j||S  tttfk
r:   Y nX d S )N)r   r   r   r|   r   r   IOError)rr   r   r   )r   rI   rM   r|     s    
zSafeFileCache.getc          
      s@   | j d krd S ytt| j||S  tttfk
r:   Y nX d S )N)r   r   r   setr   r   r   )rr   r   r   )r   rI   rM   r   *  s    
zSafeFileCache.setc          
      s@   | j d krd S ytt| j||S  tttfk
r:   Y nX d S )N)r   r   r   deleter   r   r   )rr   r   r   )r   rI   rM   r   7  s    
zSafeFileCache.delete)	r   r   r   __doc__rs   r|   r   r   __classcell__rI   rI   )r   rM   r     s
   r   c               @   s   e Zd Zdd ZdS )InsecureHTTPAdapterc             C   s   d|_ d |_d S )NZ	CERT_NONE)Z	cert_reqsZca_certs)rr   Zconnry   r   r   rI   rI   rM   cert_verifyG  s    zInsecureHTTPAdapter.cert_verifyN)r   r   r   r   rI   rI   rI   rM   r   E  s   r   c                   s,   e Zd ZdZ fddZ fddZ  ZS )
PipSessionNc       	         s   | dd}| dd }| dg }tt| j|| t | jd< t | _tj	|dddd	gd
d}|r|t
t|dd|d}n
t|d}t|d}| d| | d| | dt  x|D ]}| d|| qW d S )Nretriesr   cacheinsecure_hostsz
User-Agenti  i  i  i  g      ?)ZtotalZstatus_forcelistZbackoff_factorT)Zuse_dir_lock)r   max_retries)r   zhttps://zhttp://zfile://zhttps://{}/)popr   r   rs   rn   r   ro   Zauthr   ZRetryr   r   r
   r   Zmountr   rl   )	rr   r   r   r   r   r   Zsecure_adapterZinsecure_adapterhost)r   rI   rM   rs   P  s*    





zPipSession.__init__c                s(   | d| j tt| j||f||S )Nr   )rh   r   r   r   r   )rr   methodry   r   r   )r   rI   rM   r     s    zPipSession.request)r   r   r   r   rs   r   r   rI   rI   )r   rM   r   L  s   >r   c       
   
   C   sL  |dkrt dt| }|r|d }|dkrR|rR|drRtd|| f |dkr| ddd }|dd	}t	
|}|r|dd |d
dd  }t|}|d	rd	|d	 }|} n|| }|  |j|jfS y&t| d}t| }W dQ R X W n4 tk
rB }	 ztdt|	 W d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'rP   filehttpz6Requirements file %s references URL %s, which is localrZ   \/|r   z$Could not open requirements file: %s)	TypeError
_scheme_researchgrouplowerrc   r   splitreplace_url_slash_drive_rematchrw   Zunquotelstripr|   raise_for_statusry   textr   r   readr   rJ   )
ry   Z
comes_fromsessionr   schemepathr   fr   r   rI   rI   rM   r7     s>    	





 z^(http|https|file):z/*([a-z])\|c             C   s6   d| krdS |  ddd  }|ddddgtj kS )	z)Returns true if the name looks like a URLrZ   FrP   r   r   Zhttpsr   Zftp)r   r   r-   Zall_schemes)rC   r   rI   rI   rM   r8     s    c             C   sH   |  dstd|  t| \}}}}}|r6d| }t|| }|S )z(
    Convert a file: URL to a path.
    zfile:z4You can only turn file: urls into filenames (not %r)z\\)rc   AssertionErrorrw   Zurlspliturllib_requestZurl2pathname)ry   _rz   r   rI   rI   rM   r9     s    
c             C   s*   t jt j| } tdt| }|S )zh
    Convert a path to a file: URL.  The path will be made absolute and have
    quoted path parts.
    zfile:)r   r   normpathabspathrw   Zurljoinr   Zpathname2url)r   ry   rI   rI   rM   r:     s    c             C   s    t | d  }|tkrdS dS )z9Return True if `name` is a considered as an archive file.rP   TF)r'   r   r   )rC   extrI   rI   rM   r;     s    c             C   s   t | }|| d S )N)_get_used_vcs_backendunpack)linklocationvcs_backendrI   rI   rM   r<     s    c             C   s.   x(t jD ]}| j|jkr|| j}|S qW d S )N)r-   Zbackendsr   Zschemesry   )r   Zbackendr   rI   rI   rM   r     s    
r   c             C   s   t t| S )N)boolr   )r   rI   rI   rM   r>     s    c             C   s   | j  dS )Nzfile:)ry   r   rc   )r   rI   rI   rM   r?   	  s    c             C   s   t | j}tj|S )zReturn whether a file:// Link points to a directory.

    ``link`` must not have any other scheme but file://. Call is_file_url()
    first.

    )r9   url_without_fragmentr   r   isdir)r   	link_pathrI   rI   rM   
is_dir_url  s    
r   c             O   s   | S )NrI   )iterabler   r   rI   rI   rM   _progress_indicator  s    r   c          
      sB  y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d| |
||	tt}|r6|| nt| d S )Nzcontent-lengthr   Z
from_cacheFi@  Tc             3   s\   y$x j j| ddD ]
}|V  qW W n2 tk
rV   x j | }|sHP |V  q6W Y nX d S )NF)Zdecode_content)r   r   AttributeErrorr   )Z
chunk_sizechunk)r   rI   rM   	resp_read9  s    z _download_url.<locals>.resp_readc             3   s"   x| D ]}  | |V  qW d S )N)write)Zchunksr   )content_filerI   rM   written_chunks^  s    

z%_download_url.<locals>.written_chunks)maxzDownloading %s (%s)zDownloading %szUsing cached %szDownloading from URL %s)intr   
ValueErrorKeyErrorr   getattrr   ZgetEffectiveLevelloggingINFOshow_urlr   rz   r   r   r,   infor#   debugr   Zcheck_against_chunksr!   )r   r   r   hashesprogress_barZtotal_lengthZcached_respZshow_progressr   r   r   Zprogress_indicatorry   Zdownloaded_chunksrI   )r   r   rM   _download_url  sL    
%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)r   r   rb   filenameexistsr   r"   r   r   remover   shutilZmover_   exitcopyr   )r   r   r   r  Zdownload_locationrv   Z	dest_filerI   rI   rM   
_copy_file  s.    

r  onc       
   	   C   s   |d krt dtddx}d }|r0t| ||}|rH|}t|d }	nt| ||j||\}}	t|||	|  |r|st|||  |st	
| W d Q R X d S )Nz@unpack_http_url() missing 1 required keyword argument: 'session'r   )Zkindr   )r   r*   _check_download_dirr   r   _download_http_urlr   r(   r  r   unlink)
r   r   download_dirr   r   r   temp_diralready_downloaded_path	from_pathr   rI   rI   rM   r@     s,    	
c             C   s   t | j}t| rHtj|r&t| tj||dd |rDt	
d dS |rV|| d}|rjt| ||}|rt|}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.
    T)Zsymlinksz*Link is a directory, ignoring download_dirNr   )r9   r   r   r   r   r   r%   r   Zcopytreer   r   check_against_pathr  r   r   r(   r  )r   r   r  r   r   r
  r  r   rI   rI   rM   r=     s,    


c          	   C   s   t j|rt| d}tjg}|d |t|  |d |d|g7 }t	d|  t
  t|| dd W dQ R X t j|t |d	 }t	d
|| t||ddd dS )zCopy distribution files in `link_path` to `location`.

    Invoked when user requests to install a local directory. E.g.:

        pip install .
        pip install ~/dev/git-repos/python-prompt-toolkit

    zsetup.pyz-csdistz
--dist-dirzRunning setup.py sdist for %sF)cwdZshow_stdoutNr   zUnpacking sdist %s into %s)r   r   )r   r   r   r%   r_   
executabler   r)   r   r   r   r    rb   listdirr(   )r   r   Zsetup_pyZ
sdist_argsr  rI   rI   rM   _copy_dist_from_dir  s    

r  c               @   s$   e Zd ZdZdddZd	ddZdS )
PipXmlrpcTransportzRProvide a `xmlrpclib.Transport` implementation via a `PipSession`
    object.
    Fc             C   s*   t j| | t|}|j| _|| _d S )N)r   	Transportrs   rw   rx   r   _scheme_session)rr   Z	index_urlr   Zuse_datetimeZindex_partsrI   rI   rM   rs   +  s    
zPipXmlrpcTransport.__init__c       
   
   C   s   | j ||d d d f}t|}y6ddi}| jj|||dd}|  || _| |jS  t	j
k
r }	 ztd|	jj|  W d d }	~	X Y nX d S )NzContent-Typeztext/xmlT)r[   r   r   zHTTP error %s while getting %s)r  rw   r{   r  Zpostr   verboseZparse_responser   r   	HTTPErrorr   criticalrv   r   )
rr   r   ZhandlerZrequest_bodyr  partsry   r   rv   r   rI   rI   rM   r   1  s    


zPipXmlrpcTransport.requestN)F)F)r   r   r   r   rs   r   rI   rI   rI   rM   r  &  s   
r  Fc             C   s`   t | rt| | n<t| r.t| |||d n"|dkr<t }t| |||||d |r\t| 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   )r>   r<   r?   r=   r   r@   r   )r   r   r  Zonly_downloadr   r   r   rI   rI   rM   rA   C  s    c          
   C   sB  | j ddd }y |j|ddidd}|  W n8 tjk
rj } ztd|jj	|   W d	d	}~X Y nX |j
d
d}| j}	|j
d}
|
rt|
\}}|dp|	}	t|	d }|st|}|r|	|7 }	|s| j |j krtj|j d }|r|	|7 }	tj||	}t|d}t|| ||| W d	Q R X ||fS )z6Download link url into temp_dir using provided session#rP   r   zAccept-EncodingZidentityT)r   r   zHTTP error %s while getting %sNzcontent-typeru   zcontent-dispositionr   wb)ry   r   r|   r   r   r  r   r  rv   r   r   r   cgiZparse_headerr'   r   Zguess_extensionr   r   rb   r   r   )r   r   r	  r   r   Z
target_urlr   r   r   r   Zcontent_dispositiontypeZparamsr   Z	file_pathr   rI   rI   rM   r  t  s<    	
r  c             C   sn   t j|| j}t j|rjtd| |rfy|| W n* tk
rd   t	d| t 
| dS X |S dS )z Check download_dir for previously downloaded file with correct hash
        If a correct file is found return its path else None
    zFile was already downloaded %sz;Previously-downloaded file %s has bad hash. Re-downloading.N)r   r   rb   r   r   r   r   r  r   r   r  )r   r  r   Zdownload_pathrI   rI   rM   r    s    
r  )NN)NNNr  )NN)NFNNr  )Z
__future__r   r  Zemail.utilsr   r   r\   r   r   r   r^   rer   r_   rd   r   r   r   Zpip._vendor.cachecontrolr   Zpip._vendor.cachecontrol.cachesr   Zpip._vendor.lockfiler   Zpip._vendor.requests.adaptersr	   r
   Zpip._vendor.requests.authr   r   Zpip._vendor.requests.modelsr   r   Zpip._vendor.requests.structuresr   Zpip._vendor.requests.utilsr   Zpip._vendor.six.movesr   Zpip._vendor.six.moves.urllibr   rw   r   r   Zpip._vendor.urllib3.utilr   rB   Zpip._internal.exceptionsr   r   Zpip._internal.locationsr   Zpip._internal.models.indexr   Zpip._internal.utils.encodingr   Zpip._internal.utils.filesystemr   Zpip._internal.utils.glibcr   Zpip._internal.utils.loggingr   Zpip._internal.utils.miscr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   Z$pip._internal.utils.setuptools_buildr)   Zpip._internal.utils.temp_dirr*   Zpip._internal.utils.typingr+   Zpip._internal.utils.uir,   Zpip._internal.vcsr-   typingr.   r/   r0   r1   r2   r3   Zpip._internal.models.linkr4   Zpip._internal.utils.hashesr5   r6   rk   ImportErrorrj   __all__Z	getLoggerr   r   rn   ro   r   r   r   ZSessionr   r7   compileIr   r   r8   r9   r:   r;   r<   r   r>   r?   r   r   r   r  r@   r=   r  r  r  rA   r  r  rI   rI   rI   rM   <module>   s   8 

ER!BJ
/		f   
* 
2$     
*B