3 �PfO��@s^ddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZyddlZdZWnek r�dZYnXddlmZddlmZddlZddlmZmZddlmZdd lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)dd l*m+Z+dd l,m-Z-dd l.m/Z/ddl0m1Z1ddl2m3Z3m4Z4ddl5m6Z6ddl7m8Z8ddl9m:Z:m;Z;ddlZ>ddl?m@Z@mAZAddlBmCZCmDZDddlEmFZFddlGmHZHddl9mIZIddlJmKZKddlLmMZMddlNmOZOddlPmQZQdddd d!d"d#d$d%d&d'd(d)g ZRejSeT�ZUd*d+�ZVGd,d-�d-e@�ZWGd.d/�d/e=�ZXGd0d1�d1eM�ZYGd2d3�d3e>�ZZGd4d5�d5e:j[�Z\dWd6d�Z]e j^d7e j_�Z`e j^d8e j_�Zad9d�Zbd:d�Zcd;d �Zddd?�Zgd@d$�ZhdAd%�ZidBdC�ZjdDdE�ZkdFdG�ZldHdI�ZmdXdJd&�ZndYdKd#�ZodLdM�ZpGdNdO�dOeQjq�ZrdZdPd'�ZsdQd)�ZtdRd(�ZudSdT�ZvdUdV�ZwdS)[�)�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_url�parse_content_disposition�sanitize_content_filenamecCs dtjd�tj�dtj�id�}|dddkrBtj�|dd<n�|dddkr�tjjd krntjd d �}ntj}d jd d�|D��|dd<nB|dddkr�tj�|dd<n |dddkr�tj�|dd<tjj d��rJddl m }t t dd�tdddg|j����}t t dd�tddgt����}|�r<||d<|�rJ||d<tjj d��r|tj�d�r|dtj�dd�|d<tj��r�tj�|jdi�d<tj��r�tj�|jdi�d<tj��r�tj�|d<t�r�tjd d �d)k�r�tj|d"<d#j|tj|d*d&d'�d(�S)+z6 Return a string representing the user agent. �pip)�name�versionr8)Z installer�python�implementationr;ZCPythonr9ZPyPy�finalN��.cSsg|] }t|��qS�)�str)�.0�xr?r?�/usr/lib/python3.6/download.py� Tszuser_agent..ZJythonZ IronPython�linuxr)�distrocSs|dS)N�r?)rBr?r?rC�`szuser_agent..�idcSs|dS)NrGr?)rBr?r?rCrHds�lib�libcrF�darwinZmacOS�system�releaseZcpu��Zopenssl_versionz9{data[installer][name]}/{data[installer][version]} {json}�,�:T)Z separatorsZ sort_keys)�data�json)rOrP)rQrR)r7� __version__�platformZpython_versionZpython_implementation�sys�pypy_version_info� releaselevel�join� startswith� pip._vendorrF�dict�filter�zipZlinux_distributionrZmac_verrM� setdefaultrN�machine�HAS_TLS� version_info�sslZOPENSSL_VERSION�formatrT�dumps)rSrXrFZ distro_infosrKr?r?rC� user_agent@sP        rgc@s.eZdZd dd�Zdd�Zdd�Zdd �Zd S) �MultiDomainBasicAuthTcCs||_i|_dS)N)� prompting� passwords)�selfrir?r?rC�__init__�szMultiDomainBasicAuth.__init__cCs�tj|j�}|jjdd�d}tj|dd�|f|dd��|_|jj|d�\}}|dkrn|j|j�\}}|dkr�|dkr�t |j�}|r�|nd\}}|s�|r�||f|j|<t |p�d|p�d�|�}|j d|j �|S) N�@rGrO��response�����)NN)NN) � urllib_parse�urlparse�url�netloc�rsplit� urlunparserj�get�parse_credentialsr#r Z register_hook� handle_401)rk�req�parsedrt�username�passwordZ netrc_authr?r?rC�__call__�s & zMultiDomainBasicAuth.__call__cKs�|jdkr|S|js|Stj|j�}tjjd|j�}t j d�}|sH|rX||f|j |j<|j |j j �t|ppd|pvd�|j�}|jj|f|�}|jj|�|S)Ni�z User for %s: z Password: rn)� status_coderirqrrrsrZmoves�inputrt�getpassrj�content�rawZ release_connr rZ connection�send�history�append)rk�resp�kwargsr{r|r}rzZnew_respr?r?rCry�s     zMultiDomainBasicAuth.handle_401cCs8d|kr4|jdd�d}d|kr,|jdd�S|dfSdS)NrmrGrrR)NN)ru�split)rkrtZuserinfor?r?rCrx�s  z&MultiDomainBasicAuth.parse_credentialsN)T)�__name__� __module__� __qualname__rlr~ryrxr?r?r?rCrh�s !"rhc@seZdZddd�Zdd�ZdS)�LocalFSAdapterNc Cs�t|j�}t�}d|_|j|_ytj|�} Wn.tk rZ} zd|_| |_WYdd} ~ XnPXtj j | j dd�} t j |�dp~d} t| | j| d��|_t|d�|_|jj|_|S) N��i�T)Zusegmtrz text/plain)z Content-TypezContent-Lengthz Last-Modified�rb)r,rsr"r�os�stat�OSErrorr��emailZutilsZ formatdate�st_mtime� mimetypes� guess_typer$�st_size�headers�open�close) rkr�stream�timeout�verify�certZproxies�pathnamer�Zstats�excZmodified� content_typer?r?rCr��s$    zLocalFSAdapter.sendcCsdS)Nr?)rkr?r?rCr��szLocalFSAdapter.close)NNNNN)r�r�r�r�r�r?r?r?rCr��s r�csDeZdZdZ�fdd�Z�fdd�Z�fdd�Z�fdd �Z�ZS) � SafeFileCachezw A file based cache which is safe to use even when the target directory may not be accessible or writable. cs4tt|�j||�t|j�s0tjd|j�d|_dS)Nz�The 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�rlr� directory�logger�warning)rk�argsr�)� __class__r?rCrl�s  zSafeFileCache.__init__c s@|jdkrdSytt|�j||�Stttfk r:YnXdS)N)r�r�r�rwr(r��IOError)rkr�r�)r�r?rCrws  zSafeFileCache.getc s@|jdkrdSytt|�j||�Stttfk r:YnXdS)N)r�r�r��setr(r�r�)rkr�r�)r�r?rCr�s  zSafeFileCache.setc s@|jdkrdSytt|�j||�Stttfk r:YnXdS)N)r�r�r��deleter(r�r�)rkr�r�)r�r?rCr�)s  zSafeFileCache.delete) r�r�r��__doc__rlrwr�r�� __classcell__r?r?)r�rCr��s   r�c@seZdZdd�ZdS)�InsecureHTTPAdaptercCsd|_d|_dS)NZ CERT_NONE)Z cert_reqsZca_certs)rkZconnrsr�r�r?r?rC� cert_verify9szInsecureHTTPAdapter.cert_verifyN)r�r�r�r�r?r?r?rCr�7sr�cs,eZdZdZ�fdd�Z�fdd�Z�ZS)� PipSessionNc s�|jdd�}|jdd�}|jdg�}tt|�j||�t�|jd<t�|_tj |dgdd�}|rvt t |d d �|d �}n t |d �}t |d �}|jd |�|jd|�|jdt��x|D]}|jdj|�|�q�WdS)N�retriesr�cache�insecure_hostsz User-Agenti�g�?)ZtotalZstatus_forcelistZbackoff_factorT)Z use_dir_lock)r�� max_retries)r�zhttps://zhttp://zfile://z https://{0}/)�popr�r�rlrgr�rhZauthr%ZRetryr&r�rr�Zmountr�re) rkr�r�r�r�r�Zsecure_adapterZinsecure_adapter�host)r�r?rCrlBs*           zPipSession.__init__cs(|jd|j�tt|�j||f|�|�S)Nr�)r`r�r�r�r)rk�methodrsr�r�)r�r?rCr~szPipSession.request)r�r�r�r�rlrr�r?r?)r�rCr�>s             z^(http|https|file):z /*([a-z])\|cCs6d|kr dS|jdd�dj�}|ddddgtjkS) z)Returns true if the name looks like a URLrRFrGrr�Zhttpsr�Zftp)r�r�rZ all_schemes)r8r�r?r?rCr+�scCsH|jd�std|��tj|�\}}}}}|r6d|}tj||�}|S)z( Convert a file: URL to a path. zfile:z4You can only turn file: urls into filenames (not %r)z\\)r[�AssertionErrorrqZurlsplit�urllib_requestZ url2pathname)rs�_rtr�r?r?rCr,�s  cCs*tjjtjj|��}tjdtj|��}|S)zh Convert a path to a file: URL. The path will be made absolute and have quoted path parts. zfile:)r�r��normpath�abspathrqZurljoinr�Z pathname2url)r�rsr?r?rCr-�scCs t|�dj�}|tkrdSdS)z9Return True if `name` is a considered as an archive file.rGTF)rr�r)r8�extr?r?rCr.�scCst|�}|j|�dS)N)�_get_used_vcs_backend�unpack)�link�location� vcs_backendr?r?rCr/�scCs.x(tjD]}|j|jkr||j�}|SqWdS)N)rZbackendsr�Zschemesrs)r�Zbackendr�r?r?rCr��s   r�cCs tt|��S)N)�boolr�)r�r?r?rCr1�scCs|jj�jd�S)Nzfile:)rsr�r[)r�r?r?rCr2�scCst|j�}tjj|�S)z�Return 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?rC� is_dir_url�s r�cOs|S)Nr?)�iterabler�r�r?r?rC�_progress_indicator�sr�c sLyt�jd�}Wntttfk r0d}YnXt�dd�}tj�tj krRd}n&|r\d}n|dkrjd}n|std}nd}|j }�fdd �}�fd d �} t } |j t j kr�|} n|j} |r�|r�tjd | t|��t|d �j} ntjd| �t�j} n |�rtjd| �n tjd| �tjd|�| | |t�t��} |�r@|j| �nt| �dS)Nzcontent-lengthrZ from_cacheF�(i�Tc 3s\y$x�jj|dd�D] }|VqWWn2tk rVx�jj|�}|sHP|Vq6WYnXdS)NF)Zdecode_content)r�r��AttributeErrorr�)Z chunk_size�chunk)r�r?rC� resp_reads  z _download_url..resp_readc3s"x|D]}�j|�|VqWdS)N)�write)Zchunksr�)� content_filer?rC�written_chunks;s  z%_download_url..written_chunkszDownloading %s (%s))�maxzDownloading %szUsing cached %szDownloading from URL %si@�)�intr�� ValueError�KeyErrorr��getattrr�ZgetEffectiveLevel�logging�INFO�show_urlr�rtrr��infor r�iterr�debugr!Zcheck_against_chunksr) r�r�r��hashesZ total_lengthZ cached_respZ show_progressr�r�r�Zprogress_indicatorrsZdownloaded_chunksr?)r�r�rC� _download_urlsL   %       r�cCs�d}tjj||j�}tjj|�r�tdt|�d �}|dkr@d}nj|dkrdtjdt|��tj |�nF|dkr�t |�}tjd t|�t|��t j ||�n|dkr�t jd �|r�t j||�tjd t|��dS)NTz8The file %s exists. (i)gnore, (w)ipe, (b)ackup, (a)abort�i�w�b�aFz Deleting %szBacking up %s to %srGzSaved %s)r�r�r�r�rp)r�r�rZ�filename�existsr r r�r��remover �shutilZmoverW�exit�copyr�)r�r�r�r�Zdownload_locationroZ dest_filer?r?rC� _copy_fileas.     r�c Cs�|dkrtd��tjdd�}d}|r0t|||�}|rH|}tj|�d}nt||||�\}}t||||�|r~| r~t|||�|s�t j |�t |�dS)Nz@unpack_http_url() missing 1 required keyword argument: 'session'z-unpackzpip-r) r��tempfileZmkdtemp�_check_download_dirr�r��_download_http_urlrr�r��unlinkr ) r�r�� download_dirr�r��temp_dir�already_downloaded_path� from_pathr�r?r?rCr3|s,     cCs�t|j�}t|�rHtjj|�r&t|�tj||dd�|rDt j d�dS|rV|j |�d}|rjt |||�}|rt|}n|}t j|�d}t||||�|r�| r�t|||�dS)z�Unpack 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)Zsymlinksz*Link is a directory, ignoring download_dirNr)r,r�r�r�r�r�r r�Zcopytreer�r��check_against_pathr�r�r�rr�)r�r�rr�r�rrr�r?r?rCr0�s,     c Cs�tjj|�rt|�d}tjg}|jd�|jt|�|jd�|d|g7}tj d|�t ��t ||dd�WdQRXtjj |tj |�d �}tj d ||�t||ddd �dS) z�Copy 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 zsetup.pyz-c�sdistz --dist-dirzRunning setup.py sdist for %sF)�cwdZ show_stdoutNrzUnpacking sdist %s into %s)r�r�)r�r�r�r rW� executabler�rr�r�rrrZ�listdirr)r�r�Zsetup_pyZ sdist_argsrr?r?rC�_copy_dist_from_dir�s     r c@s$eZdZdZddd�Zd dd�ZdS) �PipXmlrpcTransportzRProvide a `xmlrpclib.Transport` implementation via a `PipSession` object. FcCs*tjj||�tj|�}|j|_||_dS)N)r)� Transportrlrqrrr��_scheme�_session)rkZ index_urlr�Z use_datetimeZ index_partsr?r?rCrl�s zPipXmlrpcTransport.__init__c Cs�|j||dddf}tj|�}y6ddi}|jj|||dd�}|j�||_|j|j�St j k r�} zt j d| j j|��WYdd} ~ XnXdS)Nz Content-Typeztext/xmlT)rSr�r�zHTTP error %s while getting %s)rrqrvrZpostr��verboseZparse_responser�r� HTTPErrorr��criticalror) rkr�ZhandlerZ request_bodyr�partsrsr�ror�r?r?rCrs     zPipXmlrpcTransport.requestN)F)F)r�r�r�r�rlrr?r?r?rCr �s r cCs^t|�rt||�n:t|�r.t||||d�n |dkrs�     0               BR!BH )  ` & 0$ '8