a
    /§»aá$  ã                   @   sš  d 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	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 )ÚTempPathFactoryz—Factory 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 u rd | _nttj t|ƒ¡ƒ| _|| _|| _d S ©N)	r   Ú_given_basetempr   ÚosÚpathÚabspathÚstrÚ_traceÚ	_basetemp)Úselfr   Útracer   r   © r"   úH/home/yuhw/wc/pochoir/venv/lib/python3.9/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_config7   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_basetempF   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.
        éÀ  ©Úmode)ÚrootÚprefixr6   Úmktemp)r1   r-   ÚjoinpathÚmkdirr   r   )r    r+   r3   Úpr"   r"   r#   r9   L   s    
zTempPathFactory.mktemp©r   c                 C   s$  | j dur| j S | jdurF| j}| ¡ r0t|ƒ |jdd� | ¡ }n¶tj d¡}t	|p^t
 ¡ ƒ ¡ }tƒ pnd}| d|› �¡}|jddd� tjd	krêt ¡ }| ¡ }|d
krê|j|krÊtd|› d�ƒ‚|jd@ dkrêt ||jd@ ¡ td|dtdd�}|du�sJ |ƒ‚|| _ |  d|¡ |S )z;Return the base temporary directory, creating it if needed.Nr4   r5   ZPYTEST_DEBUG_TEMPROOTÚunknownz
pytest-of-T)r6   Úexist_okÚwin32éÿÿÿÿzThe temporary directory z: is not owned by the current user. Fix this and try again.é?   r   iÀÿÿÿzpytest-é   )r8   r7   ZkeepZlock_timeoutr6   znew basetemp)r   r   Úexistsr   r;   r.   r   Úenvironr(   r   ÚtempfileÚ
gettempdirÚget_userr:   ÚsysÚplatformÚgetuidÚstatÚst_uidÚOSErrorÚst_modeÚchmodr   r   r   )r    r   Zfrom_envZtemprootÚuserÚrootdirÚuidZrootdir_statr"   r"   r#   r-   d   sD    






ÿûzTempPathFactory.getbasetemp)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚattrÚibr   r   r   r   r   Úboolr$   Úclassmethodr
   r*   r   r1   r9   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   Fr   N)Útmppath_factoryr   r   c                C   s   t |ƒ || _d S r   )r   Ú_tmppath_factory)r    r]   r   r"   r"   r#   r$   Ÿ   s    zTempdirFactory.__init__Tr2   c                 C   s   t j | j ||¡ ¡ ¡S )zOSame as :meth:`TempPathFactory.mktemp`, but returns a ``py.path.local`` object.)Úpyr   Úlocalr^   r9   r.   )r    r+   r3   r"   r"   r#   r9   ¥   s    zTempdirFactory.mktempr=   c                 C   s   t j | j ¡  ¡ ¡S )z=Backward compat wrapper for ``_tmppath_factory.getbasetemp``.)r_   r   r`   r^   r-   r.   )r    r"   r"   r#   r-   ©   s    zTempdirFactory.getbasetemp)T)rT   rU   rV   rW   rX   rY   r   r^   rZ   r$   r   r_   r   r`   r9   r-   r"   r"   r"   r#   r\   —   s   ÿþr\   r=   c               	   C   s0   ddl } z
|  ¡ W S  ttfy*   Y dS 0 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)ra   r"   r"   r#   rH   ®   s
    
rH   )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.
    Tr   Ú_tmp_path_factoryF)ZraisingÚ_tmpdirhandlerN)r   r   r*   r\   Ú_cleanupÚappendZundoÚsetattr)r%   ÚmpZtmppath_handlerÚtr"   r"   r#   Úpytest_configure¹   s    rl   Úsession)Zscope)Úrequestr   c                 C   s   | j jS )zNReturn a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.)r%   rf   ©rn   r"   r"   r#   Útmpdir_factoryÈ   s    rp   c                 C   s   | j jS )zOReturn a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session.)r%   re   ro   r"   r"   r#   Útmp_path_factoryÏ   s    rq   )rn   Úfactoryr   c                 C   s4   | j j}t dd|¡}d}|d |… }|j|dd�S )Nz[\W]Ú_é   T)r3   )ÚnodeÚnameÚreÚsubr9   )rn   rr   rv   ZMAXVALr"   r"   r#   Ú_mk_tmpÖ   s
    ry   )Ú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
    )r_   r   r`   )rz   r"   r"   r#   r&   Þ   s    r&   )rn   rq   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.
    )ry   )rn   rq   r"   r"   r#   rz   ð   s    rz   )'rW   r   rw   rI   rF   Úpathlibr   Útypingr   rX   r_   r   r   r   r   Z_pytest.compatr	   Z_pytest.configr
   Z_pytest.deprecatedr   Z_pytest.fixturesr   r   Z_pytest.monkeypatchr   Úsr   r\   r   rH   rl   rp   rq   ry   r   r`   r&   rz   r"   r"   r"   r#   Ú<module>   sF   
}
