
^Vk                 @   s  d  d l  m Z d  d l m 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 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 Z d  d l m Z d  d l m Z m Z m Z d  d l m Z 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& e$ rud  d l m' Z( n d  d l m( Z( d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% g Z) e	 j* e+  Z, d{ Z- d| Z. d} Z/ d~ Z0 e/ e- e0 e. Z1 e/ e0 Z2 y d  d l3 Z3 e2 e- 7Z2 Wn e4 k
 rTe, j5 d2  Yn Xy d  d l6 Z6 e2 e. 7Z2 Wn e4 k
 re, j5 d3  Yn Xd4 d5   Z7 d6 d"   Z8 d7 d   Z9 e& d8 d9 d: d;  d< d= d   Z: d> d?   Z; d@ d   Z< dA dB d  Z= dC dD   Z> dE d   Z? dF d   Z@ dG d   ZA dH d   ZB dI d   ZC e jD dJ dK  ZE dL d   ZF dM d   ZG dN dO d  ZH dP d   ZI dQ d   ZJ dR dS   ZK dT dU   ZL dV dW   ZM dX dY   ZN dZ d[   ZO dN e dN d< d< d\ d]  ZP d^ d_   ZQ d` da   ZR db d   ZS dc dd   ZT dN de d  ZU df d   ZV dg d   ZW dh d!   ZX dN d di e	 jY d d d dj d  ZZ dk dl   Z[ dm dn   Z\ Gdo dp   dp e]  Z^ Gdq dr   dr e(  Z_ e j` ds dt    Za du d    Zb Gdv dw   dw e]  Zc dx d%   Zd dy dz   Ze d S)    )absolute_import)dequeN)InstallationError)console_to_str
expanduserstdlib_pkgs)site_packages	user_siterunning_under_virtualenvvirtualenv_no_globalwrite_delete_marker_file)pkg_resources)input)PY2)retry)BytesIO)StringIOrmtreedisplay_path
backup_dirasksplitextformat_sizeis_installable_diris_svn_pagefile_contentssplit_leading_dirhas_leading_dirnormalize_pathrenamesget_terminal_sizeget_prog
unzip_file
untar_fileunpack_filecall_subprocesscaptured_stdoutremove_tracebacks
ensure_dirARCHIVE_EXTENSIONSSUPPORTED_EXTENSIONSget_installed_version.tar.bz2.tbz.tar.xz.txz.tlz.tar.lz	.tar.lzma.zip.whl.tar.gz.tgz.tarzbz2 module is not availablezlzma module is not availablec             O   s6   y t  |   SWn! t k
 r1 | | |    Yn Xd  S)N)
__import__ImportError)pkg_or_module_stringExceptionTypeargskwargs r>   1/tmp/pip-build-9m6vxulb/pip/pip/utils/__init__.pyimport_or_raiseI   s    r@   c             C   sU   y t  j |   Wn= t k
 rP } z | j t j k r>   n  WYd d } ~ Xn Xd S)z os.path.makedirs without EEXIST.N)osmakedirsOSErrorerrnoEEXIST)pather>   r>   r?   r(   P   s
    c               C   sP   y. t  j j t j d  d k r- d t j SWn t t t f k
 rK Yn Xd S)Nr   __main__.py-cz	%s -m pippip)rH   rI   )	rA   rF   basenamesysargv
executableAttributeError	TypeError
IndexErrorr>   r>   r>   r?   r!   Y   s    stop_max_delayi  
wait_fixedi  Fc             C   s   t  j |  d | d t d  S)Nignore_errorsonerror)shutilr   rmtree_errorhandler)dirrT   r>   r>   r?   r   c   s    c             C   sA   t  j |  j t j @r: t  j | t j  |  |  d S  d S)zOn Windows, the files in .svn are read-only, so when rmtree() tries to
    remove them, an exception is thrown.  We catch that here, remove the
    read-only attribute, and hopefully continue without problems.N)rA   statst_modeS_IREADchmodS_IWRITE)funcrF   exc_infor>   r>   r?   rW   i   s
    
rW   c             C   s   t  j j t  j j |    }  t j d d k rd |  j t j   d  }  |  j t j	   d  }  n  |  j
 t  j   t  j j  r d |  t t  j    d  }  n  |  S)zTGives the display value for a given path, making it relative to cwd
    if possible.r      replace.N)rA   rF   normcaseabspathrL   version_infodecodegetfilesystemencodingencodegetdefaultencoding
startswithgetcwdseplen)rF   r>   r>   r?   r   x   s    #z.bakc             C   sK   d } | } x4 t  j j |  |  rB | d 7} | t |  } q W|  | S)z\Figure out the name of a directory to back up the given dir to
    (adding .bak, .bak2, etc)   )rA   rF   existsstr)rX   extn	extensionr>   r>   r?   r      s    
c             C   sC   x3 t  j j d d  j   D] } | | k r | Sq Wt |  |  S)NPIP_EXISTS_ACTION )rA   environgetsplitr   )messageoptionsactionr>   r>   r?   ask_path_exists   s    "r|   c             C   s~   xw t  j j d  r( t d |    n  t |   } | j   j   } | | k rr t d | d j |  f  q | Sq Wd S)z@Ask the message interactively, with the given possible responsesPIP_NO_INPUTz7No input was expected ($PIP_NO_INPUT set); question: %sz<Your response (%r) was not one of the expected responses: %sz, N)	rA   rv   rw   	Exceptionr   striplowerprintjoin)ry   rz   responser>   r>   r?   r      s    c             C   sX   |  d k r d |  d d S|  d	 k r4 d |  d S|  d k rL d |  d Sd |  Sd  S)
Ni  z%.1fMBg     @@
   z%ikBz%.1fkBz%ibytesi@B i'  r>   )bytesr>   r>   r?   r      s    c             C   sE   t  j j |   s d St  j j |  d  } t  j j |  rA d Sd S)z@Return True if `path` is a directory containing a setup.py file.Fzsetup.pyT)rA   rF   isdirr   isfile)rF   setup_pyr>   r>   r?   r      s    c             C   s(   t  j d |   o' t  j d |  t  j  S)zT
    Returns true if the page appears to be the index page of an svn repository
    z<title>[^<]*Revision \d+:z#Powered by (?:<a[^>]*?>)?Subversion)researchI)htmlr>   r>   r?   r      s    c          	   C   s/   t  |  d   } | j   j d  SWd  QXd  S)Nrbzutf-8)openreadrf   )filenamefpr>   r>   r?   r      s    c             c   s)   x" |  j  |  } | s Pn  | Vq Wd S)z7Yield pieces of data from a file-like object until EOF.N)r   )filesizechunkr>   r>   r?   read_chunks   s
    r   c             C   s   |  j  d  j  d  }  d |  k rj d |  k rN |  j d  |  j d  k  sZ d |  k rj |  j d d  Sd |  k r |  j d d  S|  d f Sd  S)N/\rn   ru   )lstripfindrx   )rF   r>   r>   r?   r      s    6c             C   s\   d } xO |  D]G } t  |  \ } } | s/ d S| d k rD | } q | | k r d Sq Wd S)zyReturns true if all the paths have the same leading path name
    (i.e., everything is in one subdirectory in an archive)NFT)r   )pathscommon_prefixrF   prefixrestr>   r>   r?   r      s    	Tc             C   sI   t  |   }  | r' t j j |   }  n t j j |   }  t j j |   S)zN
    Convert a path to its canonical, case-normalized, absolute version.

    )r   rA   rF   realpathrd   rc   )rF   resolve_symlinksr>   r>   r?   r      s
    c             C   s[   t  j |   \ } } | j   j d  rQ | d d  | } | d d  } n  | | f S)z,Like os.path.splitext, but take off .tar tooz.tar   Nr   )	posixpathr   r   endswith)rF   baserq   r>   r>   r?   r      s
    c             C   s   t  j j |  \ } } | rG | rG t  j j |  rG t  j |  n  t j |  |  t  j j |   \ } } | r | r y t  j |  Wq t k
 r Yq Xn  d S)z7Like os.renames(), but handles renaming across devices.N)	rA   rF   rx   ro   rB   rV   move
removedirsrC   )oldnewheadtailr>   r>   r?   r     s    c             C   s)   t    s d St |   j t t j   S)z
    Return True if path is within sys.prefix, if we're running in a virtualenv.

    If we're not in a virtualenv, all paths are considered "local."

    T)r
   r   rj   rL   r   )rF   r>   r>   r?   is_local  s    	r   c             C   s   t  t |    S)z
    Return True if given Distribution object is installed locally
    (i.e. within current virtualenv).

    Always True if we're not in a virtualenv.

    )r   dist_location)distr>   r>   r?   dist_is_local!  s    r   c             C   s%   t  t |    } | j t  t   S)zF
    Return True if given Distribution is installed in user site.
    )r   r   rj   r	   )r   	norm_pathr>   r>   r?   dist_in_usersite,  s    r   c             C   s   t  t |    j t  t   S)ze
    Return True if given Distribution is installed in
    distutils.sysconfig.get_python_lib().
    )r   r   rj   r   )r   r>   r>   r?   dist_in_site_packages4  s    r   c             C   sJ   xC t  j D]8 } t j j | |  j d  } t j j |  r
 d Sq
 Wd S)z$Is distribution an editable install?z	.egg-linkTF)rL   rF   rA   r   project_namer   )r   	path_itemegg_linkr>   r>   r?   dist_is_editable>  s
    r   c                s   |  r t   n d d    | r0 d d     n d d     | rQ d d    n d d    | rl t  n d	 d
          f d d   t j D S)a  
    Return a list of installed Distribution objects.

    If ``local_only`` is True (default), only return installations
    local to the current virtualenv, if in a virtualenv.

    ``skip`` argument is an iterable of lower-case project names to
    ignore; defaults to stdlib_pkgs

    If ``editables`` is False, don't report editables.

    If ``editables_only`` is True , only report editables.

    If ``user_only`` is True , only report installations in the user
    site directory.

    c             S   s   d S)NTr>   )dr>   r>   r?   
local_test`  s    z/get_installed_distributions.<locals>.local_testc             S   s   d S)NTr>   )r   r>   r>   r?   editable_testd  s    z2get_installed_distributions.<locals>.editable_testc             S   s   t  |   S)N)r   )r   r>   r>   r?   r   g  s    c             S   s
   t  |   S)N)r   )r   r>   r>   r?   editables_only_testk  s    z8get_installed_distributions.<locals>.editables_only_testc             S   s   d S)NTr>   )r   r>   r>   r?   r   n  s    c             S   s   d S)NTr>   )r   r>   r>   r?   	user_testt  s    z.get_installed_distributions.<locals>.user_testc                sU   g  |  ]K }  |  r | j   k r   |  r  |  r  |  r |  q Sr>   )key).0r   )r   r   r   skipr   r>   r?   
<listcomp>w  s   	 z/get_installed_distributions.<locals>.<listcomp>)r   r   r   working_set)
local_onlyr   include_editableseditables_only	user_onlyr>   )r   r   r   r   r   r?   get_installed_distributionsG  s    		r   c             C   s   g  } t    rN t   r( | j t  qq | j t  t rq | j t  qq n# t rd | j t  n  | j t  x@ | D]8 } t j j | |  j  d } t j j	 |  rx | Sqx Wd S)a  
    Return the path for the .egg-link file if it exists, otherwise, None.

    There's 3 scenarios:
    1) not in a virtualenv
       try to find in site.USER_SITE, then site_packages
    2) in a no-global virtualenv
       try to find in site_packages
    3) in a yes-global virtualenv
       try to find in site_packages, then site.USER_SITE
       (don't look in global location)

    For #1 and #3, there could be odd cases, where there's an egg-link in 2
    locations.

    This method will just return the first one found.
    z	.egg-linkN)
r
   r   appendr   r	   rA   rF   r   r   r   )r   sitessiteegglinkr>   r>   r?   egg_link_path  s    		r   c             C   s   t  |   } | r | S|  j S)z
    Get the site-packages location of this distribution. Generally
    this is dist.location, except in the case of develop-installed
    packages, where dist.location is the source code location, and we
    want to know where the egg-link file is.

    )r   location)r   r   r>   r>   r?   r     s    r   c           
   C   s   d d   }  |  d  p- |  d  p- |  d  } | s| y8 t  j t  j   t  j  } |  |  } t  j |  Wq| Yq| Xn  | s t  j j d d  t  j j d d	  f } n  t | d  t | d  f S)
zlReturns a tuple (x, y) representing the width(x) and the height(x)
    in characters of the terminal window.c             S   so   yL d d  l  } d d  l } d d  l } | j d | j |  | j d   } Wn d  SYn X| d k rk d  S| S)Nr   hh1234)r   r   )fcntltermiosstructunpackioctl
TIOCGWINSZ)fdr   r   r   crr>   r>   r?   ioctl_GWINSZ  s    	z'get_terminal_size.<locals>.ioctl_GWINSZr   rn   r`   LINES   COLUMNSP   )rA   r   ctermidO_RDONLYcloserv   rw   int)r   r   r   r>   r>   r?   r      s    $-c              C   s    t  j d  }  t  j |   |  S)zBGet the current umask which involves having to set it temporarily.r   )rA   umask)maskr>   r>   r?   current_umask  s    r   c             C   s~  t  |  t |  d  } zSt j | d d } t | j    oF | } x| j   D]} | j } | j |  } | }	 | r t	 |  d }	 n  t
 j j | |	  }	 t
 j j |	  }
 |	 j d  s |	 j d  r t  |	  qV t  |
  t |	 d  } z | j |  Wd | j   | j d	 ?} | rft j |  rf| d
 @rft
 j |	 d t   d
 B n  XqV WWd | j   Xd S)a  
    Unzip the file (with path `filename`) to the destination `location`.  All
    files are written based on system defaults and umask (i.e. permissions are
    not preserved), except that regular file members with any execute
    permissions (user, group, or world) have "chmod +x" applied after being
    written. Note that for windows, any execute changes using os.chmod are
    no-ops per the python docs.
    r   
allowZip64Trn   r   r   wbN   I   i  )r(   r   zipfileZipFiler   namelistinfolistr   r   r   rA   rF   r   dirnamer   writer   external_attrrY   S_ISREGr\   r   )r   r   flattenzipfpzipleadinginfonamedatafnrX   r   moder>   r>   r?   r"     s0    	
	

'c             C   s  t  |  |  j   j d  s4 |  j   j d  r= d } np |  j   j t  r[ d } nR |  j   j t  ry d } n4 |  j   j d  r d } n t j d |   d	 } t j |  |  } zt	 d
 d   | j
   D  } x| j
   D]} | j } | d k rq n  | r(t |  d } n  t j j | |  } | j   rVt  |  q | j   ry | j | |  Wqt k
 r} z! t j d |  | j |  w WYd d } ~ XqXq y | j |  }	 WnG t t f k
 r} z! t j d |  | j |  w WYd d } ~ Xn Xt  t j j |   t | d   }
 t j |	 |
  Wd QX|	 j   | j | |  | j d @r t j | d t   d B q q WWd | j   Xd S)a  
    Untar the file (with path `filename`) to the destination `location`.
    All files are written based on system defaults and umask (i.e. permissions
    are not preserved), except that regular file members with any execute
    permissions (user, group, or world) have "chmod +x" applied after being
    written.  Note that for windows, any execute changes using os.chmod are
    no-ops per the python docs.
    z.gzz.tgzzr:gzzr:bz2zr:xzz.tarrz-Cannot determine compression type for file %szr:*c             S   s(   g  |  ] } | j  d  k r | j   q S)pax_global_header)r   )r   memberr>   r>   r?   r     s   	 zuntar_file.<locals>.<listcomp>r   rn   z/In the tar file %s the member %s is invalid: %sNr   r   i  )r(   r   r   BZ2_EXTENSIONSXZ_EXTENSIONSloggerwarningtarfiler   r   
getmembersr   r   rA   rF   r   r   issym_extract_memberr~   extractfileKeyErrorrO   r   rV   copyfileobjr   utimer   r\   r   )r   r   r   tarr   r   r   rF   excr   destfpr>   r>   r?   r#     s`    	
*				
	
&c             C   s(  t  j j |   }  | d k sB |  j   j t  sB t j |   rb t |  | d |  j d  n | d k s t	 j
 |   s |  j   j t t t  r t |  |  nz | r | j d  r t t |    r d d l m } | d | j  j |  n& t j d	 |  | |  t d
 |   d  S)Nzapplication/zipr   z.whlzapplication/x-gzipz	text/htmlr   )
Subversionzsvn+zZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz%Cannot determine archive format of %s)rA   rF   r   r   r   ZIP_EXTENSIONSr   
is_zipfiler"   r   
is_tarfileTAR_EXTENSIONSr   r   r#   rj   r   r   pip.vcs.subversionr  urlr   r   criticalr   )r   r   content_typelinkr  r>   r>   r?   r$   O  s,    c             C   s8   d } t  j | d |   }  t r% |  St  j d d |   S)NzR(?:\W+File "(?:.*)", line (?:.*)\W+(?:.*)\W+\^\W+)?Syntax(?:Error|Warning): (?:.*)ru   z\*\*\* Error compiling (?:.*))r   subr   )outputpatternr>   r>   r?   r'   p  s
    raisec             C   s  | r d  } n	 t  j } | d  k r g  }	 xd |  D]\ }
 d |
 k sg d |
 k sg d |
 k sg d |
 k r d |
 j d d  }
 n  |	 j |
  q1 Wd j |	  } n  t j | d |  t j j	   } | r | j
 |  n  y4 t  j |  d t  j d	 d  d
 | d | d | } Wn; t k
 rL} z t j d | |    WYd  d  } ~ Xn X| d  k	 rg  } x t | j j    } | sPn  | j   } | j | d  t j   t j k rt j |  qb| d  k	 rb| j   qbqbWn  | j   | d  k	 r"| j r| j d  q"| j d  n  | j r| d k rt j   t j k r| rt j d |  t j d j |  d  n  t d | | j | f   q| d k rt j d | | j |  q| d k rqt d t |    n  | st  d j |   Sd  S)N 
"'z"%s"z\"zRunning command %sstderrstdinstdoutcwdenvz#Error %s while executing command %serrordoner  z Complete output from command %s:ru   z)
----------------------------------------z,Command "%s" failed with error code %s in %swarnz$Command "%s" had error code %s in %signorezInvalid value: on_returncode=%s)!
subprocessPIPEra   r   r   r   logrA   rv   copyupdatePopenSTDOUTr~   r  r   r  readlinerstripgetEffectiveLevelstd_loggingDEBUGdebugspinwait
returncodefinishr   r   r   
ValueErrorreprr'   )cmdshow_stdoutr  on_returncodecommand_levelcommand_descextra_environspinnerr  	cmd_partspartr  procr  
all_outputliner>   r>   r?   r%   {  sz    		0
		
c             C   s   t  |  d   } | j   } Wd QXd t j d  d g } x: | D]2 } y | j |  } Wn t k
 rs wC Yn XPqC Wt |  t k s t  | S)aR  Return the contents of *filename*.

    Try to decode the file contents with utf-8, the preferred system encoding
    (e.g., cp1252 on some Windows machines), and latin1, in that order.
    Decoding a byte string with latin1 will never raise an error. In the worst
    case, the returned string will contain some garbage characters.

    r   Nzutf-8Flatin1)	r   r   localegetpreferredencodingrf   UnicodeDecodeErrortyper   AssertionError)r   r   r   	encodingsencr>   r>   r?   read_text_file  s    	rJ  c             C   s   t  j |   t |   d  S)N)rA   rB   r   )	build_dirr>   r>   r?   _make_build_dir  s    rL  c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	FakeFilezQWrap a list of lines in an object with readline() to make
    ConfigParser happy.c             C   s   d d   | D |  _  d  S)Nc             s   s   |  ] } | Vq d  S)Nr>   )r   lr>   r>   r?   	<genexpr>  s    z$FakeFile.__init__.<locals>.<genexpr>)_gen)selflinesr>   r>   r?   __init__  s    zFakeFile.__init__c             C   sT   y7 y t  |  j  SWn t k
 r5 |  j j    SYn XWn t k
 rO d SYn Xd  S)Nru   )nextrP  	NameErrorStopIteration)rQ  r>   r>   r?   r*     s    zFakeFile.readlinec             C   s   |  j  S)N)rP  )rQ  r>   r>   r?   __iter__	  s    zFakeFile.__iter__N)__name__
__module____qualname____doc__rS  r*  rW  r>   r>   r>   r?   rM    s   	rM  c               @   s4   e  Z d  Z e d d    Z e d d    Z d S)StreamWrapperc             C   s   | |  _  |    S)N)orig_stream)clsr]  r>   r>   r?   from_stream  s    	zStreamWrapper.from_streamc             C   s
   |  j  j S)N)r]  encoding)rQ  r>   r>   r?   r`    s    zStreamWrapper.encodingN)rX  rY  rZ  classmethodr_  propertyr`  r>   r>   r>   r?   r\    s   r\  c             c   sR   t  t |   } t t |  t j |   z t  t |   VWd t t |  |  Xd S)zReturn a context manager used by captured_stdout/stdin/stderr
    that temporarily replaces the sys stream *stream_name* with a StringIO.

    Taken from Lib/support/__init__.py in the CPython repo.
    N)getattrrL   setattrr\  r_  )stream_nameorig_stdoutr>   r>   r?   captured_output  s
    rg  c               C   s
   t  d  S)zCapture the output of sys.stdout:

       with captured_stdout() as stdout:
           print('hello')
       self.assertEqual(stdout.getvalue(), 'hello
')

    Taken from Lib/support/__init__.py in the CPython repo.
    r  )rg  r>   r>   r>   r?   r&   )  s    	c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)cached_propertyzA property that is only computed once per instance and then replaces
       itself with an ordinary attribute. Deleting the attribute resets the
       property.

       Source: https://github.com/bottlepy/bottle/blob/0.11.5/bottle.py#L175
    c             C   s   t  | d  |  _ | |  _ d  S)Nr[  )rc  r[  r^   )rQ  r^   r>   r>   r?   rS  =  s    zcached_property.__init__c             C   s4   | d  k r |  S|  j  |  } | j |  j  j <| S)N)r^   __dict__rX  )rQ  objr^  valuer>   r>   r?   __get__A  s     zcached_property.__get__N)rX  rY  rZ  r[  rS  rl  r>   r>   r>   r?   rh  5  s   rh  c             C   s>   t  j j |   } t  j   } | j |  } | r: | j Sd S)zCGet the installed version of dist_name avoiding pkg_resources cacheN)r   Requirementparse
WorkingSetr   version)	dist_namereqr   r   r>   r>   r?   r+   I  s    c             C   s   t  |  d d d S)zConsume an iterable at C speed.maxlenr   N)r   )iteratorr>   r>   r?   consumeZ  s    ru  )r,   r-   )r.   r/   r0   r1   r2   )r3   r4   )r5   r6   r7   )f
__future__r   collectionsr   
contextlibrD   iorC  loggingr-  r   rA   r   rV   rY   r#  rL   r   r   pip.exceptionsr   
pip.compatr   r   r   pip.locationsr   r	   r
   r   r   pip._vendorr   pip._vendor.six.movesr   pip._vendor.sixr   pip._vendor.retryingr   r   r   __all__	getLoggerrX  r   r   r   r	  r  r)   r*   bz2r9   r/  lzmar@   r(   r!   r   rW   r   r   r|   r   r   r   r   r   DEFAULT_BUFFER_SIZEr   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r"   r#   r$   r'   r.  r%   rJ  rL  objectrM  r\  contextmanagerrg  r&   rh  r+   ru  r>   r>   r>   r?   <module>   s   (				
	

		
	5%+L!	_