U
    
Ha                     @   sT  d 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
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mZmZmZ ddlmZmZmZ e e!Z"eee# dddZ$eee%ee& dddZ'e%e%dddZ(e%e%e%dddZ)eee%dddZ*eeedddZ+G dd  d Z,G d!d" d"Z-dS )#z)Download files with progress indicators.
    N)IterableOptionalTuple)CONTENT_CHUNK_SIZEResponse)DownloadProgressProvider)NetworkConnectionError)PyPI)Link)is_from_cache)
PipSession)HEADERSraise_for_statusresponse_chunks)format_sizeredact_auth_from_urlsplitext)respreturnc              
   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)r    r   B/tmp/pip-unpacked-wheel-tx790h60/pip/_internal/network/download.py_get_http_response_size   s    r   )r   linkprogress_barr   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	| rd}n|sd}n|dkrd}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   r   r   total_lengthurl
logged_urlshow_progresschunksr   r   r   _prepare_download   s.    
r/   )filenamer   c                 C   s   t j| S )zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )ospathbasename)r0   r   r   r   sanitize_content_filenameG   s    r4   )content_dispositiondefault_filenamer   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getr4   )r5   r6   _typeparamsr0   r   r   r   parse_content_dispositionN   s
    
r<   )r   r   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 )
r0   r   r9   r<   r   	mimetypesguess_extensionr+   r1   r2   )r   r   r0   r5   extr   r   r   _get_http_response_filename\   s    
rB   )sessionr   r   c                 C   s.   |j ddd }| j|tdd}t| |S )N#r=   r   T)r   stream)r+   splitr9   r   r   )rC   r   
target_urlr   r   r   r   _http_get_downloadq   s    rH   c                   @   s8   e Zd ZeeddddZeeeeef dddZdS )
DownloaderNrC   r   r   c                 C   s   || _ || _d S N_session_progress_barselfrC   r   r   r   r   __init__y   s    zDownloader.__init__)r   locationr   c              
   C   s   zt | j|}W nD tk
rT } z&|jdk	s0ttd|jj|  W 5 d}~X Y nX t||}t	j
||}t||| j}t|d}|D ]}	||	 qW 5 Q R X |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   rM   r   responseAssertionErrorr%   criticalstatus_coderB   r1   r2   joinr/   rN   openwriter   r9   )rP   r   rR   r   er0   filepathr.   content_filechunkcontent_typer   r   r   __call__   s$      
zDownloader.__call__)	__name__
__module____qualname__r   strrQ   r
   r   rc   r   r   r   r   rI   x   s
   rI   c                   @   sH   e Zd ZeeddddZee eeeeeeef f  dddZ	dS )BatchDownloaderNrJ   c                 C   s   || _ || _d S rK   rL   rO   r   r   r   rQ      s    zBatchDownloader.__init__)linksrR   r   c                 c   s   |D ]}zt | j|}W nD tk
r\ } z&|jdk	s8ttd|jj|  W 5 d}~X Y nX t||}t	j
||}t||| j}t|d}	|D ]}
|	|
 qW 5 Q R X |jdd}|||ffV  qdS )z0Download the files given by links into location.NrS   rT   rU   r>   rV   )rP   ri   rR   r   r   r^   r0   r_   r.   r`   ra   rb   r   r   r   rc      s&    
zBatchDownloader.__call__)
rd   re   rf   r   rg   rQ   r   r
   r   rc   r   r   r   r   rh      s   	 rh   ).__doc__r7   r(   r?   r1   typingr   r   r   pip._vendor.requests.modelsr   r   Zpip._internal.cli.progress_barsr   pip._internal.exceptionsr   pip._internal.models.indexr	   pip._internal.models.linkr
   pip._internal.network.cacher   pip._internal.network.sessionr   pip._internal.network.utilsr   r   r   pip._internal.utils.miscr   r   r   	getLoggerrd   r%   r   r   rg   bytesr/   r4   r<   rB   rH   rI   rh   r   r   r   r   <module>   s6   
)