B
    Uvgw                 @   s  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 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  d d
l!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+ er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%gZ.e/e0Z1d&Z2d'Z3d(Z4d)Z5e4e2 e5 e3 Z6e4e5 Z7yd dl8Z8e7e27 Z7W n  e9k
r   e1:d* Y nX yd dl;Z;e7e37 Z7W n  e9k
r   e1:d+ Y nX d,d- Z<d.d! Z=d/d Z>ed0d1d2dyd4dZ?d5d6 Z@d7d ZAdzd9dZBd:d; ZCd<d ZDd=d ZEd>d ZFd?d ZGd@d ZHejIfdAdBZJdCd ZKdDd ZLd{dFdZMdGd ZNdHd ZOdIdJ ZPdKdL ZQdMdN ZRdOdP ZSdQdR ZTdEe+dEd3d3fdSdTZUdUdV ZVdWdX ZWdYdZ ZXd|d[dZYd\d ZZd]d Z[d}d_dZ\d`da Z]dbdc Z^G ddde dee_Z`G dfdg dge-Zaejbdhdi Zcdjd  ZdG dkdl dle_Zed~dmd$Zfdndo Zgdpdq ZhddrdsZidtdu Zjdvd% Zkdwdx ZldS )    )absolute_importN)deque)pkg_resources)retry)PY2)input)parse)CommandErrorInstallationError)running_under_virtualenvsite_packages	user_sitevirtualenv_no_globalwrite_delete_marker_file)WINDOWSconsole_to_str
expanduserstdlib_pkgs)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_prog
unzip_file
untar_fileunpack_filecall_subprocesscaptured_stdout
ensure_dirARCHIVE_EXTENSIONSSUPPORTED_EXTENSIONSget_installed_versionremove_auth_from_url)z.tar.bz2z.tbz)z.tar.xzz.txzz.tlzz.tar.lzz	.tar.lzma)z.zipz.whl)z.tar.gzz.tgzz.tarzbz2 module is not availablezlzma module is not availablec             O   s,   yt | S  tk
r&   |||Y nX d S )N)
__import__ImportError)pkg_or_module_stringExceptionTypeargskwargs r4   9/tmp/pip-install-r_9ig3yj/pip/pip/_internal/utils/misc.pyimport_or_raiseO   s    r6   c          
   C   sD   yt |  W n0 tk
r> } z|jtjkr. W dd}~X Y nX dS )z os.path.makedirs without EEXIST.N)osmakedirsOSErrorerrnoEEXIST)pather4   r4   r5   r)   V   s
    c           
   C   sL   y,t jtjd } | dkr&dtj S | S W n tttfk
rF   Y nX dS )Nr   )z__main__.pyz-cz	%s -m pippip)	r7   r<   basenamesysargv
executableAttributeError	TypeError
IndexError)progr4   r4   r5   r#   _   s    
i  i  )stop_max_delay
wait_fixedFc             C   s   t j| |td d S )N)ignore_errorsonerror)shutilr   rmtree_errorhandler)dirrI   r4   r4   r5   r   l   s    c             C   s2   t |jtj@ r,t |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)r7   statst_modeS_IREADchmodS_IWRITE)funcr<   exc_infor4   r4   r5   rL   r   s
    rL   c             C   st   t jt j| } tjd dkrB| t d} | t	 d} | 
t  t jj rpd| tt  d  } | S )zTGives the display value for a given path, making it relative to cwd
    if possible.r      replace.N)r7   r<   normcaseabspathr@   version_infodecodegetfilesystemencodingencodegetdefaultencoding
startswithgetcwdseplen)r<   r4   r4   r5   r      s    .bakc             C   s:   d}|}x(t j| | r0|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)   )r7   r<   existsstr)rM   extn	extensionr4   r4   r5   r      s    c             C   s2   x&t jdd D ]}||kr|S qW t| |S )NPIP_EXISTS_ACTION )r7   environgetsplitr   )messageoptionsactionr4   r4   r5   ask_path_exists   s    rr   c             C   sZ   xTt jdrtd|  t| }|  }||krNtd|d|f  q|S qW d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)	r7   rl   rm   	Exceptionr   striplowerprintjoin)ro   rp   responser4   r4   r5   r      s    c             C   sL   | dkrd| d d  S | dkr,d| d  S | dkr@d| d  S d|  S d S )	Ni@B z%.1fMBg     @@i  i'  z%ikBz%.1fkBz%ibytesr4   )bytesr4   r4   r5   r      s    c             C   sP   t j| sdS t j| d}t j|r.dS t j| d}t j|rLdS dS )zBIs path is a directory containing setup.py or pyproject.toml?
    Fzsetup.pyTzpyproject.toml)r7   r<   isdirrx   isfile)r<   setup_pypyproject_tomlr4   r4   r5   r      s    c             C   s   t d| ot 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)htmlr4   r4   r5   r      s    c          	   C   s$   t | d}| dS Q R X d S )Nrbzutf-8)openreadr[   )filenamefpr4   r4   r5   r      s    c             c   s    x|  |}|sP |V  qW dS )z7Yield pieces of data from a file-like object until EOF.N)r   )filesizechunkr4   r4   r5   read_chunks   s
    
r   c             C   sh   |  d d} d| krHd| kr4| d| dk s<d| krH| ddS d| kr\| ddS | dfS d S )N/\rd   rk   )lstripfindrn   )r<   r4   r4   r5   r      s    $c             C   sD   d}x:| D ]2}t |\}}|s"dS |dkr0|}q
||kr
dS q
W d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_prefixr<   prefixrestr4   r4   r5   r       s    
Tc             C   s2   t | } |rtj| } ntj| } tj| S )zN
    Convert a path to its canonical, case-normalized, absolute version.

    )r   r7   r<   realpathrY   rX   )r<   resolve_symlinksr4   r4   r5   r!      s
    c             C   s@   t | \}}| dr8|dd | }|dd }||fS )z,Like os.path.splitext, but take off .tar tooz.tarN)	posixpathr   rv   endswith)r<   baserg   r4   r4   r5   r     s
    c             C   sz   t j|\}}|r.|r.t j|s.t | t| | t j| \}}|rv|rvyt | W n tk
rt   Y nX dS )z7Like os.renames(), but handles renaming across devices.N)	r7   r<   rn   re   r8   rK   move
removedirsr9   )oldnewheadtailr4   r4   r5   r"     s    
c             C   sr   t | } t tj}t r(| t tjS ddlm} | |rjx&|d D ]}| t |rLdS qLW dS dS dS )aB  
    Return True if this is a path pip is allowed to modify.

    If we're in a virtualenv, sys.prefix points to the virtualenv's
    prefix; only sys.prefix is considered local.

    If we're not in a virtualenv, in general we can modify anything.
    However, if the OS vendor has configured distutils to install
    somewhere other than sys.prefix (which could be a subdirectory of
    sys.prefix, e.g. /usr/local), we consider sys.prefix itself nonlocal
    and the domain of the OS vendor. (In other words, everything _other
    than_ sys.prefix is considered local.)

    r   )distutils_schemerk   TFN)r!   r@   r   r   r_   pip._internal.locationsr   values)r<   r   r   
local_pathr4   r4   r5   is_local"  s    

r   c             C   s   t t| S )zh
    Return True if given Distribution object is installed somewhere pip
    is allowed to modify.

    )r   dist_location)distr4   r4   r5   dist_is_localB  s    r   c             C   s   t t| }|t tS )zF
    Return True if given Distribution is installed in user site.
    )r!   r   r_   r   )r   	norm_pathr4   r4   r5   dist_in_usersiteK  s    r   c             C   s   t t| t tS )z[
    Return True if given Distribution is installed in
    sysconfig.get_python_lib().
    )r!   r   r_   r   )r   r4   r4   r5   dist_in_site_packagesS  s    
r   c             C   s8   x2t jD ](}tj|| jd }tj|rdS qW dS )z$Is distribution an editable install?z	.egg-linkTF)r@   r<   r7   rx   project_namer|   )r   	path_itemegg_linkr4   r4   r5   dist_is_editable]  s
    r   c                sl   | r
t ndd |r dd  ndd  |r6dd ndd |rHt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 ``include_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 )NTr4   )dr4   r4   r5   
local_test  s    z/get_installed_distributions.<locals>.local_testc             S   s   dS )NTr4   )r   r4   r4   r5   editable_test  s    z2get_installed_distributions.<locals>.editable_testc             S   s
   t |  S )N)r   )r   r4   r4   r5   r     s    c             S   s   t | S )N)r   )r   r4   r4   r5   editables_only_test  s    z8get_installed_distributions.<locals>.editables_only_testc             S   s   dS )NTr4   )r   r4   r4   r5   r     s    c             S   s   dS )NTr4   )r   r4   r4   r5   	user_test  s    z.get_installed_distributions.<locals>.user_testc                s:   g | ]2}|r|j kr |r|r|r|qS r4   )key).0r   )r   r   r   skipr   r4   r5   
<listcomp>  s    
z/get_installed_distributions.<locals>.<listcomp>)r   r   r   working_set)
local_onlyr   include_editableseditables_only	user_onlyr4   )r   r   r   r   r   r5   get_installed_distributionsf  s    

r   c             C   s   g }t  r6t r|t qN|t trN|t ntrD|t |t x0|D ](}tj|| jd }tj	|rT|S qTW d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   r7   r<   rx   r   r|   )r   sitessiteegglinkr4   r4   r5   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   r4   r4   r5   r     s    r   c              C   s   t d} t |  | S )zBGet the current umask which involves having to set it temporarily.r   )r7   umask)maskr4   r4   r5   current_umask  s    

r   c             C   s  t | t| d}ztj|dd}t| o0|}x| D ]}|j}||}|}	|rdt	|d }	t
j||	}	t
j|	}
|	ds|	drt |	 q<t |
 t|	d}z|| W d|  |jd	? }|rt|r|d
@ rt
|	dt  d
B  X q<W W d|  X d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   T)
allowZip64rd   r   r   wbN   I   i  )r)   r   zipfileZipFiler    namelistinfolistr   r   r   r7   r<   rx   dirnamer   writecloseexternal_attrrN   S_ISREGrQ   r   )r   r   flattenzipfpzipleadinginfonamedatafnrM   r   moder4   r4   r5   r$     s0    	




 c             C   s  t | |  ds$|  dr*d}nL|  tr>d}n8|  trRd}n$|  drfd}ntd|  d	}t| |}zxt	d
d |
 D }xZ|
 D ]L}|j}|dkrq|rt|d }tj||}| rt | q| rDy||| W n8 tk
r@ } ztd| |j| wW dd}~X Y nX qy||}	W n< ttfk
r } ztd| |j| wW dd}~X Y nX t tj| t|d}
t|	|
 W dQ R X |	  ||| |jd@ rt|dt  dB  qW W d|  X d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   memberr4   r4   r5   r     s    zuntar_file.<locals>.<listcomp>r   rd   z/In the tar file %s the member %s is invalid: %sNr   r   i  )r)   rv   r   BZ2_EXTENSIONSXZ_EXTENSIONSloggerwarningtarfiler   r    
getmembersr   r   r7   r<   rx   r{   issym_extract_memberrt   extractfileKeyErrorrC   r   rK   copyfileobjr   utimer   rQ   r   )r   r   r   tarr   r   r   r<   excr   destfpr4   r4   r5   r%     s`    	


c             C   s   t j| } |dks,|  ts,t| rDt| || d d n|dkslt	
| sl|  tt t rxt| | nX|r|drtt| rddlm} |d|j | ntd	| || td
| d S )Nzapplication/zipz.whl)r   z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)r7   r<   r   rv   r   ZIP_EXTENSIONSr   
is_zipfiler$   r   
is_tarfileTAR_EXTENSIONSr   r   r%   r_   r   r   pip._internal.vcs.subversionr   urlunpackr   criticalr
   )r   r   content_typelinkr   r4   r4   r5   r&   P  s,    


raisec          
   C   s|  |dkrg }|rd}nt j}|dkrzg }	xF| D ]>}
d|
ksRd|
ksRd|
ksRd|
krbd|
dd }
|	|
 q.W d|	}td| tj	 }|r|
| x|D ]}||d qW y(t j| t jt j|||d	}|j  W n4 tk
r } ztd
||  W dd}~X Y nX g }|dk	rx`t|j }|s<P | }||d  t tjkrnt| n|dk	r&|  q&W z|  W d|jr|j  X |dk	r|jr|d n
|d |jrh|dkr.t tjkr|std| td|d  td||j|f n:|dkrLtd||j| n|dkrXnt dt!| |sxd|S dS )z
    Args:
      unset_environ: an iterable of environment variable names to unset
        prior to calling subprocess.Popen().
    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:rk   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PIPErV   r   rx   r   debugr7   rl   copyupdatepopPopenSTDOUTr   r   rt   r   r   r  readlinerstripgetEffectiveLevelstd_loggingDEBUGspinwait
returncodefinishr   r
   r   
ValueErrorrepr)cmdshow_stdoutr  on_returncodecommand_descextra_environunset_environspinnerr  	cmd_partspartr  r   procr   
all_outputliner4   r4   r5   r'   q  s    	
 












c          	   C   sx   t | d}| }W dQ R X dtddg}x4|D ],}y||}W n tk
r\   w4Y nX P q4W t|tkstt|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getpreferredencodingr[   UnicodeDecodeErrortyperz   AssertionError)r   r   r   	encodingsencr4   r4   r5   read_text_file  s    	
r/  c             C   s   t |  t|  d S )N)r7   r8   r   )	build_dirr4   r4   r5   _make_build_dir  s    
r1  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 )Nr4   )r   lr4   r4   r5   	<genexpr>  s    z$FakeFile.__init__.<locals>.<genexpr>)_gen)selflinesr4   r4   r5   __init__  s    zFakeFile.__init__c             C   sD   y*y
t | jS  tk
r&   | j  S X W n tk
r>   dS X d S )Nrk   )nextr5  	NameErrorStopIteration)r6  r4   r4   r5   r    s    
zFakeFile.readlinec             C   s   | j S )N)r5  )r6  r4   r4   r5   __iter__  s    zFakeFile.__iter__N)__name__
__module____qualname____doc__r8  r  r<  r4   r4   r4   r5   r2    s   	r2  c               @   s$   e Zd Zedd Zedd ZdS )StreamWrapperc             C   s   || _ |  S )N)orig_stream)clsrB  r4   r4   r5   from_stream  s    zStreamWrapper.from_streamc             C   s   | j jS )N)rB  encoding)r6  r4   r4   r5   rE    s    zStreamWrapper.encodingN)r=  r>  r?  classmethodrD  propertyrE  r4   r4   r4   r5   rA    s   rA  c          
   c   s@   t t| }tt| t| zt t| V  W dtt| | X d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)getattrr@   setattrrA  rD  )stream_nameorig_stdoutr4   r4   r5   captured_output  s
    
rL  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  )rL  r4   r4   r4   r5   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@  )rH  r@  rS   )r6  rS   r4   r4   r5   r8  B  s    zcached_property.__init__c             C   s(   |d kr| S |  | }|j| j j< |S )N)rS   __dict__r=  )r6  objrC  valuer4   r4   r5   __get__F  s    zcached_property.__get__N)r=  r>  r?  r@  r8  rQ  r4   r4   r4   r5   rM  :  s   rM  c             C   s4   t j| }|dkrt  }||}|r0|jS dS )zCGet the installed version of dist_name avoiding pkg_resources cacheN)r   Requirementr   
WorkingSetr   version)	dist_namer   reqr   r4   r4   r5   r,   N  s
    
c             C   s   t | dd dS )zConsume an iterable at C speed.r   )maxlenN)r   )iteratorr4   r4   r5   consume`  s    rY  c              O   s@   t t| tt| f|}dd | D }||d< tdd|S )Nc             S   s   i | ]\}}||qS r4   r4   )r   r   rP  r4   r4   r5   
<dictcomp>h  s    zenum.<locals>.<dictcomp>reverse_mappingEnumr4   )dictr   rangerb   itemsr+  )
sequentialnamedenumsreverser4   r4   r5   enumf  s    rd  c             C   s$   d | ||}|r |d |7 }|S )z
    Return the URL for a VCS requirement.

    Args:
      repo_url: the remote VCS url, with any needed VCS prefix (e.g. "git+").
    z{}@{}#egg={}z&subdirectory={})format)repo_urlrevegg_project_namesubdirrV  r4   r4   r5   make_vcs_requirement_urlm  s    rj  c             C   sJ   d| kr| dfS |  dd\}} d|kr:t|dd}n|df}| |fS )zp
    Parse out and remove the auth information from a netloc.

    Returns: (netloc, (username, password)).
    @)NNrd   :N)rsplittuplern   )netlocauth	user_passr4   r4   r5   split_auth_from_netloc{  s    rr  c             C   s<   t | }t|j\}}|j||j|j|jf}t |}|S )N)	urllib_parseurlsplitrr  ro  schemer<   queryfragment
urlunsplit)r   purlro  rq  
url_piecessurlr4   r4   r5   r-     s
    

c             C   sz   dd tjd dj tjdd  g}| oBtoBtjtjd |k}|rvtjddgtjd	d  }t	d
 d
|dS )zProtection of pip.exe from modification on Windows

    On Windows, any operation modifying pip should be run as:
        python -m pip ...
    zpip.exez	pip{}.exer   zpip{}.{}.exeNrU   z-mr>   rd   z3To modify pip, please run the following command:
{}r   )re  r@   rZ   r   r7   r<   r?   rA   rB   r	   rx   )modifying_pip	pip_namesshould_show_use_python_msgnew_commandr4   r4   r5   (protect_pip_from_modification_on_windows  s    
r  )F)rc   )T)T)TNr   NNNN)N)N)m
__future__r   
contextlibr:   ior(  loggingr  r7   r   r   rK   rN   r  r@   r   r   collectionsr   pip._vendorr   Zpip._vendor.retryingr   Zpip._vendor.sixr   Zpip._vendor.six.movesr   Zpip._vendor.six.moves.urllibr   rs  pip._internal.exceptionsr	   r
   r   r   r   r   r   r   pip._internal.utils.compatr   r   r   r   r   r   __all__	getLoggerr=  r   r   r   r   r   r*   r+   bz2r/   r
  lzmar6   r)   r#   r   rL   r   r   rr   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/  r1  objectr2  rA  contextmanagerrL  r(   rM  r,   rY  rd  rj  rr  r-   r  r4   r4   r4   r5   <module>   s   
	

	
	 	
	5%
+L!   
n

