o
    !7hF+                     @   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	m
Z
mZmZ ddlmZ ddlmZ ddl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m Z m!Z! ddl"m#Z#m$Z$m%Z% e&e'Z(dedee) fddZ*dedee+ fddZ,	d,dedede+dee) dee) de
e- fddZ.de+de+fddZ/de+de+de+fd d!Z0dedede+fd"d#Z1		d-d$ededee) d%ee+ def
d&d'Z2G d(d) d)Z3G d*d+ d+Z4dS ).z(Download files with progress indicators.    N)
HTTPStatus)BinaryIOIterableOptionalTuple)Response)ReadTimeoutError)get_download_progress_renderer)IncompleteDownloadErrorNetworkConnectionError)PyPI)Link)is_from_cache)
PipSession)HEADERSraise_for_statusresponse_chunks)format_sizeredact_auth_from_urlsplitextrespreturnc              
   C   s,   zt | jd W S  tttfy   Y d S w )Nzcontent-length)intheaders
ValueErrorKeyError	TypeErrorr    r   s/var/www/epreuve.sigeris.cm/public_html/epreuve/venv/lib/python3.10/site-packages/pip/_internal/network/download.py_get_http_response_size   s
   r    c                 C   s   | j d| j dS )z
    Return either the ETag or Last-Modified header (or None if neither exists).
    The return value can be used in an If-Range header.
    etagzlast-modified)r   getr   r   r   r   (_get_http_response_etag_or_last_modified    s   r#   linkprogress_bartotal_lengthrange_startc           
      C   s   |j tjkr
|j}n|j}t|}|r/|r%| dt| dt| d}n
| dt| d}t| r:t	d| n|rCt	d| nt	d| t
 tjkrSd}nt| rZd}n|s_d}n	|d	krfd}nd}t| }|sp|S t|||d
}	|	|S )Nz (/)zUsing cached %szResuming download %szDownloading %sFTi   )bar_typesizeinitial_progress)netlocr   file_storage_domainshow_urlurl_without_fragmentr   r   r   loggerinfogetEffectiveLevelloggingINFOr   r	   )
r   r$   r%   r&   r'   url
logged_urlshow_progresschunksrendererr   r   r   _prepare_download(   s<   r;   filenamec                 C   s   t j| S )zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )ospathbasename)r<   r   r   r   sanitize_content_filename[   s   r@   content_dispositiondefault_filenamec                 C   s4   t j }| |d< |d}|rtt|}|p|S )z
    Parse the "filename" value from a Content-Disposition header, and
    return the default filename if the result is empty.
    content-typer<   )emailmessageMessage	get_paramr@   str)rA   rB   mr<   r   r   r   parse_content_dispositionb   s   

rJ   c                 C   s   |j }| jd}|rt||}t|d }|s(t| jdd}|r(||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   rC    )
r<   r   r"   rJ   r   	mimetypesguess_extensionr6   r=   r>   )r   r$   r<   rA   extr   r   r   _get_http_response_filenameq   s   
rP   sessionif_rangec              
   C   s   |j ddd }t }|rd| d|d< |r||d< z| j||dd	}t| W |S  tyI } z|jd us;J t	d
|jj
|  d }~ww )N#rK   r   zbytes=-RangezIf-RangeT)r   streamzHTTP error %s while getting %s)r6   splitr   copyr"   r   r   responser1   criticalstatus_code)rQ   r$   r'   rR   
target_urlr   r   er   r   r   _http_get_download   s    
r^   c                   @   s   e Zd ZdedededdfddZded	edeeef fd
dZ	de
dedededee defddZdee dededefddZde
dededee deddfddZde
dedeeee ee f fddZdS )
DownloaderrQ   r%   resume_retriesr   Nc                 C   s&   |dksJ d|| _ || _|| _d S )Nr   z<Number of max resume retries must be bigger or equal to zero)_session_progress_bar_resume_retriesselfrQ   r%   r`   r   r   r   __init__   s   

zDownloader.__init__r$   locationc           
      C   s   t | j|}t|}|jdd}t||}tj||}t	|d7}| 
|||d|}	|rE|	|k rO| |||||	 W d   ||fS W d   ||fS W d   ||fS 1 s\w   Y  ||fS )z.Download the file given by link into location.zContent-TyperL   wbr   N)r^   ra   r    r   r"   rP   r=   r>   joinopen_process_response_attempt_resume)
re   r$   rg   r   r&   content_typer<   filepathcontent_filebytes_receivedr   r   r   __call__   s.   









zDownloader.__call__r   ro   rp   r&   c                 C   s(   t ||| j||d}| j||t|dS )z6Process the response and write the chunks to the file.)r'   )allow_partial)r;   rb   _write_chunks_to_filebool)re   r   r$   ro   rp   r&   r9   r   r   r   rk      s   	
zDownloader._process_responser9   rr   c             
   C   sd   d}z|D ]}|t |7 }|| qW |S  ty1 } z|s!|td W Y d}~|S d}~ww )zEWrite the chunks to the file and return the number of bytes received.r   z'Connection timed out while downloading.N)lenwriter   r1   warning)re   r9   ro   rr   rp   chunkr]   r   r   r   rs      s   z Downloader._write_chunks_to_filec           
   
   C   s   t |}| j}|r^|r^||k r^|d8 }tdt|t|| j|  z't| j|||d}|jtj	k}	|	r=| 
||\}}}|| |||||7 }W n tttfyU   Y qw |r^|r^||k s|rs||k rut|j t|||| jddS dS )z9Attempt to resume the download if connection was dropped.rK   z<Attempting to resume incomplete download (%s/%s, attempt %d))r'   rR   )retriesN)r#   rc   r1   rw   r   r^   ra   r[   r   PARTIAL_CONTENT_reset_download_staterk   ConnectionErrorr   OSErrorr=   removenamer
   )
re   r   r$   ro   r&   rp   etag_or_last_modifiedattempts_leftresume_respmust_restartr   r   r   rl      sD   	



#
zDownloader._attempt_resumec                 C   s0   | d |  d}t|}t|}|||fS )zCReset the download state to restart downloading from the beginning.r   )seektruncater    r#   )re   r   ro   rp   r&   r   r   r   r   r{     s   

z Downloader._reset_download_state)__name__
__module____qualname__r   rH   r   rf   r   r   rq   r   r   r   rk   r   bytesrt   rs   rl   r{   r   r   r   r   r_      sj    



5r_   c                   @   sT   e Zd ZdedededdfddZdee d	edee	ee	eef f  fd
dZ
dS )BatchDownloaderrQ   r%   r`   r   Nc                 C   s   t |||| _d S )N)r_   _downloaderrd   r   r   r   rf   ,  s   zBatchDownloader.__init__linksrg   c                 c   s.    |D ]}|  ||\}}|||ffV  qdS )z0Download the files given by links into location.N)r   )re   r   rg   r$   rn   rm   r   r   r   rq   4  s
   zBatchDownloader.__call__)r   r   r   r   rH   r   rf   r   r   r   rq   r   r   r   r   r   +  s"    
r   )r   )r   N)5__doc__email.messagerD   r4   rM   r=   httpr   typingr   r   r   r   pip._vendor.requests.modelsr   pip._vendor.urllib3.exceptionsr   pip._internal.cli.progress_barsr	   pip._internal.exceptionsr
   r   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   	getLoggerr   r1   r   r    rH   r#   r   r;   r@   rJ   rP   r^   r_   r   r   r   r   r   <module>   sj    

3
 