3
LY}                 @   sJ  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Zyd dlZdZW n ek
r   dZY nX d dlmZ d dlmZ d dlZd dlmZ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&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l2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z; d dl<m=Z=m>Z> d dl?m@Z@mAZA d dlBmCZCmDZD d dlEmFZF d dlGmHZH d dlImJZJ d dlKmLZL d dlMmNZN d dlOmPZP d dlQmRZR dddd d!d"d#d$d%d&d'gZSejTeUZVd(d) ZWG d*d+ d+e@ZXG d,d- d-e=ZYG d.d/ d/eNZZG d0d1 d1e>Z[G d2d3 d3e:j\Z]dSd4dZ^ej_d5ej`Zaej_d6ej`Zbd7d Zcd8d Zdd9d  Zed:d! Zfd;d" Zgd<d= Zhd>d$ Zid?d% Zjd@dA ZkdBdC ZldDdE ZmdFdG ZndTdHd&ZodUdId#ZpdJdK ZqG dLdM dMeRjrZsdVdNd'ZtdOdP ZudQdR ZvdS )W    )absolute_importNTF)parse)request)InstallationErrorHashMismatch)PyPI)
splitextrmtreeformat_sizedisplay_path
backup_dirask_path_existsunpack_fileARCHIVE_EXTENSIONSconsumecall_subprocess)auto_decode)check_path_owner)
indent_log)SETUPTOOLS_SHIM)libc_ver)DownloadProgressBarDownloadProgressSpinner)write_delete_marker_file)vcs)requestssix)BaseAdapterHTTPAdapter)AuthBaseHTTPBasicAuth)CONTENT_CHUNK_SIZEResponse)get_netrc_auth)CaseInsensitiveDict)urllib3)CacheControlAdapter)	FileCache)	LockError)xmlrpc_client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j dtj id} | d d dkrBtj | d d< n| d d dkrtjjd	krntjd
d }ntj}djdd |D | d d< nB| d d dkrtj | d d< n | d d dkrtj | d d< tjj	drJddl
m} ttdd tdddg|j }ttdd tddgt }|r<||d< |rJ|| d< tjj	dr|tj d r|dtj d d| d< tj rtj | jdi d< tj rtj | jdi d< tj rtj | d< trtjd
d  d)krtj| d"< d#j| tj| d*d&d'd(S )+z6
    Return a string representing the user agent.
    pip)nameversionr6   )	installerpythonimplementationr:   CPythonr7   PyPyfinalN   .c             S   s   g | ]}t |qS  )str).0xr@   r@   _/private/var/folders/7d/20zwc49s3kn54d3vgq8bd4640000gn/T/pip-build-an2lx5zf/pip/pip/download.py
<listcomp>S   s    zuser_agent.<locals>.<listcomp>Jython
IronPythonlinuxr   )distroc             S   s   | d S )N   r@   )rC   r@   r@   rD   <lambda>_   s    zuser_agent.<locals>.<lambda>idc             S   s   | d S )NrJ   r@   )rC   r@   r@   rD   rK   c   s    liblibcrI   darwinmacOSsystemreleasecpu      openssl_versionz9{data[installer][name]}/{data[installer][version]} {json},:T)
separators	sort_keys)datajson)rT   rU   )rW   rX   )r5   __version__platformpython_versionpython_implementationsyspypy_version_inforeleaseleveljoin
startswithpip._vendorrI   dictfilterziplinux_distributionr   mac_verrQ   
setdefaultrR   machineHAS_TLSversion_infosslOPENSSL_VERSIONformatr\   dumps)r[   rb   rI   distro_infosrN   r@   r@   rD   
user_agent?   sP    




ru   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)selfrw   r@   r@   rD   __init__   s    zMultiDomainBasicAuth.__init__c             C   s   t j|j}|jjddd }t j|d d |f |dd   |_| jj|d\}}|d krn| j|j\}}|d kr|d krt	|j}|r|nd\}}|s|r||f| j|< t
|pd|pd|}|jd| j |S )	N@rJ   rT    response)NN)NN)urllib_parseurlparseurlnetlocrsplit
urlunparserx   getparse_credentialsr#   r    register_hook
handle_401)ry   reqparsedr   usernamepassword
netrc_authr@   r@   rD   __call__   s    &
zMultiDomainBasicAuth.__call__c             K   s   |j dkr|S | js|S tj|j}tjjd|j }t	j	d}|sH|rX||f| j
|j< |j |jj  t|ppd|pvd|j}|jj|f|}|jj| |S )Ni  zUser for %s: z
Password: r|   )status_coderw   r   r   r   r   movesinputr   getpassrx   contentrawrelease_connr    r   
connectionsendhistoryappend)ry   respkwargsr   r   r   r   new_respr@   r@   rD   r      s    


zMultiDomainBasicAuth.handle_401c             C   s8   d|kr4|j ddd }d|kr,|jddS |d fS dS )Nr{   rJ   r   rX   )NN)r   split)ry   r   userinfor@   r@   rD   r      s    z&MultiDomainBasicAuth.parse_credentialsN)T)__name__
__module____qualname__rz   r   r   r   r@   r@   r@   rD   rv      s   
!"rv   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j|}	W n. tk
rZ }
 zd|_|
|_W Y d d }
~
X nPX tj	j
|	jdd}tj|d p~d}t||	j|d|_t|d|_|jj|_|S )	N   i  T)usegmtr   z
text/plain)zContent-TypezContent-LengthzLast-Modifiedrb)r,   r   r"   r   osstatOSErrorr   emailutils
formatdatest_mtime	mimetypes
guess_typer$   st_sizeheadersopenclose)ry   r   streamtimeoutverifycertproxiespathnamer   statsexcmodifiedcontent_typer@   r@   rD   r      s$    

zLocalFSAdapter.sendc             C   s   d S )Nr@   )ry   r@   r@   rD   r      s    zLocalFSAdapter.close)NNNNN)r   r   r   r   r   r@   r@   r@   rD   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j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   rz   r   	directoryloggerwarning)ry   argsr   )	__class__r@   rD   rz      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)ry   r   r   )r   r@   rD   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   )ry   r   r   )r   r@   rD   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   )ry   r   r   )r   r@   rD   r   (  s    
zSafeFileCache.delete)	r   r   r   __doc__rz   r   r   r   __classcell__r@   r@   )r   rD   r      s
   r   c               @   s   e Zd Zdd ZdS )InsecureHTTPAdapterc             C   s   d|_ d |_d S )N	CERT_NONE)	cert_reqsca_certs)ry   connr   r   r   r@   r@   rD   cert_verify8  s    zInsecureHTTPAdapter.cert_verifyN)r   r   r   r   r@   r@   r@   rD   r   6  s   r   c                   s,   e Zd ZdZ fddZ fddZ  ZS )
PipSessionNc       	         s   |j dd}|j dd }|j dg }tt| j|| t | jd< t | _tj	|dgdd}|rvt
t|d	d
|d}n
t|d}t|d}| jd| | jd| | jdt  x|D ]}| jdj|| qW d S )Nretriesr   cacheinsecure_hostsz
User-Agenti  g      ?)totalstatus_forcelistbackoff_factorT)use_dir_lock)r   max_retries)r   zhttps://zhttp://zfile://zhttps://{0}/)popr   r   rz   ru   r   rv   authr%   Retryr&   r   r   r   mountr   rr   )	ry   r   r   r   r   r   secure_adapterinsecure_adapterhost)r   r@   rD   rz   A  s*    




zPipSession.__init__c                s(   |j d| j tt| j||f||S )Nr   )rl   r   r   r   r   )ry   methodr   r   r   )r   r@   rD   r   }  s    zPipSession.request)r   r   r   r   rz   r   r   r@   r@   )r   rD   r   =  s   <r   c       
      C   sL  |dkrt dtj| }|r|jdj }|dkrR|rR|jdrRtd|| f |dkr| jddd }|jdd	}t	j
|}|r|jdd |jd
dd  }tj|}|jd	rd	|jd	 }|} n|j| }|j  |j|jfS y&t| d}t|j }W dQ R X W n4 tk
rB }	 ztdt|	 W Y dd}	~	X nX | |fS )zGets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.NzAget_file_content() missing 1 required keyword argument: 'session'rJ   filehttpz6Requirements file %s references URL %s, which is localrX   \/|r   z$Could not open requirements file: %s)	TypeError
_scheme_researchgrouplowerre   r   r   replace_url_slash_drive_rematchr   unquotelstripr   raise_for_statusr   textr   r   readr   rA   )
r   
comes_fromsessionr   schemepathr   fr   r   r@   r@   rD   r*     s>    





 z^(http|https|file):z/*([a-z])\|c             C   s6   d| krdS | j ddd j }|ddddgtj kS )	z)Returns true if the name looks like a URLrX   FrJ   r   r   httpsr   ftp)r   r   r   all_schemes)r6   r   r@   r@   rD   r+     s    c             C   sH   | j dstd|  tj| \}}}}}|r6d| }tj|| }|S )z(
    Convert a file: URL to a path.
    zfile:z4You can only turn file: urls into filenames (not %r)z\\)re   AssertionErrorr   urlspliturllib_requesturl2pathname)r   _r   r   r@   r@   rD   r,     s    
c             C   s*   t jjt jj| } tjdtj| }|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abspathr   urljoinr   pathname2url)r   r   r@   r@   rD   r-     s    c             C   s    t | d j }|tkrdS dS )z9Return True if `name` is a considered as an archive file.rJ   TF)r   r   r   )r6   extr@   r@   rD   r.     s    c             C   s   t | }|j| d S )N)_get_used_vcs_backendunpack)linklocationvcs_backendr@   r@   rD   r/     s    c             C   s.   x(t jD ]}| j|jkr|| j}|S qW d S )N)r   backendsr   schemesr   )r  backendr  r@   r@   rD   r    s    
r  c             C   s   t t| S )N)boolr  )r  r@   r@   rD   r1     s    c             C   s   | j j jdS )Nzfile:)r   r   re   )r  r@   r@   rD   r2     s    c             C   s   t | j}tj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.

    )r,   url_without_fragmentr   r   isdir)r  	link_pathr@   r@   rD   
is_dir_url  s    
r  c             O   s   | S )Nr@   )iterabler   r   r@   r@   rD   _progress_indicator  s    r  c                sL  yt jd }W n tttfk
r0   d}Y nX tdd}tj 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}|r|rtjd|t| t|dj}
ntjd| t j}
n |rtjd| ntjd| tjd| |	|
|tt}|r@|j| nt| d S )Nzcontent-lengthr   
from_cacheF(   i  Tc             3   s\   y$x j j| ddD ]
}|V  qW W n2 tk
rV   x j j| }|sHP |V  q6W Y nX d S )NF)decode_content)r   r   AttributeErrorr   )
chunk_sizechunk)r   r@   rD   	resp_read  s    z _download_url.<locals>.resp_readc             3   s"   x| D ]} j | |V  qW d S )N)write)chunksr  )content_filer@   rD   written_chunks:  s    

z%_download_url.<locals>.written_chunkszDownloading %s (%s))maxzDownloading %szUsing cached %szDownloading from URL %si@  )intr   
ValueErrorKeyErrorr   getattrr   getEffectiveLevelloggingINFOshow_urlr  r   r   r  infor
   r   iterr   debugr!   check_against_chunksr   )r   r  r  hashestotal_lengthcached_respshow_progressr&  r  r  progress_indicatorr   downloaded_chunksr@   )r  r   rD   _download_url   sL    
%
r1  c             C   s   d}t jj||j}t jj|rtdt| d}|dkr@d}nj|dkrdtjdt| t j	| nF|dkrt
|}tjd	t|t| tj|| n|dkrtjd |rtj| | tjdt| d S )NTz8The file %s exists. (i)gnore, (w)ipe, (b)ackup, (a)abortiwbaFzDeleting %szBacking up %s to %srJ   zSaved %s)r2  r3  r4  r5  r~   )r   r   rd   filenameexistsr   r   r   r   remover   shutilmovera   exitcopyr'  )r6  r  r  r<  download_locationr}   	dest_filer@   r@   rD   
_copy_file`  s.    

r?  c       	      C   s   |d krt dtjdd}d }|r0t| ||}|rH|}tj|d }nt| |||\}}t||||  |r~| r~t|||  |st	j
| t| d S )Nz@unpack_http_url() missing 1 required keyword argument: 'session'z-unpackzpip-r   )r   tempfilemkdtemp_check_download_dirr   r   _download_http_urlr   r?  r   unlinkr	   )	r  r  download_dirr   r+  temp_diralready_downloaded_path	from_pathr   r@   r@   rD   r3   {  s,    


c             C   s   t | j}t| rHtjj|r&t| tj||dd |rDt	j
d dS |rV|j| d}|rjt| ||}|rt|}n|}tj|d }t||||  |r| rt|||  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)symlinksz*Link is a directory, ignoring download_dirNr   )r,   r  r  r   r   r  r	   r9  copytreer   r'  check_against_pathrB  r   r   r   r?  )r  r  rE  r+  r  rG  rH  r   r@   r@   rD   r0     s,    



c             C   s   t jj|rt| d}tjg}|jd |jt|  |jd |d|g7 }tj	d|  t
  t|| dd W dQ R X t jj|t j|d	 }tj	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)cwdshow_stdoutNr   zUnpacking sdist %s into %s)r   r  )r   r   r  r	   ra   
executabler   r   r   r'  r   r   rd   listdirr   )r  r  setup_py
sdist_argsrL  r@   r@   rD   _copy_dist_from_dir  s    

rS  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j| | tj|}|j| _|| _d S )N)r)   	Transportrz   r   r   r   _scheme_session)ry   	index_urlr   use_datetimeindex_partsr@   r@   rD   rz     s    
zPipXmlrpcTransport.__init__c       
      C   s   | j ||d d d f}tj|}y6ddi}| jj|||dd}|j  || _| j|jS  t	j
k
r }	 ztjd|	jj|  W Y d d }	~	X nX d S )NzContent-Typeztext/xmlT)r[   r   r   zHTTP error %s while getting %s)rV  r   r   rW  postr   verboseparse_responser   r   	HTTPErrorr   criticalr}   r   )
ry   r   handlerrequest_bodyr\  partsr   r   r}   r   r@   r@   rD   r     s    


zPipXmlrpcTransport.requestN)F)F)r   r   r   r   rz   r   r@   r@   r@   rD   rT    s   
rT  c             C   s^   t | rt| | n:t| r.t| |||d n |dkr<t }t| ||||d |rZt| 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)r1   r/   r2   r0   r   r3   r   )r  r  rE  only_downloadr   r+  r@   r@   rD   r4     s    c             C   s@  | j jddd }y |j|ddidd}|j  W n8 tjk
rj } ztjd|jj	|   W Y d	d	}~X nX |j
jd
d}| j}|j
jd}	|	rtj|	\}
}|jdp|}t|d }|stj|}|r||7 }| o| j |j krtjj|j d }|r||7 }tjj||}t|d}t|| || W d	Q R X ||fS )z6Download link url into temp_dir using provided session#rJ   r   zAccept-EncodingidentityT)r   r   zHTTP error %s while getting %sNzcontent-typer|   zcontent-dispositionr6  wb)r   r   r   r   r   r^  r   r_  r}   r   r   r6  cgiparse_headerr   r   guess_extensionr   r   rd   r   r1  )r  r   rF  r+  
target_urlr   r   r   r6  content_dispositiontypeparamsr  	file_pathr  r@   r@   rD   rC  ;  s<    
rC  c             C   sn   t jj|| j}t jj|rjtjd| |rfy|j| W n* tk
rd   tj	d| t j
| 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   rd   r6  r7  r   r'  rK  r   r   rD  )r  rE  r+  download_pathr@   r@   rD   rB  v  s    
rB  )NN)NNN)NN)NFNN)w
__future__r   rg  email.utilsr   r   r\   r$  r   r   r^   rer9  ra   r@  rp   rn   ImportErrorZpip._vendor.six.moves.urllibr   r   r   r   r5   pip.exceptionsr   r   
pip.modelsr   	pip.utilsr   r	   r
   r   r   r   r   r   r   r   pip.utils.encodingr   pip.utils.filesystemr   pip.utils.loggingr   pip.utils.setuptools_buildr   pip.utils.glibcr   pip.utils.uir   r   pip.locationsr   pip.vcsr   rf   r   r   Zpip._vendor.requests.adaptersr   r   Zpip._vendor.requests.authr   r    Zpip._vendor.requests.modelsr!   r"   Zpip._vendor.requests.utilsr#   Zpip._vendor.requests.structuresr$   Zpip._vendor.requests.packagesr%   pip._vendor.cachecontrolr&   pip._vendor.cachecontrol.cachesr'   pip._vendor.lockfiler(   pip._vendor.six.movesr)   __all__	getLoggerr   r   ru   rv   r   r   r   Sessionr   r*   compileIr   r   r+   r,   r-   r.   r/   r  r1   r2   r  r  r1  r?  r3   r0   rS  rU  rT  r4   rC  rB  r@   r@   r@   rD   <module>   s   
0
BR!BH
)
` 
&
0$ 
';