
'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(/   iN(   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   sz   |  j  d  s t d |   |  t d  j d  } t j |  } t j |  rl | 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	