a
    e0hp                     @   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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" d dl#m$Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4m5Z5 d dl6m7Z7m8Z8 er\d dlm9Z: nd dlm:Z: e4rd dl;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZG d dlHmIZI eEeJeJeJf ZKeFdZLg dZMeNeOZPdd ZQdd ZRdd ZSdd ZTed d!d"dd$d%ZUd&d' ZVd(d) ZWd*d+ ZXdd-d.ZYd/d0 ZZd1d2 Z[d3d4 Z\d5d6 Z]d7d8 Z^d9d: Z_d;d< Z`d=d> Zaejbfd?d@ZcddBdCZddDdE ZedFdG ZfdHdI ZgdJdK ZhdLdM ZidNdO ZjdPdQ ZkdAe1dAd#d#dfdRdSZldTdU ZmdVdW ZndXdY ZodZd[ Zpd\d] ZqG d^d_ d_erZsG d`da dae:Ztejudbdc Zvddde Zwdfdg ZxddhdiZydjdk Zzdldm Z{dndo Z|ddqdrZ}dsdt Z~dudv Zdwdx Zdydz Zd{d| Zd}d~ Zdd Zdd Zdd ZG dd derZdd Zdd Zdd Zdd ZdddZdd Zdd Zdd ZdS )    )absolute_importNdeque)tee)pkg_resources)canonicalize_name)retry)PY2	text_type)filterfilterfalseinputmapzip_longest)parse)unquote)__version__)CommandError)get_major_minor_versionsite_packages	user_site)WINDOWS
expanduserstdlib_pkgsstr_to_display)MYPY_CHECK_RUNNINGcast)running_under_virtualenvvirtualenv_no_global)BytesIO)StringIO)AnyAnyStrCallable	ContainerIterableIteratorListOptionalTextTupleTypeVarUnion)DistributionT)rmtreedisplay_path
backup_dirasksplitextformat_sizeis_installable_dirnormalize_pathrenamesget_progcaptured_stdout
ensure_dirget_installed_versionremove_auth_from_urlc                  C   s4   t jt jtdd} t j| } dt| t S )Nz..zpip {} from {} (python {}))	ospathjoindirname__file__abspathformatr   r   )pip_pkg_dir rE   F/var/www/auris/lib/python3.9/site-packages/pip/_internal/utils/misc.pyget_pip_versionQ   s    rG   c                 C   sD   t | dk r"| dt |  d 7 } nt | dkr:| dd } td| S )ax  
    Convert a tuple of ints representing a Python version to one of length
    three.

    :param py_version_info: a tuple of ints representing a Python version,
        or None to specify no version. The tuple can have any length.

    :return: a tuple of length three if `py_version_info` is non-None.
        Otherwise, return `py_version_info` unchanged (i.e. None).
       )r   NVersionInfo)lenr   )py_version_inforE   rE   rF   normalize_version_info]   s
    rL   c              
   C   sR   zt |  W n> tyL } z&|jtjkr8|jtjkr8 W Y d}~n
d}~0 0 dS )z os.path.makedirs without EEXIST.N)r=   makedirsOSErrorerrnoEEXIST	ENOTEMPTY)r>   erE   rE   rF   r:   q   s
    r:   c               
   C   sP   z2t jtjd } | dv r*dtjW S | W S W n ttt	fyJ   Y n0 dS )Nr   )z__main__.pyz-cz	{} -m pippip)
r=   r>   basenamesysargvrC   
executableAttributeError	TypeError
IndexError)progrE   rE   rF   r8   |   s    
r8   i  i  )stop_max_delay
wait_fixedFc                 C   s   t j| |td d S )N)ignore_errorsonerror)shutilr/   rmtree_errorhandler)dirr^   rE   rE   rF   r/      s    r/   c              	   C   sV   zt |jtj@  }W n ttfy0   Y dS 0 |rPt |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)r=   statst_modeS_IWRITEIOErrorrN   chmod)funcr>   exc_infohas_attr_readonlyrE   rE   rF   ra      s    ra   c                 C   sb   | du rdS t | tr| S z| t d}W n. ty\   trPtd| }nt	| }Y n0 |S )z
    Convert a bytes (or text) path to text (unicode in Python 2) for display
    and logging purposes.

    This function should never error out. Also, this function is mainly needed
    for Python 2 since in Python 3 str paths are already text.
    Nstrictzb{!r})

isinstancer
   decoderU   getfilesystemencodingUnicodeDecodeErrorr	   r   rC   ascii)r>   r0   rE   rE   rF   path_to_display   s    	
rq   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)r=   r>   normcaserB   rU   version_inform   rn   encodegetdefaultencoding
startswithgetcwdseprJ   )r>   rE   rE   rF   r0      s    r0   .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)   )r=   r>   existsstr)rb   extn	extensionrE   rE   rF   r1      s    r1   c                 C   s2   t jdd D ]}||v r|  S qt| |S )NPIP_EXISTS_ACTION )r=   environgetsplitr2   )messageoptionsactionrE   rE   rF   ask_path_exists   s    
r   c                 C   s   t jdrtd| dS )z&Raise an error if no input is allowed.PIP_NO_INPUTz7No input was expected ($PIP_NO_INPUT set); question: {}N)r=   r   r   	ExceptionrC   r   rE   rE   rF   _check_no_input   s    r   c                 C   sF   t |  t| }|  }||vr<td|d| q |S q dS )z@Ask the message interactively, with the given possible responsesz>Your response ({!r}) was not one of the expected responses: {}z, N)r   r   striplowerprintrC   r?   )r   r   responserE   rE   rF   r2      s    
r2   c                 C   s   t |  t| S )zAsk for input interactively.)r   r   r   rE   rE   rF   	ask_input  s    r   c                 C   s   t |  t| S )z!Ask for a password interactively.)r   getpassr   rE   rE   rF   ask_password	  s    r   c                 C   s\   | dkrd | d d S | dkr4d t| d S | dkrJd | d S d t| S d S )	Ni@B z	{:.1f} MBg     @@i  i'  z{} kBz	{:.1f} kBz{} bytes)rC   int)bytesrE   rE   rF   r4     s    r4   c                    s@   dd | D } dd t | ddiD   fdd| D }| fS )zReturn a list of formatted rows and a list of column sizes.

    For example::

    >>> tabulate([['foobar', 2000], [0xdeadbeef]])
    (['foobar     2000', '3735928559'], [10, 4])
    c                 S   s   g | ]}t tt|qS rE   )tupler   r   .0rowrE   rE   rF   
<listcomp>%      ztabulate.<locals>.<listcomp>c                 S   s   g | ]}t tt|qS rE   )maxr   rJ   )r   colrE   rE   rF   r   &  r   	fillvaluer   c                    s$   g | ]}d  ttj|  qS ) )r?   r   r   ljustrstripr   sizesrE   rF   r   '  r   )r   )rowstablerE   r   rF   tabulate  s    	r   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)r=   r>   isdirr?   isfile)r>   setup_pypyproject_tomlrE   rE   rF   r5   +  s    r5   c                 c   s   |  |}|sq|V  q dS )z7Yield pieces of data from a file-like object until EOF.N)read)filesizechunkrE   rE   rF   read_chunks:  s    
r   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   r=   r>   realpathrB   ru   )r>   resolve_symlinksrE   rE   rF   r6   C  s
    r6   c                 C   s@   t | \}}| dr8|dd | }|dd }||fS )z,Like os.path.splitext, but take off .tar tooz.tarN)	posixpathr3   r   endswith)r>   baser   rE   rE   rF   r3   Q  s
    r3   c                 C   sx   t j|\}}|r.|r.t j|s.t | t| | t j| \}}|rt|rtzt | W n tyr   Y n0 dS )z7Like os.renames(), but handles renaming across devices.N)	r=   r>   r   r~   rM   r`   move
removedirsrN   )oldnewheadtailrE   rE   rF   r7   [  s    
r7   c                 C   sj   t | } d}t r"| t tjS ddlm} | |rb|d D ]}| t |rD dS qDdS dS dS )a  
    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.)

    Caution: this function assumes the head of path has been normalized
    with normalize_path.
    z/usrr   )distutils_schemer   TFN)r6   r   ry   rU   prefixpip._internal.locationsr   values)r>   r   r   
local_pathrE   rE   rF   is_localm  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distrE   rE   rF   dist_is_local  s    r   c                 C   s   t | ttS )zF
    Return True if given Distribution is installed in user site.
    )r   ry   r6   r   r   rE   rE   rF   dist_in_usersite  s    r   c                 C   s   t | ttS )z[
    Return True if given Distribution is installed in
    sysconfig.get_python_lib().
    )r   ry   r6   r   r   rE   rE   rF   dist_in_site_packages  s    r   c                 C   s   t t| S )zC
    Return True if given Distribution is an editable install.
    )boolegg_link_pathr   rE   rE   rF   dist_is_editable  s    r   c                    s   |rt |}nt j}| r tndd |r6dd  ndd  |rLdd ndd |r^tnd	d
  fdd|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.

    If ``paths`` is set, only report the distributions present at the
    specified list of locations.
    c                 S   s   dS NTrE   drE   rE   rF   
local_test  s    z/get_installed_distributions.<locals>.local_testc                 S   s   dS r   rE   r   rE   rE   rF   editable_test  s    z2get_installed_distributions.<locals>.editable_testc                 S   s
   t |  S Nr   r   rE   rE   rF   r     s    c                 S   s   t | S r   r   r   rE   rE   rF   editables_only_test  s    z8get_installed_distributions.<locals>.editables_only_testc                 S   s   dS r   rE   r   rE   rE   rF   r     s    c                 S   s   dS r   rE   r   rE   rE   rF   	user_test  s    z.get_installed_distributions.<locals>.user_testc                    s:   g | ]2}|r|j vr |r|r|r|qS rE   )key)r   r   r   r   r   skipr   rE   rF   r     s   
z/get_installed_distributions.<locals>.<listcomp>)r   
WorkingSetworking_setr   r   )
local_onlyr   include_editableseditables_only	user_onlypathsr   rE   r   rF   get_installed_distributions  s     

r   c                 C   s4   t | } tddddddd}dd |D }|| S )zFind a distribution matching the ``req_name`` in the environment.

    This searches from *all* distributions available in the environment, to
    match the behavior of ``pkg_resources.get_distribution()``.
    FrE   TN)r   r   r   r   r   r   c                 S   s   i | ]}t |j|qS rE   )r   r   )r   prE   rE   rF   
<dictcomp>  r   z(_search_distribution.<locals>.<dictcomp>)r   r   r   )req_namepackagespkg_dictrE   rE   rF   _search_distribution  s    
r   c                 C   s<   t | }|s4ztj|  W n tjy2   Y dS 0 t | S )zGiven a requirement name, return the installed Distribution object.

    This searches from *all* distributions available in the environment, to
    match the behavior of ``pkg_resources.get_distribution()``.
    N)r   r   r   requireDistributionNotFound)r   r   rE   rE   rF   get_distribution  s    	r   c                 C   sx   g }t  r*|t t sBtrB|t ntr8|t |t |D ],}tj|| jd }tj	|rF|  S qFd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   appendr   r   r   r=   r>   r?   project_namer   )r   sitessiteegglinkrE   rE   rF   r   +  s    




r   c                 C   s   t | }|rt|S t| jS )aO  
    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.

    The returned location is normalized (in particular, with symlinks removed).
    )r   r6   location)r   egg_linkrE   rE   rF   r   O  s    
r   c                 G   s   t j| g|R   d S r   )loggerinfo)msgargsrE   rE   rF   write_output_  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   t || _d S r   )iter_gen)selflinesrE   rE   rF   __init__g  s    zFakeFile.__init__c                 C   s&   zt | jW S  ty    Y dS 0 d S )Nr   )nextr   StopIterationr   rE   rE   rF   readlinej  s    zFakeFile.readlinec                 C   s   | j S r   )r   r   rE   rE   rF   __iter__p  s    zFakeFile.__iter__N)__name__
__module____qualname____doc__r   r   r  rE   rE   rE   rF   r   d  s   r   c                   @   s$   e Zd Zedd Zedd ZdS )StreamWrapperc                 C   s   || _ |  S r   )orig_stream)clsr  rE   rE   rF   from_streamv  s    zStreamWrapper.from_streamc                 C   s   | j jS r   )r  encodingr   rE   rE   rF   r
  |  s    zStreamWrapper.encodingN)r  r  r  classmethodr	  propertyr
  rE   rE   rE   rF   r  t  s   
r  c              
   c   sL   t t| }tt| t| zt t| V  W tt| | ntt| | 0 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)getattrrU   setattrr  r	  )stream_nameorig_stdoutrE   rE   rF   captured_output  s
    
r  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.
    stdoutr  rE   rE   rE   rF   r9     s    	r9   c                   C   s   t dS )z 
    See captured_stdout().
    stderrr  rE   rE   rE   rF   captured_stderr  s    r  c                 C   s4   t j| }|du rt  }||}|r0|jS dS )zCGet the installed version of dist_name avoiding pkg_resources cacheN)r   Requirementr   r   findversion)	dist_namer   reqr   rE   rE   rF   r;     s
    
r;   c                 C   s   t | dd dS )zConsume an iterable at C speed.r   )maxlenNr   )iteratorrE   rE   rF   consume  s    r  c                  O   sD   t t| tt| fi |}dd | D }||d< tdd|S )Nc                 S   s   i | ]\}}||qS rE   rE   )r   r   valuerE   rE   rF   r     r   zenum.<locals>.<dictcomp>reverse_mappingEnumrE   )dictziprangerJ   itemstype)
sequentialnamedenumsreverserE   rE   rF   enum  s    r*  c                 C   s*   |du r| S d| v rd | } d | |S )z.
    Build a netloc from a host-port pair
    N:[{}]z{}:{})rC   )hostportrE   rE   rF   build_netloc  s
    
r/  httpsc                 C   s4   |  ddkr(d| vr(d| vr(d| } d|| S )z)
    Build a full URL from a netloc.
    r+  rr   @[r,  z{}://{})countrC   )netlocschemerE   rE   rF   build_url_from_netloc  s    
r6  c                 C   s   t | }t|}|j|jfS )z2
    Return the host-port pair from a netloc.
    )r6  urllib_parseurlparsehostnamer.  )r4  urlparsedrE   rE   rF   parse_netloc  s    
r<  c                 C   sX   d| vr| dfS |  dd\}} d|v r6|dd}n|df}tdd |D }| |fS )zp
    Parse out and remove the auth information from a netloc.

    Returns: (netloc, (username, password)).
    r1  )NNr}   r+  Nc                 s   s"   | ]}|d u rd nt |V  qd S r   )urllib_unquote)r   xrE   rE   rF   	<genexpr>  s   z)split_auth_from_netloc.<locals>.<genexpr>)rsplitr   r   )r4  auth	user_passrE   rE   rF   split_auth_from_netloc  s    rC  c                 C   sL   t | \} \}}|du r| S |du r.d}d}nt|}d}dj||| dS )z
    Replace the sensitive data in a netloc with "****", if it exists.

    For example:
        - "user:pass@example.com" returns "user:****@example.com"
        - "accesstoken@example.com" returns "****@example.com"
    N****r   z:****z{user}{password}@{netloc})userpasswordr4  )rC  r7  quoterC   )r4  rE  rF  rE   rE   rF   redact_netloc   s    	
rH  c                 C   s@   t | }||j}|j|d |j|j|jf}t |}||fS )aR  Transform and replace netloc in a url.

    transform_netloc is a function taking the netloc and returning a
    tuple. The first element of this tuple is the new netloc. The
    entire tuple is returned.

    Returns a tuple containing the transformed url as item 0 and the
    original tuple returned by transform_netloc as item 1.
    r   )r7  urlsplitr4  r5  r>   queryfragment
urlunsplit)r:  transform_netlocpurlnetloc_tuple
url_piecessurlrE   rE   rF   _transform_url  s    



rR  c                 C   s   t | S r   )rC  r4  rE   rE   rF   _get_netloc+  s    rT  c                 C   s
   t | fS r   )rH  rS  rE   rE   rF   _redact_netloc/  s    rU  c                 C   s   t | t\}\}}|||fS )z
    Parse a url into separate netloc, auth, and url with no auth.

    Returns: (url_without_auth, netloc, (username, password))
    rR  rT  )r:  url_without_authr4  rA  rE   rE   rF   split_auth_netloc_from_url3  s    rX  c                 C   s   t | td S )z7Return a copy of url with 'username:password@' removed.r   rV  r:  rE   rE   rF   r<   >  s    r<   c                 C   s   t | td S )z.Replace the password in a given url with ****.r   )rR  rU  rY  rE   rE   rF   redact_auth_from_urlF  s    rZ  c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )
HiddenTextc                 C   s   || _ || _d S r   )secretredacted)r   r\  r]  rE   rE   rF   r   M  s    zHiddenText.__init__c                 C   s   d t| S )Nz<HiddenText {!r}>)rC   r   r   rE   rE   rF   __repr__V  s    zHiddenText.__repr__c                 C   s   | j S r   r]  r   rE   rE   rF   __str__Z  s    zHiddenText.__str__c                 C   s    t | t |krdS | j|jkS )NF)r%  r\  r   otherrE   rE   rF   __eq___  s    zHiddenText.__eq__c                 C   s
   | |k S r   rE   ra  rE   rE   rF   __ne__j  s    zHiddenText.__ne__N)r  r  r  r   r^  r`  rc  rd  rE   rE   rE   rF   r[  L  s
   	r[  c                 C   s   t | ddS )NrD  r_  )r[  )r  rE   rE   rF   
hide_valueo  s    re  c                 C   s   t | }t| |dS )Nr_  )rZ  r[  )r:  r]  rE   rE   rF   hide_urlt  s    rf  c                 C   sz   dd tjd dj tjdd  g}| oBtoBtjtjd |v }|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{}.{}.exeNrr   z-mrS   r}   z3To modify pip, please run the following command:
{}r   )rC   rU   rv   r   r=   r>   rT   rV   rW   r   r?   )modifying_pip	pip_namesshould_show_use_python_msgnew_commandrE   rE   rF   (protect_pip_from_modification_on_windowsz  s&    rk  c                   C   s   t jduot j S )z!Is this console interactive?
    N)rU   stdinisattyrE   rE   rE   rF   is_console_interactive  s    rn     c                 C   sf   t  }d}t| d8}t||dD ]}|t|7 }|| q$W d   n1 sT0    Y  ||fS )z:Return (hash, length) for path using hashlib.sha256()
    r   rb)r   N)hashlibsha256openr   rJ   update)r>   	blocksizehlengthfblockrE   rE   rF   	hash_file  s    *rz  c                  C   s&   zddl } W n ty    Y dS 0 dS )z8
    Return whether the wheel package is installed.
    r   NFT)wheelImportError)r{  rE   rE   rF   is_wheel_installed  s
    r}  c                 C   s   t | } t| | S )zb
    Return paired elements.

    For example:
        s -> (s0, s1), (s2, s3), (s4, s5), ...
    )r   r   )iterablerE   rE   rF   pairwise  s    r  c                 C   s    t |\}}t| |t| |fS )z
    Use a predicate to partition entries into false entries and true entries,
    like

        partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
    )r   r   r   )predr~  t1t2rE   rE   rF   	partition  s    r  )F)r|   )T)N)r0  )ro  )
__future__r   
contextlibrO   r   rq  iologgingr=   r   r`   rc   rU   collectionsr   	itertoolsr   pip._vendorr   pip._vendor.packaging.utilsr   Zpip._vendor.retryingr   Zpip._vendor.sixr	   r
   Zpip._vendor.six.movesr   r   r   r   r   Zpip._vendor.six.moves.urllibr   r7  Z"pip._vendor.six.moves.urllib.parser   r=  rS   r   pip._internal.exceptionsr   r   r   r   r   pip._internal.utils.compatr   r   r   r   pip._internal.utils.typingr   r   pip._internal.utils.virtualenvr   r   r   r    typingr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   Zpip._vendor.pkg_resourcesr-   r   rI   r.   __all__	getLoggerr  r   rG   rL   r:   r8   r/   ra   rq   r0   r1   r   r   r2   r   r   r4   r   r5   DEFAULT_BUFFER_SIZEr   r6   r3   r7   r   r   r   r   r   r   r   r   r   r   r   objectr   r  contextmanagerr  r9   r  r;   r  r*  r/  r6  r<  rC  rH  rR  rT  rU  rX  r<   rZ  r[  re  rf  rk  rn  rz  r}  r  r  rE   rE   rE   rF   <module>   s   8	

"

	

$
		
D$



#
