a
    e                     @   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rd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   R/var/www/sunrise/env/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	| 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   linkprogress_bartotal_lengthurlZ
logged_urlZ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    r2   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.
    r1   )cgiparse_headergetr2   )content_dispositionZdefault_filename_typeparamsr1   r   r   r   parse_content_dispositionY   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 )
r1   r   r5   r9   r   	mimetypesguess_extensionr+   r.   r/   )r   r(   r1   r6   extr   r   r   _get_http_response_filenameh   s     
r?   c                 C   s.   |j ddd }| j|tdd}t| |S )N#r:   r   T)r   stream)r+   splitr5   r   r   )sessionr(   Z
target_urlr   r   r   r   _http_get_download   s    rD   c                   @   s   e Zd Zdd Zdd ZdS )
Downloaderc                 C   s   || _ || _d S N_session_progress_barselfrC   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 ]}	||	 qW d   n1 s0    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;   )rD   rH   r   responser#   criticalstatus_coder?   r.   r/   joinr-   rI   openwriter   r5   )rK   r(   locationr   er1   filepathr,   content_filechunkcontent_typer   r   r   __call__   s     

*zDownloader.__call__N__name__
__module____qualname__rL   r\   r   r   r   r   rE      s   	rE   c                   @   s   e Zd Zdd Zdd ZdS )BatchDownloaderc                 C   s   || _ || _d S rF   rG   rJ   r   r   r   rL      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 ]}
|	|
 qW d   n1 s0    Y  |jdd}|j||ffV  qdS )z0Download the files given by links into location.NrM   rN   rO   r;   )rD   rH   r   rP   r#   rQ   rR   r?   r.   r/   rS   r-   rI   rT   rU   r   r5   r+   )rK   linksrV   r(   r   rW   r1   rX   r,   rY   rZ   r[   r   r   r   r\      s$    
*zBatchDownloader.__call__Nr]   r   r   r   r   ra      s   	ra   ).__doc__r3   r&   r<   r.   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   	getLoggerr^   r#   r   r-   r2   r9   r?   rD   objectrE   ra   r   r   r   r   <module>   s4   
,!