B
    `$                 @   s  d 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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 ddlmZ ddlmZ ddlmZ eejddG dd dZeejddG dd dZee dddZeddddZeddeeddd Zeddeedd!d"Z eeed#d$d%Z!eee	j"j#d&d'd(Z$eeeed)d*d+Z%dS ),z>Support for providing temporary directories to test functions.    N)Path)Optional   )LOCK_TIMEOUT)make_numbered_dir)make_numbered_dir_with_cleanup)rm_rf)final)Config)check_ispytest)fixture)FixtureRequest)MonkeyPatchF)initc               @   s   e Zd ZdZejee dZe Z	ejee dZ
dddee ee eddddZeddeed d	d
dZeedddZdeeedddZedddZdS )TempPathFactoryzFactory for temporary directories under the common base temp directory.

    The base directory can be configured using the ``--basetemp`` option.
    )typeNF)	_ispytest)given_basetempbasetempr   returnc            C   s>   t | |d krd | _nttjt|| _|| _|| _d S )N)	r   _given_basetempr   ospathabspathstr_trace	_basetemp)selfr   tracer   r    r   V/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/_pytest/tmpdir.py__init__#   s    zTempPathFactory.__init__)configr   r   c            C   s"   t | | |jj|jdddS )zTCreate a factory according to pytest configuration.

        :meta private:
        tmpdirT)r   r   r   )r   optionr   r   get)clsr"   r   r   r   r    from_config6   s
    
zTempPathFactory.from_config)basenamer   c             C   s8   t j|}|  |  j|  kr4t| d|S )Nz& is not a normalized and relative path)r   r   normpathgetbasetempresolveparent
ValueError)r   r(   r   r   r    _ensure_relative_to_basetempE   s    z,TempPathFactory._ensure_relative_to_basetempT)r(   numberedr   c             C   sL   |  |}|s*|  |}|jdd nt|  |dd}| d| |S )a  Create a new temporary directory managed by the factory.

        :param basename:
            Directory base name, must be a relative path.

        :param numbered:
            If ``True``, ensure the directory is unique by adding a numbered
            suffix greater than any existing one: ``basename="foo-"`` and ``numbered=True``
            means that this function will create directories named ``"foo-0"``,
            ``"foo-1"``, ``"foo-2"`` and so on.

        :returns:
            The path to the new directory.
        i  )mode)rootprefixr0   mktemp)r.   r*   joinpathmkdirr   r   )r   r(   r/   pr   r   r    r3   K   s    
zTempPathFactory.mktemp)r   c             C   s$  | j dk	r| j S | jdk	rF| j}| r0t| |jdd | }ntjd}t	|p^t
  }t pnd}|d| }|jddd ttd	r| }t }|d
kr|j|krtd| d|jd@ dkrt||jd@  td|dtdd}|dk	st||| _ | d| |S )z;Return the base temporary directory, creating it if needed.Ni  )r0   ZPYTEST_DEBUG_TEMPROOTunknownz
pytest-of-T)r0   exist_okgetuidzThe temporary directory z: is not owned by the current user. Fix this and try again.?   r   izpytest-   )r2   r1   ZkeepZlock_timeoutr0   znew basetemp)r   r   existsr   r5   r+   r   environr%   r   tempfile
gettempdirget_userr4   hasattrstatr9   st_uidOSErrorst_modechmodr   r   AssertionErrorr   )r   r   Zfrom_envZtemprootuserrootdirZrootdir_statuidr   r   r    r*   c   s@    





zTempPathFactory.getbasetemp)N)T)__name__
__module____qualname____doc__attribr   r   r   r   r   boolr!   classmethodr
   r'   r   r.   r3   r*   r   r   r   r    r      s   
r   c               @   s^   e Zd ZdZejedZddeeddddZ	de
eejjd
ddZejjdddZdS )TempdirFactoryzjBackward comptibility wrapper that implements :class:``py.path.local``
    for :class:``TempPathFactory``.)r   F)r   N)tmppath_factoryr   r   c            C   s   t | || _d S )N)r   _tmppath_factory)r   rU   r   r   r   r    r!      s    zTempdirFactory.__init__T)r(   r/   r   c             C   s   t j| j|| S )zOSame as :meth:`TempPathFactory.mktemp`, but returns a ``py.path.local`` object.)pyr   localrV   r3   r+   )r   r(   r/   r   r   r    r3      s    zTempdirFactory.mktemp)r   c             C   s   t j| j  S )z=Backward compat wrapper for ``_tmppath_factory.getbasetemp``.)rW   r   rX   rV   r*   r+   )r   r   r   r    r*      s    zTempdirFactory.getbasetemp)T)rL   rM   rN   rO   rP   rQ   r   rV   rR   r!   r   rW   r   rX   r3   r*   r   r   r   r    rT      s   
rT   )r   c           	   C   s.   ddl } y|  S  ttfk
r(   dS X dS )zlReturn the current user name, or None if getuser() does not work
    in the current environment (see #1010).r   N)getpassgetuserImportErrorKeyError)rY   r   r   r    rA      s
    rA   )r"   r   c             C   sV   t  }tj| dd}t|dd}| j|j |j| d|dd |j| d|dd dS )a  Create a TempdirFactory and attach it to the config object.

    This is to comply with existing plugins which expect the handler to be
    available at pytest_configure time, but ideally should be moved entirely
    to the tmpdir_factory session fixture.
    T)r   _tmp_path_factoryF)Zraising_tmpdirhandlerN)r   r   r'   rT   _cleanupappendZundosetattr)r"   mpZtmppath_handlertr   r   r    pytest_configure   s    rd   session)Zscope)requestr   c             C   s   | j jS )zNReturn a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.)r"   r^   )rf   r   r   r    tmpdir_factory   s    rg   c             C   s   | j jS )zOReturn a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session.)r"   r]   )rf   r   r   r    tmp_path_factory   s    rh   )rf   factoryr   c             C   s4   | j j}tdd|}d}|d | }|j|ddS )Nz[\W]_   T)r/   )nodenameresubr3   )rf   ri   rm   ZMAXVALr   r   r    _mk_tmp   s
    rp   )tmp_pathr   c             C   s   t j| S )a"  Return a temporary directory path object which is unique to each test
    function invocation, created as a sub directory of the base temporary
    directory.

    By default, a new base temporary directory is created each test session,
    and old bases are removed after 3 sessions, to aid in debugging. If
    ``--basetemp`` is used then it is cleared each session. See :ref:`base
    temporary directory`.

    The returned object is a `py.path.local`_ path object.

    .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html
    )rW   r   rX   )rq   r   r   r    r#      s    r#   )rf   rh   r   c             C   s
   t | |S )a  Return a temporary directory path object which is unique to each test
    function invocation, created as a sub directory of the base temporary
    directory.

    By default, a new base temporary directory is created each test session,
    and old bases are removed after 3 sessions, to aid in debugging. If
    ``--basetemp`` is used then it is cleared each session. See :ref:`base
    temporary directory`.

    The returned object is a :class:`pathlib.Path` object.
    )rp   )rf   rh   r   r   r    rq      s    rq   )&rO   r   rn   r?   pathlibr   typingr   rP   rW   r   r   r   r   Z_pytest.compatr	   Z_pytest.configr
   Z_pytest.deprecatedr   Z_pytest.fixturesr   r   Z_pytest.monkeypatchr   sr   rT   r   rA   rd   rg   rh   rp   r   rX   r#   rq   r   r   r   r    <module>   s@   ~