ó
ĵS]c           @@ s   d  d l  m Z m Z d  d l m Z d  d l m Z d  d l j j Z d  d l	 j j
 Z
 d  d l Z d  d l Z d e f d     YZ d S(   i    (   t   absolute_importt   print_function(   t
   httpclient(   t   genNt   FileDownloaderc           B@ sD   e  Z d d   Z e j d    Z e d    Z e d    Z	 RS(   c         C@ s:   | |  _  | |  _ | |  _ d |  _ d |  _ g  |  _ d S(   s   Downloader for the given url to the given filename, computing the given hash.

        hash_algorithm is the name of a hash function in hashlib
        N(   t   _urlt	   _filenamet   _hash_algorithmt   Nonet   _hasht   _clientt   _errors(   t   selft   urlt   filenamet   hash_algorithm(    (    sD   lib/python2.7/site-packages/anaconda_project/internal/http_client.pyt   __init__   s    					c      	   #@ sı   j  d k s t  t j j  j  } y t j |  Wn< t	 k
 ry }  j
 j d | | f  t j d   n X j d k	 rĦ t t  j     n  t j d d d d d d d t   _   j d	  y t  d
    Wn< t k
 r&}  j
 j d  | f  t j d   n X   f d   }      f d   } zYd } t j d  j d | d |  } y  j  j |  V} WnE t	 k
 rÙ}  j
 j d  j t |  f  t j d   n X| j d k sït  t  j
  d k rdy!   j   t j   j  Wqdt k
 r`}  j
 j d   j t |  f  qdXn  t  j
  d k r j d k	 r j    _  n  t j |   Wd |   Xd S(   s&   Run the download on the given io_loop.s#   Could not create directory '%s': %st   max_clientsi   t   max_buffer_sizei   t   max_body_sizeid   t   force_instances   .partt   wbs   Failed to open %s: %sc           @ s3   y   j    t j   Wn t k
 r. n Xd  S(   N(   t   closet   ost   removet   EnvironmentError(    (   t   _filet   tmp_filename(    sD   lib/python2.7/site-packages/anaconda_project/internal/http_client.pyt   cleanup_tmpB   s
    
c         @ s}   t   j  d k r d  S j d  k	 r8  j |   n  y   j |   Wn- t k
 rx }  j j d  | f  n Xd  S(   Ni    s   Failed to write to %s: %s(   t   lenR   R   R   t   updatet   writeR   t   append(   t   chunkt   e(   R   t   hasherR   R   (    sD   lib/python2.7/site-packages/anaconda_project/internal/http_client.pyt   writerM   s    i<   i
   R   t   streaming_callbackt   request_timeouts   Failed download to %s: %si    s   Failed to rename %s to %s: %sNi   i  i  @I       iX  (!   R
   R   t   AssertionErrorR   t   patht   dirnameR   t   makedirst   makedirs_ok_if_existst	   ExceptionR   R    R   t   ReturnR   t   getattrt   hashlibR   t   AsyncHTTPClientt   Truet   openR   t   HTTPRequestR   t   fetcht   strt   errorR   R   t   renamet   rename_over_existingt	   hexdigestR	   (   R   R)   R"   R   R$   t   timeout_in_secondst   requestt   response(    (   R   R#   R   R   sD   lib/python2.7/site-packages/anaconda_project/internal/http_client.pyt   run!   sT    		#
-$c         C@ s   |  j  S(   sQ   Hash of the downloaded file if we succeeded in downloading it, None if we failed.(   R	   (   R   (    (    sD   lib/python2.7/site-packages/anaconda_project/internal/http_client.pyt   hashw   s    c         C@ s   |  j  S(   sD   List of errors if we failed to download, empty list if we succeeded.(   R   (   R   (    (    sD   lib/python2.7/site-packages/anaconda_project/internal/http_client.pyt   errors|   s    N(
   t   __name__t
   __module__R   R   R   t	   coroutineR=   t   propertyR>   R?   (    (    (    sD   lib/python2.7/site-packages/anaconda_project/internal/http_client.pyR      s   V(   t
   __future__R    R   t   tornadoR   R   t"   anaconda_project.internal.makedirst   internalR*   t    anaconda_project.internal.renameR7   R   R/   t   objectR   (    (    (    sD   lib/python2.7/site-packages/anaconda_project/internal/http_client.pyt   <module>   s   