
 X a                 @   s  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 m Z m Z d  d l m Z m Z m Z m Z m Z m Z d  d l m Z m Z m Z m Z d  d l m Z d  d l Z 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 d  d   Z  d! d" d  Z! d# d$   Z" d% d   Z# d& d' d	  Z$ d d d( d
  Z% d d) d*  Z& d+ d,   Z' d- d   Z( Gd. d/   d/ e)  Z* e*   Z+ [* e j, d0 e j-  Z. d1 d   Z/ d2 d   Z0 d3 d   Z1 d4 d   Z2 d5 d   Z3 d6 d   Z4 d7 d   Z5 d8 d   Z6 d9 d   Z7 d: d   Z8 d; d   Z9 d< d=   Z: d> d?   Z; d@ dA   Z< dB dC   Z= dD dE   Z> dF d\ dF d! dK dL  Z? dM dN   Z@ dO dP   ZA dQ d   ZB dR dS   ZC dF dT d  ZD dU d   ZE dV d   ZF dW d   ZG dX d   ZH dF d d dF e jI d d dY d  ZJ dZ d[   ZK d S)]    N)InstallationError
BadCommandPipError)WindowsErrorstring_types	raw_inputconsole_to_str	user_sitePermissionError)site_packagesrunning_under_virtualenvvirtualenv_no_globaldistutils_scheme)logger)versionrmtreedisplay_path
backup_dirfind_commandaskInfnormalize_namesplitextformat_sizeis_installable_diris_svn_pagefile_contentssplit_leading_dirhas_leading_dirmake_path_relativenormalize_pathrenamesget_terminal_sizeget_prog
unzip_file
untar_filecreate_download_cache_foldercache_downloadunpack_filecall_subprocessc               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)r*   r+   )	ospathbasenamesysargv
executableAttributeError	TypeError
IndexError r6   r6   3/home/ubuntu/projects/ifolica/build/pip/pip/util.pyr#   !   s    Fc             C   s   t  j |  d | d t d  S)Nignore_errorsonerror)shutilr   rmtree_errorhandler)dirr8   r6   r6   r7   r   *   s    c             C   s   | d d  \ } } | t  k r5 | j d d k pp | t k rT | j d d k pp | t k op | j d d k sy   n  t j |  j t j @t j k r   n  t j | t j	  |  |  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   r            )
r   argsOSErrorr
   r-   statst_modeS_IREADchmodS_IWRITE)funcr.   exc_infoexctypevaluer6   r6   r7   r;   /   s    "r;   c             C   sd   t  j j t  j j |    }  |  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..N)r-   r.   normcaseabspath
startswithgetcwdseplen)r.   r6   r6   r7   r   B   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)   )r-   r.   existsstr)r<   extn	extensionr6   r6   r7   r   K   s    
c             C   s7  | d k r0 t  j j d d  j t  j  } n  t | t  rK | g } n  | d k rc t   } n  d d   | j   j t  j  D } t  j	 j
 |   d j   | k r d g } n  xj | D]b } t  j	 j | |   } x. | D]& } | | } t  j	 j |  r | Sq Wt  j	 j |  r | Sq Wt d |    d S)z<Searches the PATH for the given command and returns its pathNPATH c             S   s"   g  |  ] } t  |  r |  q Sr6   )rR   ).0rV   r6   r6   r7   
<listcomp>_   s   	 z find_command.<locals>.<listcomp>rS   zCannot find command %r)r-   environgetsplitpathsep
isinstancer   get_pathextlowerr.   r   joinisfiler   )cmdpathspathextr.   cmd_pathrV   cmd_path_extr6   r6   r7   r   V   s$    $%"
c             C   sF   |  d k r- t  j j d d d d g  }  n  t  j j d |   } | S)z9Returns the path extensions from environment or a defaultNz.COMz.EXEz.BATz.CMDPATHEXT)r-   r`   rd   r]   r^   )default_pathextrh   r6   r6   r7   rb   q   s    !rb   c             C   sC   x3 t  j j d d  j   D] } | | k r | Sq Wt |  |  S)NPIP_EXISTS_ACTIONrZ   )r-   r]   r^   r_   r   )messageoptionsactionr6   r6   r7   ask_path_existsy   s    "rq   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)	r-   r]   r^   	Exceptionr   striprc   printrd   )rn   ro   responser6   r6   r7   r      s    c               @   sj   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z	 d d   Z
 d S)_InfzI am bigger than everything!c             C   s   |  | k r d Sd Sd  S)NTFr6   )selfotherr6   r6   r7   __eq__   s    z_Inf.__eq__c             C   s   |  j  |  S)N)rz   )rx   ry   r6   r6   r7   __ne__   s    z_Inf.__ne__c             C   s   d S)NFr6   )rx   ry   r6   r6   r7   __lt__   s    z_Inf.__lt__c             C   s   d S)NFr6   )rx   ry   r6   r6   r7   __le__   s    z_Inf.__le__c             C   s   d S)NTr6   )rx   ry   r6   r6   r7   __gt__   s    z_Inf.__gt__c             C   s   d S)NTr6   )rx   ry   r6   r6   r7   __ge__   s    z_Inf.__ge__c             C   s   d S)Nr   r6   )rx   r6   r6   r7   __repr__   s    z_Inf.__repr__N)__name__
__module____qualname____doc__rz   r{   r|   r}   r~   r   r   r6   r6   r6   r7   rw      s   rw   z[^a-z]c             C   s   t  j d |  j    S)N-)_normalize_resubrc   )namer6   r6   r7   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'  r6   )bytesr6   r6   r7   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)r-   r.   isdirrd   re   )r.   setup_pyr6   r6   r7   r      s    c             C   s(   t  j d |   o' t  j d |  t  j  S)zJReturns true if the page appears to be the index page of an svn repositoryz<title>[^<]*Revision \d+:z#Powered by (?:<a[^>]*?>)?Subversion)researchI)htmlr6   r6   r7   r      s    c          
   C   s8   t  |  d  } z | j   j d  SWd  | j   Xd  S)Nrbzutf-8)openreaddecodeclose)filenamefpr6   r6   r7   r      s    c             C   s   t  |   }  |  j d  j d  }  d |  k rv d |  k rZ |  j d  |  j d  k  sf d |  k rv |  j d d  Sd |  k r |  j d d  S|  d f Sd  S)N/\rS   rZ   )rU   lstripfindr_   )r.   r6   r6   r7   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   )rg   common_prefixr.   prefixrestr6   r6   r7   r      s    	c             C   s7  t  j j |   } t  j j |   }  t  j j t  j j |    }  t  j j t  j j |   } |  j t  j j  j t  j j  } | j t  j j  j t  j j  } x> | r | r | d | d k r | j	 d  | j	 d  q Wd g t
 |  | | g } | d g k r$d t  j j St  j j j |  S)a  
    Make a filename relative, where the filename path, and it is
    relative to rel_to

        >>> make_relative_path('/usr/share/something/a-file.pth',
        ...                    '/usr/share/another-place/src/Directory')
        '../../../something/a-file.pth'
        >>> make_relative_path('/usr/share/something/a-file.pth',
        ...                    '/home/user/src/Directory')
        '../../../usr/share/something/a-file.pth'
        >>> make_relative_path('/usr/share/a-file.pth', '/usr/share/')
        'a-file.pth'
    r   z..rZ   rL   )r-   r.   r/   dirnamenormpathrN   rt   rQ   r_   poprR   rd   )r.   rel_topath_filename
path_partsrel_to_parts
full_partsr6   r6   r7   r      s    $$#c             C   s(   t  j j t  j j t  j j |     S)zN
    Convert a path to its canonical, case-normalized, absolute version.

    )r-   r.   rM   realpath
expanduser)r.   r6   r6   r7   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   rc   endswith)r.   baserV   r6   r6   r7   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)	r-   r.   r_   rT   makedirsr:   move
removedirsrB   )oldnewheadtailr6   r6   r7   r!   !  s    c             C   s   t  |   }  t  t j  } t   r: |  j t  t j   S|  j |  r x3 t d  j   D] } |  j t  |   r\ d Sq\ Wd Sd Sd 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.)

    rZ   TFN)r    r0   r   r   rO   r   values)r.   r   
local_pathr6   r6   r7   is_local2  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_localQ  s    r   c             C   s-   t  r% t t |    j t t    Sd Sd S)zF
    Return True if given Distribution is installed in user site.
    FN)r	   r    r   rO   )r   r6   r6   r7   dist_in_usersiteZ  s    r   c             C   s   t  t |    j t  t   S)za
    Return True if given Distribution is installed in distutils.sysconfig.get_python_lib().
    )r    r   rO   r   )r   r6   r6   r7   dist_in_site_packagesc  s    r   c             C   s)   d d l  m } | j |  g   } | j S)z$Is distribution an editable install?r   )FrozenRequirement)r,   r   	from_disteditable)r   r   reqr6   r6   r7   dist_is_editablej  s    r   T
setuptoolsr,   python
distributec                s   |  r t   n d d    | r0 d d     n d d     | rQ d d    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 ('setuptools', 'pip', 'python'). [FIXME also
    skip virtualenv?]

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

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

    c             S   s   d S)NTr6   )dr6   r6   r7   <lambda>  s    z-get_installed_distributions.<locals>.<lambda>c             S   s   d S)NTr6   )r   r6   r6   r7   r     s    c             S   s   t  |   S)N)r   )r   r6   r6   r7   r     s    c             S   s
   t  |   S)N)r   )r   r6   r6   r7   r     s    c             S   s   d S)NTr6   )r   r6   r6   r7   r     s    c                sI   g  |  ]? }  |  r | j   k r   |  r  |  r |  q Sr6   )key)r[   r   )editable_testeditables_only_test
local_testskipr6   r7   r\     s
   	 z/get_installed_distributions.<locals>.<listcomp>)r   pkg_resourcesworking_set)
local_onlyr   include_editableseditables_onlyr6   )r   r   r   r   r7   get_installed_distributionsq  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	   r-   r.   rd   project_namere   )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   egg_linkr6   r6   r7   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   s   yL d d  l  } d d  l } d d  l } | j d | j |  | j d   } Wn d  SYn X| d k rk d  S| d k r{ d  S| S)Nr   hh1234)r   r   )r   r   )fcntltermiosstructunpackioctl
TIOCGWINSZ)fdr   r   r   crr6   r6   r7   ioctl_GWINSZ  s    	z'get_terminal_size.<locals>.ioctl_GWINSZr   rS   r=   LINES   COLUMNSP   )r-   r   ctermidO_RDONLYr   r]   r^   int)r   r   r   r6   r6   r7   r"     s    $-c              C   s    t  j d  }  t  j |   |  S)zBGet the current umask which involves having to set it temporarily.r   )r-   umask)maskr6   r6   r7   current_umask  s    r   c             C   s  t  j j |  s" t  j |  n  t |  d  } z}t j |  } t | j    oX | } xO| j	   D]A} | j
 } | j |  } | }	 | r t |  d }	 n  t  j j | |	  }	 t  j j |	  }
 t  j j |
  s t  j |
  n  |	 j d  s|	 j d  r1t  j j |	  st  j |	  qqh t |	 d  } z | j |  Wd | j   | j d ?} | rt j |  r| d @rt  j |	 d	 t   d B n  Xqh 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   rS   r   r   wbN   I   i  )r-   r.   rT   r   r   zipfileZipFiler   namelistinfolistr   r   r   rd   r   r   writer   external_attrrC   S_ISREGrF   r   )r   r   flattenzipfpzipleadinginfor   datafnr<   r   moder6   r6   r7   r$     s6    		
'c             C   s  t  j j |  s" t  j |  n  |  j   j d  sL |  j   j d  rU d } nh |  j   j d  s |  j   j d  r d } n5 |  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  | r8t |  d } n  t  j j | |  } | j   r~t  j j |  st  j |  qq | j   ry | j | |  Wqt j   d } t j d |  | j | f  w YqXq y | j |  }	 WnH t t f k
 r9t j   d } t j d |  | j | f  w Yn Xt  j j t  j j |   stt  j t  j j |   n  t	 | d  }
 z t j |	 |
  Wd |
 j   X|	 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.bz2z.tbzzr:bz2z.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[   memberr6   r6   r7   r\   5  s   	 zuntar_file.<locals>.<listcomp>r  rS   z/In the tar file %s the member %s is invalid: %sr   Nr   i  )r-   r.   rT   r   rc   r   r   warntarfiler   r   
getmembersr   r   rd   r   issym_extract_memberr0   rI   extractfileKeyErrorr3   r   r:   copyfileobjr   r   rF   r   )r   r   r   tarr   r  r   r.   er   destfpr6   r6   r7   r%     sf    	*	*			
&c             C   s@   t  j d 8_ t  j d |   t  j d 7_ t j |   d  S)Nr=   z&Creating supposed download cache at %s)r   indentnotifyr-   r   )folderr6   r6   r7   r&   i  s    c             C   sU   t  j d t |    t j | |   t |  d d  } | j |  | j   d  S)NzStoring download in cache at %sz.content-typew)r   r  r   r:   copyfiler   r   r   )target_filetemp_locationcontent_typer   r6   r6   r7   r'   p  s
    c             C   sC  t  j j |   }  | d k sZ |  j d  sZ |  j d  sZ |  j d  sZ t j |   rz t |  | d |  j d  n | d k s t j |   s t	 |   d j
   d k r t |  |  n~ | r| j d  rt t |    rd d l m } | d | j  j |  n* t j d |  | | f  t d |   d  S)Nzapplication/zipz.zip	.pybundle.whlr   zapplication/x-gziprS   .tar.tar.gz.tar.bz2.tgz.tbzz	text/htmlr   )
Subversionzsvn+zZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz%Cannot determine archive format of %s)r  r  )r  r  r  r  r  )r-   r.   r   r   r   
is_zipfiler$   r  
is_tarfiler   rc   r%   rO   r   r   pip.vcs.subversionr  urlr   r   fatalr   )r   r   r  linkr  r6   r6   r7   r(   x  s$     	c             C   s  | d  k r g  } xd |  D]\ }	 d |	 k sO d |	 k sO d |	 k sO d |	 k rh d |	 j  d d  }	 n  | j |	  q Wd j |  } n  | r d  }
 n	 t j }
 t j | d |  t j j	   } | r | j
 |  n  y4 t j |  d t j d	 d  d
 |
 d | d | } Wn< t k
 rNt j   d } t j d | | f    Yn Xg  } |
 d  k	 r| j }
 x t |
 j    } | sPn  | j   } | j | d  | r| |  } t | t  r| \ } } n  t j | |  t j |  st j   qqmt j |  qmWn! | j   \ } } | p4d g } | j   | j r| r| rt j d |  t j d j |  d  n  t d | | j | f   qt j d | | j | f  n  |
 d  k	 rd j |  Sd  S)N 
"'z"%s"z\"zRunning command %sstderrstdinstdoutcwdenvrS   z#Error %s while executing command %srZ   z Complete output from command %s:z)
----------------------------------------z*Command %s failed with error code %s in %sz"Command %s had error code %s in %s) replacer   rd   
subprocessPIPEr   logr-   r]   copyupdatePopenSTDOUTrs   r0   rI   r#  r+  r   readlinerstripra   tuplestdout_level_matchesshow_progressr   communicatewait
returncoder  r   r  )rf   show_stdoutfilter_stdoutr,  raise_on_returncodecommand_levelcommand_descextra_environ	cmd_partspartr+  r-  procr  
all_outputlinelevelreturned_stdoutreturned_stderrr6   r6   r7   r)     sp    0			
	c             C   sE   t  j |   } | d k r d St  j |  } t d d   | D  S)z
    Attempt to determine if this is a pre-release using PEP386/PEP426 rules.

    Will return True if it is a pre-release and False if not. Versions are
    assumed to be a pre-release if they cannot be parsed.
    NTc             S   s)   g  |  ] } t  d  d   | D   q S)c          	   S   s1   g  |  ]' } | t  d  d d d d g  k  q S)abcrcdev)set)r[   yr6   r6   r7   r\     s   	 z,is_prerelease.<locals>.<listcomp>.<listcomp>)any)r[   xr6   r6   r7   r\     s   	 z!is_prerelease.<locals>.<listcomp>)r   _suggest_normalized_version_normalized_keyrS  )vers
normalizedparsedr6   r6   r7   is_prerelease  s
    rZ  )z
setuptoolszpipzpythonz
distribute)Lr0   r:   r-   rC   r   r   r   r  r/  textwrappip.exceptionsr   r   r   Zpip.backwardcompatr   r   r   r   r	   r
   pip.locationsr   r   r   r   pip.logr   r   distlibr   __all__r#   r   r;   r   r   r   rb   rq   r   objectrw   r   compiler   r   r   r   r   r   r   r   r   r   r    r   r!   r   r   r   r   r   r   r   r   r"   r   r$   r%   r&   r'   r(   DEBUGr)   rZ  r6   r6   r6   r7   <module>   s   ."							
			'"-L	?