ó
'šMc           @   sr  d  d l  Z  d  d l Z d  d l 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
 m Z m Z m Z m Z m Z d  d l m Z d  d l m Z m Z m Z m Z m Z m Z m Z m Z m Z d  d l m Z d  d l m Z d d d	 d
 d d d d d d d d d d g Z  e
 j! ƒ  Z" d d „ Z$ e  j% d e  j& ƒ Z' e  j% d e  j& ƒ Z( d e) f d „  ƒ  YZ* e* ƒ  Z+ d „  Z, d „  Z- e  j% d e  j& ƒ Z. e  j% d e  j& ƒ Z/ d „  Z0 d „  Z1 d  „  Z2 d! „  Z3 e4 d" „ Z5 d# „  Z6 d$ „  Z7 d% „  Z8 d& „  Z9 d' „  Z: d( „  Z; d) „  Z< d* „  Z= d+ „  Z> d, „  Z? d- e j@ f d. „  ƒ  YZA d S(/   iÿÿÿÿN(   t   md5t   copytreet	   xmlrpclibt   urllibt   urllib2t   urlparset   string_typest	   HTTPError(   t   InstallationError(	   t   splitextt   rmtreet   format_sizet   display_patht
   backup_dirt   askt   unpack_filet   create_download_cache_foldert   cache_download(   t   vcs(   t   loggert   xmlrpclib_transportt   get_file_contentt   urlopent   is_urlt   url_to_patht   path_to_urlt   path_to_url2t   geturlt   is_archive_filet   unpack_vcs_linkt   unpack_file_urlt
   is_vcs_urlt   is_file_urlt   unpack_http_urlc   	      C   s£  t  j |  ƒ } | r9| j d ƒ j ƒ  } | d k rd | rd | j d ƒ rd t d | |  f ƒ ‚ n  | d k r|  j d d ƒ d } | j d d ƒ } t j	 | ƒ } | r× | j d ƒ d | j d d ƒ d } n  t
 j | ƒ } | j d ƒ rd | j d ƒ } n  | }  q9t |  ƒ } t | ƒ | j ƒ  f Sn  y t |  ƒ } | j ƒ  } Wn7 t k
 rŽt j ƒ  d } t d	 t | ƒ ƒ ‚ n X| j ƒ  |  | f S(
   sk   Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content)i   t   filet   https6   Requirements file %s references URL %s, which is localt   :s   \t   /t   |s$   Could not open requirements file: %s(   t
   _scheme_ret   searcht   groupt   lowert
   startswithR   t   splitt   replacet   _url_slash_drive_ret   matchR   t   unquotet   lstripR   R   t   readt   opent   IOErrort   syst   exc_infot   strt   close(	   t   urlt
   comes_fromR/   t   schemet   patht   respt   ft   contentt   e(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR      s8    *	
s   ^(http|https|file):s   /*([a-z])\|t	   URLOpenerc           B   se   e  Z d  Z d „  Z d „  Z d „  Z d
 d
 d „ Z d e d „ Z	 d „  Z
 d „  Z d d	 „ Z RS(   sD   
    pip's own URL helper that adds HTTP auth and proxy support
    c         C   s   t  j ƒ  |  _ d  S(   N(   R   t   HTTPPasswordMgrWithDefaultRealmt   passman(   t   self(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt   __init__F   s    c         C   s§   |  j  | ƒ \ } } } | d k rŽ y t j |  j | ƒ ƒ } Wq£ t j k
 rŠ t j ƒ  d } | j d k rx ‚  n  |  j	 | ƒ } q£ Xn |  j	 | | | ƒ } | S(   s²   
        If the given url contains auth info or if a normal request gets a 401
        response, an attempt is made to fetch the resource using basic HTTP
        auth.

        i   i‘  N(
   t   extract_credentialst   NoneR   R   t   get_requestR   R5   R6   t   codet   get_response(   RD   R9   t   usernamet   passwordt   responseR@   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt   __call__I   s    c         C   s2   t  | t ƒ r. t j | d i d d 6ƒ} n  | S(   sŽ   
        Wraps the URL to retrieve to protects against "creative"
        interpretation of the RFC: http://bugs.python.org/issue8732
        t   headerst   identitys   Accept-encoding(   t
   isinstanceR   R   t   Request(   RD   R9   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyRH   ]   s    c         C   s  t  j | ƒ \ } } } } } |  j | ƒ }	 |  j j d | ƒ \ }
 } |
 d k rã | d k r |  j r t j t	 d | ƒ ƒ } t j t
 j
 d ƒ ƒ } n  | rÅ | rÅ |  j j d | | | ƒ n  |  j j d | ƒ \ }
 } n  t j |  j ƒ } t j | ƒ } | j |	 ƒ S(   s~   
        does the dirty work of actually getting the rsponse object using urllib2
        and its HTTP auth builtins.
        s   User for %s: s
   Password: N(   R   t   urlsplitRH   RC   t   find_user_passwordRG   t	   promptingR   t   quotet	   raw_inputt   getpasst   add_passwordR   t   HTTPBasicAuthHandlert   build_openerR3   (   RD   R9   RK   RL   R;   t   netlocR<   t   queryt   fragt   reqt   stored_usernamet   stored_passwordt   authhandlert   opener(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyRJ   f   s    t    c         C   sd   | |  _  |  j | ƒ } | r` t j i | d 6| d 6ƒ } t j | t j ƒ } t j | ƒ n  d S(   s´   
        Sets the proxy handler given the option passed on the command
        line.  If an empty string is passed it looks at the HTTP_PROXY
        environment variable.
        R#   t   ftpN(   RU   t	   get_proxyR   t   ProxyHandlerR[   t   CacheFTPHandlert   install_opener(   RD   t   proxystrRU   t   proxyt   proxy_supportRc   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt   setup|   s    	c         C   sL   d | k rH | j  d d ƒ d } d | k r> | j d d ƒ S| d  f Sd S(   Nt   @i   i    R$   (   NN(   t   rsplitR,   RG   (   RD   R\   t   userinfo(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt   parse_credentials‰   s    
c         C   s  t  | t j ƒ r* t j | j ƒ  ƒ } n t j | ƒ } | \ } } } } } |  j | ƒ \ } }	 | d k r| | d d f S|	 d k rÕ |  j rÕ | j	 d | d d ƒ } d | | f }
 t
 j t j |
 ƒ ƒ }	 n | j	 d | |	 f d d ƒ } t j | | | | | f ƒ } | | |	 f S(   s   
        Extracts user/password from a url.

        Returns a tuple:
            (url-without-auth, username, password)
        s   %s@Rd   i   s   Password for %s@%s: s   %s:%s@N(   RQ   R   RR   R   RS   t   get_full_urlRq   RG   RU   R-   R   RV   RX   t
   urlunsplit(   RD   R9   t   resultR;   R\   R<   R]   R^   RK   RL   t   promptt
   target_url(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyRF   ‘   s    c         C   s½   | s t  j j d d ƒ } n  | rµ d | k r® | j d d ƒ \ } } d | k ro | j d d ƒ \ } } n. | } d | | f } t j t j | ƒ ƒ } d | | | f S| Sn d Sd S(	   sª   
        Get the proxy given the option passed on the command line.
        If an empty string is passed it looks at the HTTP_PROXY
        environment variable.
        t
   HTTP_PROXYRd   Rn   i   R$   s   Password for %s@%s: s   %s:%s@%sN(   t   ost   environt   getR,   R   RV   RX   RG   (   RD   Rj   t   user_passwordt   server_portt   userRL   Ru   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyRf   ®   s    N(   t   __name__t
   __module__t   __doc__RE   RN   RH   RG   RJ   t   TrueRm   Rq   RF   Rf   (    (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyRA   B   s   						c         C   sI   d |  k r t  S|  j d d ƒ d j ƒ  } | d d d d g t j k S(   s)   Returns true if the name looks like a URLR$   i   i    R#   t   httpsR"   Re   (   t   FalseR,   R*   R   t   all_schemes(   t   nameR;   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR   È   s    c         C   s}   |  j  d ƒ s t d |  ƒ ‚ |  t d ƒ j d ƒ } t j | ƒ } t j | ƒ ro | d d | d } n
 d | } | S(   s(   
    Convert a file: URL to a path.
    s   file:s4   You can only turn file: urls into filenames (not %r)R%   i    R$   i   (   R+   t   AssertionErrort   lenR1   R   R0   t   _url_drive_reR/   (   R9   R<   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR   Ð   s    
s	   ^([a-z]):s   ^([a-z])[:|]c         C   s„   t  j j t  j j |  ƒ ƒ }  t j |  ƒ rF |  d d |  d }  n  t j |  ƒ } | j t  j j	 d ƒ } | j
 d ƒ } d | S(   sI   
    Convert a path to a file: URL.  The path will be made absolute.
    i    R&   i   R%   s   file:///(   Rx   R<   t   normcaset   abspatht	   _drive_reR/   R   RV   R-   t   sepR1   (   R<   R9   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR   ã   s    c         C   sš   t  j j t  j j |  ƒ ƒ }  t  j j |  ƒ \ } }  |  j t  j j ƒ } d j g  | D] } t j	 | ƒ ^ qX ƒ } | sŽ | j
 d ƒ } n  d | | S(   sh   
    Convert a path to a file: URL.  The path will be made absolute and have
    quoted path parts.
    R%   s   file:///(   Rx   R<   t   normpathRŠ   t
   splitdriveR,   RŒ   t   joinR   RV   R1   (   R<   t   drivet   filepatht   partR9   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR   ð   s    +c         C   sI   |  j  ƒ  } | j d d ƒ \ } } | j d ƒ r7 | Sd | | f Sd S(   s6  
    Use instead of urllib.addinfourl.geturl(), which appears to have
    some issues with dropping the double slash for certain schemes
    (e.g. file://).  This implementation is probably over-eager, as it
    always restores '://' if it is missing, and it appears some url
    schemata aren't always followed by '//' after the colon, but as
    far as I know pip doesn't need any of those.
    The URI RFC can be found at: http://tools.ietf.org/html/rfc1630

    This function assumes that
        scheme:/foo/bar
    is the same as
        scheme:///foo/bar
    R$   i   s   //s   %s://%sN(   R   R,   R+   (   t   urllib2_respR9   R;   t   rest(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR   þ   s
    c         C   s0   d } t  |  ƒ d j ƒ  } | | k r, t St S(	   s9   Return True if `name` is a considered as an archive file.s   .zips   .tar.gzs   .tar.bz2s   .tgzs   .tars	   .pybundlei   (   s   .zips   .tar.gzs   .tar.bz2s   .tgzs   .tars	   .pybundle(   R	   R*   R   Rƒ   (   R…   t   archivest   ext(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR     s
    c         C   s3   t  |  ƒ } | r" | j | ƒ n | j | ƒ d  S(   N(   t   _get_used_vcs_backendt   exportt   unpack(   t   linkt   locationt   only_downloadt   vcs_backend(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR     s    c         C   sz   t  |  j ƒ } t j | ƒ d } t j j | ƒ rc t j j | ƒ rS t | ƒ n  t | | ƒ n t	 | | | |  ƒ d  S(   Ni    (
   R   R9   t	   mimetypest
   guess_typeRx   R<   t   isdirR
   R   R   (   Rš   R›   t   sourcet   content_type(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR   '  s    c         C   s=   x6 t  j D]+ } |  j | j k r
 | |  j ƒ } | Sq
 Wd  S(   N(   R   t   backendsR;   t   schemesR9   (   Rš   t   backendR   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR—   3  s    c         C   s   t  t |  ƒ ƒ S(   N(   t   boolR—   (   Rš   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR   :  s    c         C   s   |  j  j ƒ  j d ƒ S(   Ns   file:(   R9   R*   R+   (   Rš   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR    >  s    c         C   sO   |  j  ƒ  }  |  | j k rK t j d | |  | j f ƒ t d | ƒ ‚ n  d  S(   NsC   MD5 hash of the package %s (%s) doesn't match the expected hash %s!s   Bad MD5 hash for package %s(   t	   hexdigestt   md5_hashR   t   fatalR   (   t   download_hashRš   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt
   _check_md5B  s
    	c         C   sR   t  ƒ  } t |  d ƒ } x) | j d ƒ } | s4 Pn  | j | ƒ q | j ƒ  | S(   Nt   rbi   (   R    R3   R2   t   updateR8   (   t   target_fileRš   Rª   t   fpt   chunk(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt   _get_md5_from_fileJ  s    	
c   
      C   sÁ  t  | d ƒ } d  } | j r* t ƒ  } n  y t |  j ƒ  d ƒ } Wn t t f k
 rc d } n Xd } | d k pz | } | j } z| rÉ | rµ t	 j
 d | t | ƒ f ƒ qÚ t	 j
 d | ƒ n t	 j d | ƒ t	 j d	 | ƒ x  |  j d
 ƒ }	 |	 sPn  | t |	 ƒ 7} | re| s=t	 j d t | ƒ ƒ qet	 j d d | | t | ƒ f ƒ n  | j r~| j |	 ƒ n  | j |	 ƒ qî | j ƒ  Wd  | r¼t	 j d t | ƒ ƒ n  X| S(   Nt   wbs   content-lengthi    i(   iè  s   Downloading %s (%s): s   Downloading %s (unknown size): s   Downloading %ss   Downloading from URL %si   s   %ss	   %3i%%  %sid   s   %s downloadedi@œ  (   R3   RG   R¨   R    t   intt   infot
   ValueErrort   KeyErrort   show_urlR   t   start_progressR   t   notifyt   debugR2   R‡   t   show_progressR­   t   writeR8   t   end_progress(
   R=   Rš   t   temp_locationR¯   Rª   t   total_lengtht
   downloadedR»   R·   R°   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt   _download_urlV  sD    	
	 (	c         C   s%  t  } t j j | | j ƒ } t j j | ƒ râ t d t | ƒ d	 ƒ } | d k r^ t } qâ | d k r‘ t	 j
 d t | ƒ ƒ t j | ƒ qâ | d k râ t | ƒ } t	 j
 d t | ƒ t | ƒ f ƒ t j | | ƒ qâ n  | r!t j |  | ƒ t	 j d 8_ t	 j d t | ƒ ƒ n  d  S(
   Ns/   The file %s exists. (i)gnore, (w)ipe, (b)ackup t   it   wt   bs   Deleting %ss   Backing up %s to %si   s   Saved %s(   RÂ   RÃ   RÄ   (   R   Rx   R<   R   t   filenamet   existsR   R   Rƒ   R   t   warnt   removeR   t   shutilt   movet   copyt   indentR¹   (   RÅ   R›   R¢   Rš   RË   t   download_locationRM   t	   dest_file(    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt
   _copy_file  s&    		c         C   s  t  j d d ƒ } |  j j d d ƒ d } d  } d  } | r€ t j j | t j	 | d ƒ ƒ } t j j
 | ƒ s€ t | ƒ q€ n  | rt j j | ƒ rt j j | d ƒ rt | d ƒ } | j ƒ  j ƒ  }	 | j ƒ  |  j rõ t | |  ƒ } n  | }
 t j d | ƒ nØ t | |  ƒ } | j ƒ  d	 }	 |  j } t | ƒ d } | srt j |	 ƒ } | rr| | 7} qrn  | rÀ|  j t | ƒ k rÀt j j t | ƒ ƒ d } | rÀ| | 7} qÀn  t j j | | ƒ }
 t | |  |
 ƒ } |  j r t | |  ƒ n  | rt |
 | |	 |  ƒ n t |
 | |	 |  ƒ | rT| |
 k rTt  | |
 |	 ƒ n  | d  k rpt j! |
 ƒ n  t j" | ƒ d  S(
   Ns   -unpacks   pip-t   #i   i    Rd   s   .content-types   Using download cache from %ss   content-type(#   t   tempfilet   mkdtempR9   R,   RG   Rx   R<   R   R   RV   R    R   RÆ   R3   R2   t   stripR8   R¨   R±   R   R¹   t   _get_response_from_urlR´   RÅ   R	   Rž   t   guess_extensionR   RÁ   R«   RÏ   R   R   t   unlinkt   rmdir(   Rš   R›   t   download_cacheRœ   t   temp_dirRv   R®   Rª   R¯   R¢   R¾   R=   RÅ   R–   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyR!   —  sV    
			c         C   s’   y t  |  ƒ } Wn{ t j k
 rS t j ƒ  d } t j d | j | f ƒ ‚  n; t k
 r t j ƒ  d } t j d | | f ƒ ‚  n X| S(   Ni   s   HTTP error %s while getting %ss   Error %s while getting %s(	   R   R   R   R5   R6   R   R©   RI   R4   (   Rv   Rš   R=   R@   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyRÔ   Ç  s    t   Urllib2HeadRequestc           B   s   e  Z d  „  Z RS(   c         C   s   d S(   Nt   HEAD(    (   RD   (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt
   get_methodÖ  s    (   R~   R   RÜ   (    (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyRÚ   Õ  s   (B   t   reRX   R5   Rx   Rž   RÉ   RÑ   t   pip.backwardcompatR    R   R   R   R   R   R   R   t   pip.exceptionsR   t   pip.utilR	   R
   R   R   R   R   R   R   R   t   pip.vcsR   t   pip.logR   t   __all__t	   TransportR   RG   R   t   compilet   IR'   R.   t   objectRA   R   R   R   R‹   Rˆ   R   R   R   R   Rƒ   R   R   R—   R   R    R«   R±   RÁ   RÏ   R!   RÔ   RR   RÚ   (    (    (    s0   /usr/lib/python2.7/dist-packages/pip/download.pyt   <module>   sR   :@		$ƒ															+		0	