U
    KQhm                     @   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$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	*e+Z,d%Z-d&Z.d'Z/d(Z0e/e- e0 e. Z1e/e0 Z2zd dl3Z3e2e-7 Z2W n  e4k
r   e,5d) Y nX zd dl6Z6e2e.7 Z2W n  e4k
r   e,5d* Y nX d+d, Z7d-d! Z8d.d Z9e&d/d0d1dpd3dZ:d4d5 Z;d6d Z<dqd8dZ=d9d: Z>d;d Z?d<d Z@d=d ZAd>d ZBd?d ZCejDfd@dAZEdBd ZFdCd ZGdrdEdZHdFd ZIdGd ZJdHdI ZKdJdK ZLdLdM ZMdNdO ZNdPdQ ZOdDedDd2d2fdRdSZPdTdU ZQdVdW ZRdXd ZSdYdZ ZTdsd[dZUd\d ZVd]d ZWdtd_dZXd`da ZYdbdc ZZG ddde dee[Z\G dfdg dge(Z]ej^dhdi Z_djd  Z`G dkdl dle[Zadudmd$Zbdndo ZcdS )v    )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
ensure_dirARCHIVE_EXTENSIONSSUPPORTED_EXTENSIONSget_installed_version)z.tar.bz2z.tbz)z.tar.xzz.txzz.tlzz.tar.lzz	.tar.lzma)z.zip.whl)z.tar.gz.tgz.tarzbz2 module is not availablezlzma module is not availablec                 O   s.   z
t | W S  tk
r(   |||Y nX d S N)
__import__ImportError)pkg_or_module_stringExceptionTypeargskwargs r6   1/tmp/pip-build-tlro1kwj/pip/pip/utils/__init__.pyimport_or_raiseI   s    
r8   c              
   C   sD   zt |  W n0 tk
r> } z|jtjkr. W 5 d}~X Y nX dS )z os.path.makedirs without EEXIST.N)osmakedirsOSErrorerrnoEEXIST)pather6   r6   r7   r(   P   s
    c                
   C   sF   z&t jtjd dkr$dtj W S W n tttfk
r@   Y nX dS )Nr   )z__main__.pyz-cz	%s -m pippip)	r9   r>   basenamesysargv
executableAttributeError	TypeError
IndexErrorr6   r6   r6   r7   r"   Y   s    i  i  )stop_max_delay
wait_fixedFc                 C   s   t j| |td d S )N)ignore_errorsonerror)shutilr   rmtree_errorhandler)dirrJ   r6   r6   r7   r   c   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)r9   statst_modeS_IREADchmodS_IWRITE)funcr>   exc_infor6   r6   r7   rM   i   s
    rM   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)r9   r>   normcaseabspathrB   version_infodecodegetfilesystemencodingencodegetdefaultencoding
startswithgetcwdseplenr>   r6   r6   r7   r   x   s    .bakc                 C   s6   d}|}t j| | r.|d7 }|t| }q| | S )z\Figure out the name of a directory to back up the given dir to
    (adding .bak, .bak2, etc)   )r9   r>   existsstr)rN   extn	extensionr6   r6   r7   r      s    c                 C   s2   t jdd D ]}||kr|  S qt| |S )NPIP_EXISTS_ACTION )r9   environgetsplitr   )messageoptionsactionr6   r6   r7   ask_path_exists   s    
rt   c                 C   sV   t jdrtd|  t| }|  }||krLtd|d|f  q |S q 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)	r9   rn   ro   	Exceptionr   striplowerprintjoin)rq   rr   responser6   r6   r7   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%ibytesr6   )bytesr6   r6   r7   r      s    c                 C   s2   t j| sdS t j| d}t j|r.dS dS )z@Return True if `path` is a directory containing a setup.py file.Fzsetup.pyT)r9   r>   isdirrz   isfile)r>   setup_pyr6   r6   r7   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)htmlr6   r6   r7   r      s    c              
   C   s0   t | d}| dW  5 Q R  S Q R X d S )Nrbutf-8)openreadr\   )filenamefpr6   r6   r7   r      s    c                 c   s   |  |}|sq|V  q dS )z7Yield pieces of data from a file-like object until EOF.N)r   )filesizechunkr6   r6   r7   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/\rf   rm   )lstripfindrp   rd   r6   r6   r7   r      s    $c                 C   sD   d}| D ]6}t |\}}|s" dS |dkr0|}q||kr dS q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restr6   r6   r7   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   r9   r>   realpathrZ   rY   )r>   resolve_symlinksr6   r6   r7   r      s
    c                 C   s@   t | \}}| dr8|dd | }|dd }||fS )z,Like os.path.splitext, but take off .tar toor.   N)	posixpathr   rx   endswith)r>   baseri   r6   r6   r7   r      s
    c                 C   sz   t j|\}}|r.|r.t j|s.t | t| | t j| \}}|rv|rvzt | W n tk
rt   Y nX dS )z7Like os.renames(), but handles renaming across devices.N)	r9   r>   rp   rg   r:   rL   move
removedirsr;   )oldnewheadtailr6   r6   r7   r      s    
c                 C   sp   t | } t tj}t r(| t tjS ddlm} | |rh|d D ]}| t |rJ dS qJd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_schemerm   TFN)r   rB   r   r   r`   pip.locationsr   values)r>   r   r   
local_pathr6   r6   r7   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distr6   r6   r7   dist_is_local5  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_pathr6   r6   r7   dist_in_usersite>  s    r   c                 C   s   t t| t tS )ze
    Return True if given Distribution is installed in
    distutils.sysconfig.get_python_lib().
    )r   r   r`   r	   r   r6   r6   r7   dist_in_site_packagesF  s
    r   c                 C   s6   t jD ]*}tj|| jd }tj|r dS qdS )z$Is distribution an editable install?	.egg-linkTF)rB   r>   r9   rz   project_namer~   )r   	path_itemegg_linkr6   r6   r7   dist_is_editableP  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 ``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 NTr6   dr6   r6   r7   
local_testr  s    z/get_installed_distributions.<locals>.local_testc                 S   s   dS r   r6   r   r6   r6   r7   editable_testv  s    z2get_installed_distributions.<locals>.editable_testc                 S   s
   t |  S r/   r   r   r6   r6   r7   r   y  s    c                 S   s   t | S r/   r   r   r6   r6   r7   editables_only_test}  s    z8get_installed_distributions.<locals>.editables_only_testc                 S   s   dS r   r6   r   r6   r6   r7   r     s    c                 S   s   dS r   r6   r   r6   r6   r7   	user_test  s    z.get_installed_distributions.<locals>.user_testc                    s:   g | ]2}|r|j kr |r|r|r|qS r6   )key).0r   r   r   r   skipr   r6   r7   
<listcomp>  s    
z/get_installed_distributions.<locals>.<listcomp>)r   r   r   working_set)
local_onlyr   include_editableseditables_only	user_onlyr6   r   r7   get_installed_distributionsY  s    

r   c                 C   s   g }t  r6t r|t qN|t trN|t ntrD|t |t |D ],}tj|| jd }tj	|rR|  S qRd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.
    r   N)
r   r   appendr	   r
   r9   r>   rz   r   r~   )r   sitessiteegglinkr6   r6   r7   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   r6   r6   r7   r     s    r   c                  C   s   dd } | dp| dp| d}|sZz(t t  t j}| |}t | W n   Y nX |szt jddt jdd	f}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   sT   z4dd l }dd l}dd l}|d|| |jd}W n   Y d S X |dkrPd S |S )Nr   hh1234)r   r   )fcntltermiosstructunpackioctl
TIOCGWINSZ)fdr   r   r   crr6   r6   r7   ioctl_GWINSZ  s    z'get_terminal_size.<locals>.ioctl_GWINSZr   rf   rV   LINES   COLUMNSP   )r9   r   ctermidO_RDONLYclosern   ro   int)r   r   r   r6   r6   r7   r!     s    c                  C   s   t d} t |  | S )zBGet the current umask which involves having to set it temporarily.r   )r9   umask)maskr6   r6   r7   current_umask  s    

r   c                 C   s  t | t| d}ztj|dd}t| o0|}| D ]}|j}|	|}|}	|rbt
|d }	tj||	}	tj|	}
|	ds|	drt |	 q:t |
 t|	d}z|| W 5 |  |jd? }|rt|r|d	@ rt|	d
t  d	B  X q:W 5 |  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)
allowZip64rf   r   r   wb   I     N)r(   r   r   zipfileZipFiler   namelistinfolistr   r   r   r9   r>   rz   dirnamer   external_attrrO   S_ISREGrR   r   write)r   r   flattenzipfpzipleadinginfonamedatafnrN   r   moder6   r6   r7   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| |}z~t
d
d | D }| D ]X}|j}|dkrq|rt|d }tj||}| rt | q| rFz||| W n> tk
rB } ztd| |j| W Y qW 5 d}~X Y nX qz||}	W nB ttfk
r } ztd| |j| W Y qW 5 d}~X Y nX t tj| t|d}
t|	|
 W 5 Q R X |		  ||| |jd@ rt|dt  dB  qW 5 |	  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.gzr-   zr:gzzr:bz2zr:xzr.   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   memberr6   r6   r7   r   /  s   
zuntar_file.<locals>.<listcomp>r   rf   z/In the tar file %s the member %s is invalid: %sNr   r   r   )r(   rx   r   BZ2_EXTENSIONSXZ_EXTENSIONSloggerwarningtarfiler   r   r   
getmembersr   r   r9   r>   rz   r}   issym_extract_memberrv   extractfileKeyErrorrE   r   rL   copyfileobjutimer   rR   r   )r   r   r   tarr   r   r   r>   excr   destfpr6   r6   r7   r$     sr    	 

    
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/zipr,   )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)r9   r>   r   rx   r   ZIP_EXTENSIONSr   
is_zipfiler#   r   
is_tarfileTAR_EXTENSIONSr   r   r$   r`   r   r   pip.vcs.subversionr
  urlr   r   criticalr   )r   r   content_typelinkr
  r6   r6   r7   r%   a  sB    


  raisec              
   C   s$  |r
d }nt j}|d krjg }| D ]>}	d|	ksDd|	ksDd|	ksDd|	krTd|	dd }	||	 q d|}td| tj	 }
|r|

| zt j| t jd |||
d}W n2 tk
r } ztd	||  W 5 d }~X Y nX |d k	rHg }t|j }|sqH| }||d  t tjkr6t| q|d k	r|  q|  |d k	rx|jrn|d
 n
|d |jr|dkrt tjkr|std| td|d  td||j|f n:|dkrtd||j| n|dkr ntdt| |s d|S d 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:rm   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PIPErW   r   rz   r   debugr9   rn   copyupdatePopenSTDOUTrv   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spinnerr  	cmd_partspartr  procr  
all_outputliner6   r6   r7   r&     s     


    
  




 

  
c              	   C   sx   t | d}| }W 5 Q R X dtddg}|D ]0}z||}W n tk
r\   Y q2Y nX  qdq2t|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   r   Flatin1)	r   r   localegetpreferredencodingr\   UnicodeDecodeErrortyper|   AssertionError)r   r   r   	encodingsencr6   r6   r7   read_text_file  s    	
rG  c                 C   s   t |  t|  d S r/   )r9   r:   r   )	build_dirr6   r6   r7   _make_build_dir  s    
rI  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 r/   r6   )r   lr6   r6   r7   	<genexpr>  s     z$FakeFile.__init__.<locals>.<genexpr>_gen)selflinesr6   r6   r7   __init__  s    zFakeFile.__init__c                 C   sP   z4zt | jW W S  tk
r0   | j   Y W S X W n tk
rJ   Y dS X d S )Nrm   )nextrN  	NameErrorStopIterationrO  r6   r6   r7   r)    s    zFakeFile.readlinec                 C   s   | j S r/   rM  rU  r6   r6   r7   __iter__  s    zFakeFile.__iter__N)__name__
__module____qualname____doc__rQ  r)  rV  r6   r6   r6   r7   rJ    s   	rJ  c                   @   s$   e Zd Zedd Zedd ZdS )StreamWrapperc                 C   s   || _ |  S r/   )orig_stream)clsr\  r6   r6   r7   from_stream  s    zStreamWrapper.from_streamc                 C   s   | j jS r/   )r\  encodingrU  r6   r6   r7   r_    s    zStreamWrapper.encodingN)rW  rX  rY  classmethodr^  propertyr_  r6   r6   r6   r7   r[    s   
r[  c              
   c   s@   t t| }tt| t| zt t| V  W 5 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)getattrrB   setattrr[  r^  )stream_nameorig_stdoutr6   r6   r7   captured_output!  s
    
rf  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  )rf  r6   r6   r6   r7   r'   0  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 )NrZ  )rb  rZ  rT   )rO  rT   r6   r6   r7   rQ  D  s    zcached_property.__init__c                 C   s(   |d kr| S |  | }|j| j j< |S r/   )rT   __dict__rW  )rO  objr]  valuer6   r6   r7   __get__H  s    zcached_property.__get__N)rW  rX  rY  rZ  rQ  rk  r6   r6   r6   r7   rg  <  s   rg  c                 C   s@   t j| }|dkrt  }n
t |}||}|r<|jS dS )zCGet the installed version of dist_name avoiding pkg_resources cacheN)r   Requirementparse
WorkingSetr   version)	dist_namelookup_dirsreqr   r   r6   r6   r7   r+   P  s    


c                 C   s   t | dd dS )zConsume an iterable at C speed.r   )maxlenNr   )iteratorr6   r6   r7   consumed  s    ru  )F)re   )T)T)TNr  NNN)N)d
__future__r   collectionsr   
contextlibr<   ior@  loggingr,  r   r9   r   rL   rO   r"  rB   r   r   pip.exceptionsr   
pip.compatr   r   r   r   r	   r
   r   r   r   pip._vendorr   Zpip._vendor.six.movesr   Zpip._vendor.sixr   Zpip._vendor.retryingr   r   r   __all__	getLoggerrW  r   r   r   r  r  r)   r*   bz2r1   r$  lzmar8   r(   r"   r   rM   r   r   rt   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&   rG  rI  objectrJ  r[  contextmanagerrf  r'   rg  r+   ru  r6   r6   r6   r7   <module>   s              
	



	
	 	
	
9%
+L!      
b

