o
    LK&hP%                     @  sJ  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mZ ejdk r@d dlmZ nd dlZejd'd
dZej	d(d)ddZd*ddZdd ZeeeZejdd Zdd ZejejfddZejddefddZdd  ZG d!d" d"ZG d#d$ d$ejej ZG d%d& d&ej Z!dS )+    )annotationsN)Iterator)      )tarfiledirstr | os.PathLikereturnIterator[str | os.PathLike]c              	   c  s8    t  }t |  z| V  W t | dS t | w )z
    >>> tmp_path = getfixture('tmp_path')
    >>> with pushd(tmp_path):
    ...     assert os.getcwd() == os.fspath(tmp_path)
    >>> assert os.getcwd() != os.fspath(tmp_path)
    N)osgetcwdchdir)r   orig r   T/var/www/html/venv/lib/python3.10/site-packages/setuptools/_vendor/jaraco/context.pypushd   s   	
r   
target_dirstr | os.PathLike | Nonec              	   c  s    |du rt j| dddd}t | z/tj| }tj	|dd}|j
|td W d   n1 s8w   Y  |V  W t| dS t| w )a  
    Get a tarball, extract it, yield, then clean up.

    >>> import urllib.request
    >>> url = getfixture('tarfile_served')
    >>> target = getfixture('tmp_path') / 'out'
    >>> tb = tarball(url, target_dir=target)
    >>> import pathlib
    >>> with tb as extracted:
    ...     contents = pathlib.Path(extracted, 'contents.txt').read_text(encoding='utf-8')
    >>> assert not os.path.exists(extracted)
    Nz.tar.gz z.tgzzr|*)fileobjmode)pathfilter)r   r   basenamereplacemkdirurllibrequesturlopenr   open
extractallstrip_first_componentshutilrmtree)urlr   reqtfr   r   r   tarball'   s   
r'   membertarfile.TarInfoc                 C  s   | j dd\}| _ | S )N/   )namesplit)r(   r   _r   r   r   r!   G   s   r!   c                  G  s   dd }t |t| S )a  
    Compose any number of dependent context managers into a single one.

    The last, innermost context manager may take arbitrary arguments, but
    each successive context manager should accept the result from the
    previous as a single parameter.

    Like :func:`jaraco.functools.compose`, behavior works from right to
    left, so the context manager should be indicated from outermost to
    innermost.

    Example, to create a context manager to change to a temporary
    directory:

    >>> temp_dir_as_cwd = _compose(pushd, temp_dir)
    >>> with temp_dir_as_cwd() as dir:
    ...     assert os.path.samefile(os.getcwd(), dir)
    c                   s    fdd}t |S )Nc               	   ?  st     | i |(}|}|V  W d    n1 sw   Y  W d    d S W d    d S 1 s3w   Y  d S Nr   )argskwargssavedresinnerouterr   r   composedd   s   Pz/_compose.<locals>.compose_two.<locals>.composed)
contextlibcontextmanager)r5   r6   r7   r   r4   r   compose_twoc   s   
z_compose.<locals>.compose_two)	functoolsreducereversed)cmgrsr:   r   r   r   _composeO   s   r?   c               	   o  s    t jdtdd |dt}t| i |(}||}|V  W d    n1 s)w   Y  W d    d S W d    d S 1 sAw   Y  d S )NzBtarball_context is deprecated. Use tarball or tarball_cwd instead.   
stacklevelr   )warningswarnDeprecationWarningpopr   r'   )r0   r1   	pushd_ctxtballr   r   r   r   tarball_contextp   s   PrI   c                 C  s6   t jdtdd | dd }tdddd	}||dS )
a  
    Given a URL or filename, infer the compression code for tar.

    >>> infer_compression('http://foo/bar.tar.gz')
    'z'
    >>> infer_compression('http://foo/bar.tgz')
    'z'
    >>> infer_compression('file.bz')
    'j'
    >>> infer_compression('file.xz')
    'J'
    z3infer_compression is deprecated with no replacementr@   rA   NzjJ)gzbzxz)rC   rD   rE   dictget)r$   compression_indicatormappingr   r   r   infer_compression|   s   rU   c                 c  s*    t  }z
|V  W | | dS | | w )a`  
    Create a temporary directory context. Pass a custom remover
    to override the removal behavior.

    >>> import pathlib
    >>> with temp_dir() as the_dir:
    ...     assert os.path.isdir(the_dir)
    ...     _ = pathlib.Path(the_dir).joinpath('somefile').write_text('contents', encoding='utf-8')
    >>> assert not os.path.exists(the_dir)
    N)tempfilemkdtemp)removertemp_dirr   r   r   rY      s
   rY   Tc           	      c  s    d| v rdnd}| /}|d| |g}|r| d|g ttjjd}|r'|nd}tj||d |V  W d   dS 1 s>w   Y  dS )z
    Check out the repo indicated by url.

    If dest_ctx is supplied, it should be a context manager
    to yield the target directory for the check out.
    githgclonez--branchwN)stdout)extendr   r   r   devnull
subprocess
check_call)	r$   branchquietdest_ctxexerepo_dircmdr`   r^   r   r   r   repo_context   s   "ri   c                   C  s   t jdtdd t S )aD  
    A null context suitable to stand in for a meaningful context.

    >>> with null() as value:
    ...     assert value is None

    This context is most useful when dealing with two or more code
    branches but only some need a context. Wrap the others in a null
    context to provide symmetry across all options.
    z.null is deprecated. Use contextlib.nullcontextr@   rA   )rC   rD   rE   r8   nullcontextr   r   r   r   null   s   rk   c                   @  st   e Zd ZdZdZeffddZdd Zedd Z	ed	d
 Z
edd Zdd Zdd ZedddZdd ZdS )ExceptionTrapa  
    A context manager that will catch certain exceptions and provide an
    indication they occurred.

    >>> with ExceptionTrap() as trap:
    ...     raise Exception()
    >>> bool(trap)
    True

    >>> with ExceptionTrap() as trap:
    ...     pass
    >>> bool(trap)
    False

    >>> with ExceptionTrap(ValueError) as trap:
    ...     raise ValueError("1 + 1 is not 3")
    >>> bool(trap)
    True
    >>> trap.value
    ValueError('1 + 1 is not 3')
    >>> trap.tb
    <traceback object at ...>

    >>> with ExceptionTrap(ValueError) as trap:
    ...     raise Exception()
    Traceback (most recent call last):
    ...
    Exception

    >>> bool(trap)
    False
    )NNNc                 C  s
   || _ d S r/   )
exceptions)selfrm   r   r   r   __init__      
zExceptionTrap.__init__c                 C     | S r/   r   rn   r   r   r   	__enter__      zExceptionTrap.__enter__c                 C  
   | j d S Nr   exc_inforr   r   r   r   type      
zExceptionTrap.typec                 C  ru   )Nr+   rw   rr   r   r   r   value   rz   zExceptionTrap.valuec                 C  ru   )Nr@   rw   rr   r   r   r   tb   rz   zExceptionTrap.tbc                 G  s&   |d }|ot || j}|r|| _|S rv   )
issubclassrm   rx   )rn   rx   ry   matchesr   r   r   __exit__  s
   zExceptionTrap.__exit__c                 C  s
   t | jS r/   )boolry   rr   r   r   r   __bool__  rp   zExceptionTrap.__bool___testc                  s   t  fdd}|S )a  
        Wrap func and replace the result with the truth
        value of the trap (True if an exception occurred).

        First, give the decorator an alias to support Python 3.8
        Syntax.

        >>> raises = ExceptionTrap(ValueError).raises

        Now decorate a function that always fails.

        >>> @raises
        ... def fail():
        ...     raise ValueError('failed')
        >>> fail()
        True
        c                    sF   t j}| i | W d     |S 1 sw   Y   |S r/   )rl   rm   )r0   r1   trapr   funcrn   r   r   wrapper!  s   
z%ExceptionTrap.raises.<locals>.wrapper)r;   wraps)rn   r   r   r   r   r   r   raises  s   zExceptionTrap.raisesc                 C  s   | j |tjdS )a  
        Wrap func and replace the result with the truth
        value of the trap (True if no exception).

        First, give the decorator an alias to support Python 3.8
        Syntax.

        >>> passes = ExceptionTrap(ValueError).passes

        Now decorate a function that always fails.

        >>> @passes
        ... def fail():
        ...     raise ValueError('failed')

        >>> fail()
        False
        r   )r   operatornot_)rn   r   r   r   r   passes)  s   zExceptionTrap.passesN)__name__
__module____qualname____doc__rx   	Exceptionro   rs   propertyry   r{   r|   r   r   r   r   r   r   r   r   r   rl      s    !


rl   c                   @  s   e Zd ZdZdS )suppressz
    A version of contextlib.suppress with decorator support.

    >>> @suppress(KeyError)
    ... def key_error():
    ...     {}['']
    >>> key_error()
    N)r   r   r   r   r   r   r   r   r   ?  s    r   c                   @  s*   e Zd ZdZdddZdd Zdd	 Zd
S )on_interrupta  
    Replace a KeyboardInterrupt with SystemExit(1)

    >>> def do_interrupt():
    ...     raise KeyboardInterrupt()
    >>> on_interrupt('error')(do_interrupt)()
    Traceback (most recent call last):
    ...
    SystemExit: 1
    >>> on_interrupt('error', code=255)(do_interrupt)()
    Traceback (most recent call last):
    ...
    SystemExit: 255
    >>> on_interrupt('suppress')(do_interrupt)()
    >>> with __import__('pytest').raises(KeyboardInterrupt):
    ...     on_interrupt('ignore')(do_interrupt)()
    errorr+   c                C  s   || _ || _d S r/   )actioncode)rn   r   r   r   r   r   ro   ]  s   
zon_interrupt.__init__c                 C  rq   r/   r   rr   r   r   r   rs   a  rt   zon_interrupt.__enter__c                 C  s6   |t us	| jdkrd S | jdkrt| j|| jdkS )Nignorer   r   )KeyboardInterruptr   
SystemExitr   )rn   exctypeexcinstexctbr   r   r   r   d  s
   

zon_interrupt.__exit__N)r   r+   )r   r   r   r   ro   rs   r   r   r   r   r   r   J  s
    
r   )r   r   r	   r
   r/   )r   r   r	   r
   )r(   r)   r	   r)   )"
__future__r   r8   r;   r   r   r"   ra   sysrV   urllib.requestr   rC   typingr   version_info	backportsr   r9   r   r'   r!   r?   tarball_cwdrI   rU   r#   rY   ri   rk   rl   r   ContextDecoratorr   r   r   r   r   <module>   sD    



q