a
    Œ�éi  ã                   @   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mZmZ dd
lmZ erÐddlmZ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)ÚCONTENT_CHUNK_SIZE)ÚDownloadProgressProvider)ÚNetworkConnectionError)ÚPyPI)Úis_from_cache)ÚHEADERSÚraise_for_statusÚresponse_chunks)Úformat_sizeÚredact_auth_from_urlÚsplitext)ÚMYPY_CHECK_RUNNING)ÚIterableÚOptionalÚTuple)ÚResponse)ÚLink)Ú
PipSessionc              
   C   s0   zt | jd ƒW S  tttfy*   Y d S 0 d S )Nzcontent-length)ÚintÚheadersÚ
ValueErrorÚKeyErrorÚ	TypeError)Úresp© r   úN/home/Claro/venv/lib/python3.9/site-packages/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%   s6    
ÿþ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_filenameQ   s    r4   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.
    r3   )ÚcgiÚparse_headerÚgetr4   )Úcontent_dispositionÚdefault_filenameÚ_typeÚparamsr3   r   r   r   Úparse_content_dispositionY   s
    
r<   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Ú )
r3   r   r7   r<   r   Ú	mimetypesÚguess_extensionr+   r0   r1   )r   r(   r3   r8   Úextr   r   r   Ú_get_http_response_filenameh   s     
ÿrB   c                 C   s.   |j  dd¡d }| j|tdd�}t|ƒ |S )Nú#r=   r   T)r   Ústream)r+   Úsplitr7   r   r   )Úsessionr(   Ú
target_urlr   r   r   r   Ú_http_get_download€   s    rH   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
Downloaderc                 C   s   || _ || _d S ©N©Ú_sessionÚ_progress_bar©ÚselfrF   r)   r   r   r   Ú__init__‰   s    zDownloader.__init__c              
   C   sÒ   zt | j|ƒ}W nF tyV } z.|jdus.J ‚t d|jj|¡ ‚ W Y d}~n
d}~0 0 t||ƒ}tj	 
||¡}t||| jƒ}t|dƒ�$}|D ]}	| |	¡ qŽW d  ƒ n1 s²0    Y  |j dd¡}
||
fS )z.Download the file given by link into location.NúHTTP error %s while getting %sÚwbúContent-Typer>   )rH   rL   r   Úresponser#   ÚcriticalÚstatus_coderB   r0   r1   Újoinr/   rM   ÚopenÚwriter   r7   )rO   r(   Úlocationr   Úer3   Úfilepathr.   Úcontent_fileÚchunkÚcontent_typer   r   r   Ú__call__’   s     
ÿ
*zDownloader.__call__N©Ú__name__Ú
__module__Ú__qualname__rP   r`   r   r   r   r   rI   ˆ   s   	rI   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚBatchDownloaderc                 C   s   || _ || _d S rJ   rK   rN   r   r   r   rP   «   s    zBatchDownloader.__init__c                 c   sè   |D ]Þ}zt | j|ƒ}W nF ty^ } z.|jdus6J ‚t d|jj|¡ ‚ W Y d}~n
d}~0 0 t||ƒ}tj	 
||¡}t||| jƒ}t|dƒ�$}	|D ]}
|	 |
¡ q–W d  ƒ n1 sº0    Y  |j dd¡}|j||ffV  qdS )z0Download the files given by links into location.NrQ   rR   rS   r>   )rH   rL   r   rT   r#   rU   rV   rB   r0   r1   rW   r/   rM   rX   rY   r   r7   r+   )rO   ÚlinksrZ   r(   r   r[   r3   r\   r.   r]   r^   r_   r   r   r   r`   ´   s$    þ
*zBatchDownloader.__call__Nra   r   r   r   r   re   ©   s   	re   ).Ú__doc__r5   r&   r?   r0   Úpip._vendor.requests.modelsr   Úpip._internal.cli.progress_barsr   Úpip._internal.exceptionsr   Úpip._internal.models.indexr   Úpip._internal.network.cacher   Úpip._internal.network.utilsr   r   r	   Úpip._internal.utils.miscr
   r   r   Úpip._internal.utils.typingr   Útypingr   r   r   r   Úpip._internal.models.linkr   Úpip._internal.network.sessionr   Ú	getLoggerrb   r#   r   r/   r4   r<   rB   rH   ÚobjectrI   re   r   r   r   r   Ú<module>   s4   
,!