
(h                 @   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 j" e#  Z$ d d   Z% d d   Z& d d   Z' d d   Z( d d   Z) d d   Z* Gd d   d e+  Z, Gd 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   s;   y t  |  j d  SWn t t t f k
 r6 d  SYn Xd  S)Nzcontent-length)intheaders
ValueErrorKeyError	TypeError)resp r   =/tmp/pip-build-kmztzv_d/pip/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 j | t |   } t	 |   r| t
 j d |  n t
 j d |  t
 j   t j k r d } n? t	 |   r d } n* | s d } n | d	 k r d } n d } t |  t  } | s| St | d | |  S)
Nz{} ({})zUsing cached %szDownloading %sFT(   i  maxi@  )r   netlocr   Z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_urlshow_progresschunksr   r   r   _prepare_download%   s2    					r.   c             C   s   t  j j |   S)zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )ospathbasename)filenamer   r   r   sanitize_content_filenameQ   s    r3   c             C   s@   t  j |   \ } } | j d  } | r6 t |  } | p? | S)z
    Parse the "filename" value from a Content-Disposition header, and
    return the default filename if the result is empty.
    r2   )cgiparse_headergetr3   )content_dispositionZdefault_filename_typeparamsr2   r   r   r   parse_content_dispositionY   s
    r:   c             C   s   | j  } |  j j d  } | r0 t | |  } t |  d } | st t j |  j j d d   } | rt | | 7} | r | j |  j k r t j	 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 )
r2   r   r6   r:   r   	mimetypesguess_extensionr+   r/   r0   )r   r(   r2   r7   extr   r   r   _get_http_response_filenameh   s    	

r@   c             C   sB   | j  j d d  d } |  j | d t d d } t |  | S)N#r;   r   r   streamT)r+   splitr6   r   r   )sessionr(   
target_urlr   r   r   r   _http_get_download   s    
rF   c               @   s(   e  Z d  Z d d   Z d d   Z d S)
Downloaderc             C   s   | |  _  | |  _ d  S)N)_session_progress_bar)selfrD   r)   r   r   r   __init__   s    	zDownloader.__init__c             C   s   y t  |  j |  } WnV t k
 rn } z6 | j d k	 s@ t  t j d | j j |    WYd d } ~ Xn Xt | |  } t	 j
 j | |  } t | | |  j  } t | d  % } x | D] }	 | j |	  q WWd QRX| j j d d  }
 | |
 f S)z.Download the file given by link into location.NzHTTP error %s while getting %swbzContent-Typer<   )rF   rH   r   responseAssertionErrorr#   criticalstatus_coder@   r/   r0   joinr.   rI   openwriter   r6   )rJ   r(   locationr   er2   filepathr-   content_filechunkcontent_typer   r   r   __call__   s    zDownloader.__call__N)__name__
__module____qualname__rK   rZ   r   r   r   r   rG      s   	rG   c               @   s(   e  Z d  Z d d   Z d d   Z d S)BatchDownloaderc             C   s   | |  _  | |  _ d  S)N)rH   rI   )rJ   rD   r)   r   r   r   rK      s    	zBatchDownloader.__init__c             c   s  x| D]} y t  |  j |  } WnV t k
 r{ } z6 | j d k	 sM t  t j d | j j |    WYd d } ~ Xn Xt | |  } t	 j
 j | |  } t | | |  j  } t | d  % }	 x | D] }
 |	 j |
  q WWd QRX| j j d d  } | j | | f f Vq Wd S)z0Download the files given by links into location.NzHTTP error %s while getting %srL   zContent-Typer<   )rF   rH   r   rM   rN   r#   rO   rP   r@   r/   r0   rQ   r.   rI   rR   rS   r   r6   r+   )rJ   linksrT   r(   r   rU   r2   rV   r-   rW   rX   rY   r   r   r   rZ      s"    zBatchDownloader.__call__N)r[   r\   r]   rK   rZ   r   r   r   r   r^      s   	r^   ).__doc__r4   r&   r=   r/   Zpip._vendor.requests.modelsr   Zpip._internal.cli.progress_barsr   Zpip._internal.exceptionsr   Zpip._internal.models.indexr   Zpip._internal.network.cacher   Zpip._internal.network.utilsr   r   r	   Zpip._internal.utils.miscr
   r   r   Zpip._internal.utils.typingr   Ztypingr   r   r   r   Zpip._internal.models.linkr   Zpip._internal.network.sessionr   	getLoggerr[   r#   r   r.   r3   r:   r@   rF   objectrG   r^   r   r   r   r   <module>   s4   ,!