3
[t                 @   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j/e0Z1dZ2dZ3dZ4dZ5e4e2 e5 e3 Z6e4e5 Z7yd dl8Z8e7e27 Z7W n  e9k
r   e1j:d2 Y nX yd dl;Z;e7e37 Z7W n  e9k
r   e1j:d3 Y nX d4d5 Z<d6d! Z=d7d Z>ed8d9d:dd<dZ?d=d> Z@d?d ZAddAdZBdBdC ZCdDd ZDdEd ZEdFd ZFdGd ZGdHd ZHejIfdIdJZJdKd ZKdLd ZLddNdZMdOd ZNdPd ZOdQdR ZPdSdT ZQdUdV ZRdWdX ZSdYdZ ZTdMe+dMd;d;fd[d\ZUd]d^ ZVd_d` ZWdadb ZXddcdZYddd ZZded Z[ddgdZ\dhdi Z]djdk Z^G dldm dme_Z`G dndo doe-Zaejbdpdq Zcdrd  ZdG dsdt dte_Zeddud$Zfdvdw Zgdxdy Zhddzd{Zid|d} Zjd~d% Zkdd 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.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   s,   yt | S  tk
r&   |||Y nX d S )N)
__import__ImportError)pkg_or_module_stringExceptionTypeargskwargs r@   9/tmp/pip-install-65viz3iy/pip/pip/_internal/utils/misc.pyimport_or_raiseO   s    rB   c             C   sD   yt j|  W n0 tk
r> } z|jtjkr. W Y dd}~X nX dS )z os.path.makedirs without EEXIST.N)osmakedirsOSErrorerrnoEEXIST)pather@   r@   rA   r)   V   s
    c              C   sL   y,t j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   __main__.py-cz	%s -m pippip)rJ   rK   )	rC   rH   basenamesysargv
executableAttributeError	TypeError
IndexError)progr@   r@   rA   r#   _   s    
i  i  )stop_max_delay
wait_fixedFc             C   s   t j| |td d S )N)ignore_errorsonerror)shutilr   rmtree_errorhandler)dirrW   r@   r@   rA   r   l   s    c             C   s2   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)rC   statst_modeS_IREADchmodS_IWRITE)funcrH   exc_infor@   r@   rA   rZ   r   s
    rZ   c             C   st   t jjt jj| } tjd dkrB| jtj d} | jtj	 d} | j
t j t jj rpd| tt j d  } | S )zTGives the display value for a given path, making it relative to cwd
    if possible.r      replace.N)rC   rH   normcaseabspathrN   version_infodecodegetfilesystemencodingencodegetdefaultencoding
startswithgetcwdseplen)rH   r@   r@   rA   r      s    .bakc             C   s:   d}|}x(t j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)   )rC   rH   existsstr)r[   extn	extensionr@   r@   rA   r      s    c             C   s2   x&t jjddj D ]}||kr|S qW t| |S )NPIP_EXISTS_ACTION )rC   environgetsplitr   )messageoptionsactionr@   r@   rA   ask_path_exists   s    r   c             C   sZ   xTt jjdrtd|  t| }|j j }||krNtd|dj|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)	rC   rz   r{   	Exceptionr   striplowerprintjoin)r}   r~   responser@   r@   rA   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  z%.1fMBg     @@
   z%ikBz%.1fkBz%ibytesi@B i'  r@   )bytesr@   r@   rA   r      s    c             C   sP   t jj| sdS t jj| d}t jj|r.dS t jj| d}t jj|rLdS dS )zBIs path is a directory containing setup.py or pyproject.toml?
    Fzsetup.pyTzpyproject.toml)rC   rH   isdirr   isfile)rH   setup_pypyproject_tomlr@   r@   rA   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@   rA   r      s    c          	   C   s$   t | d}|j jdS Q R X d S )Nrbzutf-8)openreadri   )filenamefpr@   r@   rA   r      s    c             c   s    x| j |}|sP |V  qW dS )z7Yield pieces of data from a file-like object until EOF.N)r   )filesizechunkr@   r@   rA   read_chunks   s
    
r   c             C   sh   | j dj d} d| krHd| kr4| jd| jdk s<d| krH| jddS d| kr\| jddS | dfS d S )N/\rr   ry   )lstripfindr|   )rH   r@   r@   rA   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_prefixrH   prefixrestr@   r@   rA   r       s    
Tc             C   s2   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   rC   rH   realpathrg   rf   )rH   resolve_symlinksr@   r@   rA   r!      s
    c             C   s@   t j| \}}|j jdr8|dd | }|dd }||fS )z,Like os.path.splitext, but take off .tar tooz.tar   Nr   )	posixpathr   r   endswith)rH   baseru   r@   r@   rA   r     s
    c             C   s|   t jj|\}}|r0|r0t jj| r0t j| tj| | t jj| \}}|rx|rxyt j| W n tk
rv   Y nX dS )z7Like os.renames(), but handles renaming across devices.N)	rC   rH   r|   rs   rD   rY   move
removedirsrE   )oldnewheadtailr@   r@   rA   r"     s    
c             C   s   t  s
dS t| 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!   rm   rN   r   )rH   r@   r@   rA   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@   rA   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   rm   r   )r   	norm_pathr@   r@   rA   dist_in_usersite9  s    r   c             C   s   t t| jt tS )z[
    Return True if given Distribution is installed in
    sysconfig.get_python_lib().
    )r!   r   rm   r   )r   r@   r@   rA   dist_in_site_packagesA  s    
r   c             C   s8   x2t jD ](}tjj|| jd }tjj|rdS qW dS )z$Is distribution an editable install?z	.egg-linkTF)rN   rH   rC   r   project_namer   )r   	path_itemegg_linkr@   r@   rA   dist_is_editableK  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 )NTr@   )dr@   r@   rA   
local_testm  s    z/get_installed_distributions.<locals>.local_testc             S   s   dS )NTr@   )r   r@   r@   rA   editable_testq  s    z2get_installed_distributions.<locals>.editable_testc             S   s
   t |  S )N)r   )r   r@   r@   rA   r   t  s    c             S   s   t | S )N)r   )r   r@   r@   rA   editables_only_testx  s    z8get_installed_distributions.<locals>.editables_only_testc             S   s   dS )NTr@   )r   r@   r@   rA   r   {  s    c             S   s   dS )NTr@   )r   r@   r@   rA   	user_test  s    z.get_installed_distributions.<locals>.user_testc                s:   g | ]2}|r|j kr |r|r|r|qS r@   )key).0r   )r   r   r   skipr   r@   rA   
<listcomp>  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   rA   get_installed_distributionsT  s    

r   c             C   s   g }t  r6t r|jt qN|jt trN|jt ntrD|jt |jt x0|D ](}tjj|| jd }tj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   rC   rH   r   r   r   )r   sitessiteegglinkr@   r@   rA   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@   rA   r     s    r   c              C   s   t jd} t j|  | S )zBGet the current umask which involves having to set it temporarily.r   )rC   umask)maskr@   r@   rA   current_umask  s    

r   c             C   s  t | t| d}ztj|dd}t|j o0|}x|j D ]}|j}|j|}|}	|rdt	|d }	t
jj||	}	t
jj|	}
|	jds|	jdrt |	 q<t |
 t|	d}z|j| W d|j  |jd	? }|rtj|r|d
@ rt
j|	dt  d
B  X q<W W d|j  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)
allowZip64rr   r   r   wbN   I   i  )r)   r   zipfileZipFiler    namelistinfolistr   r   r   rC   rH   r   dirnamer   writecloseexternal_attrr\   S_ISREGr_   r   )r   r   flattenzipfpzipleadinginfonamedatafnr[   r   moder@   r@   rA   r$     s0    	




 c              C   s  t | | j jds$| j jdr*d}nL| j jtr>d}n8| j jtrRd}n$| j jdrfd}ntjd|  d	}tj| |}zxt	d
d |j
 D }xZ|j
 D ]L}|j}|dkrq|rt|d }tjj||}|j rt | q|j rDy|j|| W n8 tk
r@ } ztjd| |j| wW Y dd}~X nX qy|j|}	W n< ttfk
r } ztjd| |j| wW Y dd}~X nX t tjj| t|d}
tj|	|
 W dQ R X |	j  |j|| |jd@ rtj|dt  dB  qW W d|j  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   memberr@   r@   rA   r     s    zuntar_file.<locals>.<listcomp>r   rr   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   rC   rH   r   r   issym_extract_memberr   extractfileKeyErrorrQ   r   rY   copyfileobjr   utimer   r_   r   )r   r   r   tarr   r   r   rH   excr   destfpr@   r@   rA   r%     s`    	


c             C   s   t jj| } |dks,| j jts,tj| rDt| || jd d n|dkslt	j
| sl| j jtt t rxt| | nX|r|jdrtt| rddlm} |d|j j| ntj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)rC   rH   r   r   r   ZIP_EXTENSIONSr   
is_zipfiler$   r   
is_tarfileTAR_EXTENSIONSr   r   r%   rm   r   r   pip._internal.vcs.subversionr   urlunpackr   criticalr
   )r   r   content_typelinkr   r@   r@   rA   r&   >  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|
jdd }
|	j|
 q.W dj|	}tjd| tjj	 }|r|j
| x|D ]}|j|d qW y(t j| t jt j|||d	}|jj  W n4 tk
r } ztjd
||  W Y dd}~X nX g }|dk	rx`t|jj }|s<P |j }|j|d  tj tjkrntj| n|dk	r&|j  q&W z|j  W d|jr|jj  X |dk	r|jr|jd n
|jd |jrj|dkr0tj tjkr| rtjd| tjdj|d  td||j|f n:|dkrNtjd||j| n|dkrZnt dt!| |szdj|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:ry   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PIPErd   r   r   r   debugrC   rz   copyupdatepopPopenSTDOUTr  r   r   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liner@   r@   rA   r'   _  s    	
 












c             C   sx   t | d}|j }W dQ R X dtjddg}x4|D ],}y|j|}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getpreferredencodingri   UnicodeDecodeErrortyper   AssertionError)r   r   r   	encodingsencr@   r@   rA   read_text_file  s    	
r;  c             C   s   t j|  t|  d S )N)rC   rD   r   )	build_dirr@   r@   rA   _make_build_dir  s    
r=  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@   rA   	<genexpr>  s    z$FakeFile.__init__.<locals>.<genexpr>)_gen)selflinesr@   r@   rA   __init__  s    zFakeFile.__init__c             C   sD   y*y
t | jS  tk
r&   | jj  S X W n tk
r>   dS X d S )Nry   )nextrA  	NameErrorStopIteration)rB  r@   r@   rA   r    s    
zFakeFile.readlinec             C   s   | j S )N)rA  )rB  r@   r@   rA   __iter__  s    zFakeFile.__iter__N)__name__
__module____qualname____doc__rD  r  rH  r@   r@   r@   rA   r>    s   	r>  c               @   s$   e Zd Zedd Zedd ZdS )StreamWrapperc             C   s   || _ |  S )N)orig_stream)clsrN  r@   r@   rA   from_stream  s    zStreamWrapper.from_streamc             C   s   | j jS )N)rN  encoding)rB  r@   r@   rA   rQ    s    zStreamWrapper.encodingN)rI  rJ  rK  classmethodrP  propertyrQ  r@   r@   r@   rA   rM     s   rM  c             c   s@   t t| }tt| tj| 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)getattrrN   setattrrM  rP  )stream_nameorig_stdoutr@   r@   rA   captured_output  s
    
rX  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  )rX  r@   r@   r@   rA   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 )NrL  )rT  rL  ra   )rB  ra   r@   r@   rA   rD  0  s    zcached_property.__init__c             C   s(   |d kr| S | j | }|j| j j< |S )N)ra   __dict__rI  )rB  objrO  valuer@   r@   rA   __get__4  s    zcached_property.__get__N)rI  rJ  rK  rL  rD  r]  r@   r@   r@   rA   rY  (  s   rY  c             C   s4   t jj| }|dkrt j }|j|}|r0|jS dS )zCGet the installed version of dist_name avoiding pkg_resources cacheN)r   Requirementr   
WorkingSetr   version)	dist_namer   reqr   r@   r@   rA   r,   <  s
    
c             C   s   t | dd dS )zConsume an iterable at C speed.r   )maxlenN)r   )iteratorr@   r@   rA   consumeN  s    re  c              O   s@   t t| tt| f|}dd |j D }||d< tdf |S )Nc             S   s   i | ]\}}||qS r@   r@   )r   r   r\  r@   r@   rA   
<dictcomp>V  s    zenum.<locals>.<dictcomp>reverse_mappingEnum)dictr   rangerp   itemsr7  )
sequentialnamedenumsreverser@   r@   rA   enumT  s    rp  c             C   s$   dj | ||}|r |dj |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)Zrepo_urlrevegg_project_namesubdirrb  r@   r@   rA   make_vcs_requirement_url[  s    ru  c             C   sJ   d| kr| dfS | j dd\}} d|kr:t|jdd}n|df}| |fS )zp
    Parse out and remove the auth information from a netloc.

    Returns: (netloc, (username, password)).
    @Nrr   :)NN)rsplittupler|   )netlocauth	user_passr@   r@   rA   split_auth_from_netloci  s    r}  c             C   s<   t j| }t|j\}}|j||j|j|jf}t j|}|S )N)	urllib_parseurlsplitr}  rz  schemerH   queryfragment
urlunsplit)r  purlrz  r|  
url_piecessurlr@   r@   rA   r-     s
    

c             C   sz   ddj tjd dj tjdd  g}| oBtoBtjjtjd |k}|rvtjddgtjd	d  }t	d
j dj
|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{}.{}.exeNrc   z-mrL   rr   z3To modify pip, please run the following command:
{}r  )rq  rN   rh   r   rC   rH   rM   rO   rP   r	   r   )Zmodifying_pipZ	pip_namesshould_show_use_python_msgnew_commandr@   r@   rA   (protect_pip_from_modification_on_windows  s    
r  )r.   r/   )r0   r1   r2   r3   r4   )r5   r6   )r7   r8   r9   )F)rq   )T)T)TNr  NNNN)N)N)m
__future__r   
contextlibrF   ior4  loggingr  rC   r   r   rY   r\   r  rN   r   r   collectionsr   pip._vendorr   pip._vendor.retryingr   pip._vendor.sixr   pip._vendor.six.movesr   Zpip._vendor.six.moves.urllibr   r~  pip._internal.exceptionsr	   r
   pip._internal.locationsr   r   r   r   r   Zpip._internal.utils.compatr   r   r   r   r   r   __all__	getLoggerrI  r   r   r   r   r   r*   r+   bz2r;   r  lzmarB   r)   r#   r   rZ   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=  objectr>  rM  contextmanagerrX  r(   rY  r,   re  rp  ru  r}  r-   r  r@   r@   r@   rA   <module>   s   
	

	
	
	5%
+L!   
n

