U
    «ÃT_t  ã                   @   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 ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ erÄddlmZmZ ddlmZ ddlmZ ddlmZ e e ¡Z!dd„ Z"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'G dd„ de(ƒZ)G dd„ de(ƒZ*dS )z)Download files with progress indicators.
é    N)Úrequests)ÚCONTENT_CHUNK_SIZE)ÚPyPI)Úis_from_cache)Úresponse_chunks)Úformat_sizeÚredact_auth_from_urlÚsplitext)ÚMYPY_CHECK_RUNNING)ÚDownloadProgressProvider)ÚIterableÚOptional)ÚResponse)ÚLink)Ú
PipSessionc              
   C   s2   zt | jd ƒW S  tttfk
r,   Y d S X d S )Nzcontent-length)ÚintÚheadersÚ
ValueErrorÚKeyErrorÚ	TypeError)Úresp© r   úB/tmp/pip-unpacked-wheel-8u7dkzi1/pip/_internal/network/download.pyÚ_get_http_response_size!   s    r   c                 C   sÀ   t | ƒ}|jtjkr|j}n|j}t|ƒ}|r>d |t|ƒ¡}t	| ƒrTt
 d|¡ nt
 d|¡ t
 ¡ tjkrtd}n*t	| ƒr‚d}n|sŒd}n|dkršd}nd}t| tƒ}|s°|S t||d�|ƒS )Nz{} ({})zUsing cached %szDownloading %sFTi@œ  )Úmax)r   Únetlocr   Úfile_storage_domainÚshow_urlÚurl_without_fragmentr   Úformatr   r   ÚloggerÚinfoÚgetEffectiveLevelÚloggingÚINFOr   r   r   )r   ÚlinkÚprogress_barÚtotal_lengthÚurlÚ
logged_urlÚshow_progressÚchunksr   r   r   Ú_prepare_download)   s8    
 ÿþr,   c                 C   s   t j | ¡S )zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )ÚosÚpathÚbasename)Úfilenamer   r   r   Úsanitize_content_filenameU   s    r1   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.
    r0   )ÚcgiÚparse_headerÚgetr1   )Úcontent_dispositionÚdefault_filenameÚ_typeÚparamsr0   r   r   r   Úparse_content_disposition]   s
    
r9   c                 C   s‚   |j }| j d¡}|r t||ƒ}t|ƒd }|sPt | j dd¡¡}|rP||7 }|s~|j| jkr~tj	 | j¡d }|r~||7 }|S )zoGet an ideal filename from the given HTTP response, falling back to
    the link filename if not provided.
    zcontent-dispositioné   zcontent-typeÚ )
r0   r   r4   r9   r	   Ú	mimetypesÚguess_extensionr(   r-   r.   )r   r%   r0   r5   Úextr   r   r   Ú_get_http_response_filenamel   s     
ÿr?   c                 C   s2   |j  dd¡d }| j|ddidd�}| ¡  |S )Nú#r:   r   zAccept-EncodingÚidentityT)r   Ústream)r(   Úsplitr4   Úraise_for_status)Úsessionr%   Ú
target_urlr   r   r   r   Ú_http_get_download„   s    êrG   c                   @   s   e Zd Zdd„ ZdS )ÚDownloadc                 C   s   || _ || _|| _d S ©N)Úresponser0   r+   )ÚselfrJ   r0   r+   r   r   r   Ú__init__¤   s    zDownload.__init__N)Ú__name__Ú
__module__Ú__qualname__rL   r   r   r   r   rH   £   s   rH   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
Downloaderc                 C   s   || _ || _d S rI   )Ú_sessionÚ_progress_bar)rK   rE   r&   r   r   r   rL   ±   s    zDownloader.__init__c              
   C   sf   zt | j|ƒ}W n8 tjk
rH } zt d|jj|¡ ‚ W 5 d }~X Y nX t|t	||ƒt
||| jƒƒS )NzHTTP error %s while getting %s)rG   rQ   r   Ú	HTTPErrorr    ÚcriticalrJ   Ústatus_coderH   r?   r,   rR   )rK   r%   r   Úer   r   r   Ú__call__º   s      ÿýzDownloader.__call__N)rM   rN   rO   rL   rW   r   r   r   r   rP   °   s   	rP   )+Ú__doc__r2   r#   r<   r-   Úpip._vendorr   Úpip._vendor.requests.modelsr   Úpip._internal.models.indexr   Úpip._internal.network.cacher   Úpip._internal.network.utilsr   Úpip._internal.utils.miscr   r   r	   Úpip._internal.utils.typingr
   Úpip._internal.utils.uir   Útypingr   r   r   Úpip._internal.models.linkr   Úpip._internal.network.sessionr   Ú	getLoggerrM   r    r   r,   r1   r9   r?   rG   ÚobjectrH   rP   r   r   r   r   Ú<module>   s4   
,