a
    ˆÏ?e¼-  ã                   @   sH  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	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rÀddlmZ ed 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!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e.e/f  ƒ Z0e!ej1G dd „ d ƒƒƒZ2ee. d!œd"d#„Z3e$dd$œd%d&„Z4edd'œd(d)„Z5e*d*d+�e+e2d,œd-d.„ƒZ6e+e2ed/œd0d1„Z7e*e+e2eeddf d2œd3d4„ƒZ8ee9e%f d5œd6d7„Z:e&d8d8d9�ed:œd;d<„ƒZ;dS )=z>Support for providing temporary directories to test functions.é    N)ÚPath)Úrmtree)ÚAny)ÚDict)Ú	Generator)ÚOptional)ÚTYPE_CHECKING)ÚUnion)ÚItem)ÚCollectReport)ÚStashKey)ÚLiteral©ÚallÚfailedÚnone)ÚParseré   )ÚLOCK_TIMEOUT)Úmake_numbered_dir)Úmake_numbered_dir_with_cleanup)Úrm_rf)Úcleanup_dead_symlinks)ÚfinalÚget_user_id)ÚConfig)ÚExitCode)Úhookimpl)Úcheck_ispytest)Úfixture)ÚFixtureRequest)ÚMonkeyPatchc                	   @   s¸   e Zd ZU dZee ed< eed< ee ed< eed< ded< dd	d
œee ed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.
    Ú_given_basetempÚ_traceÚ	_basetempÚ_retention_countÚRetentionTypeÚ_retention_policyNF©Ú	_ispytest)Úgiven_basetempÚretention_countÚretention_policyÚbasetempr*   Úreturnc                C   sJ   t |ƒ |d u rd | _nttj t|ƒ¡ƒ| _|| _|| _|| _	|| _
d S ©N)r   r#   r   ÚosÚpathÚabspathÚstrr$   r&   r(   r%   )Úselfr+   r,   r-   Útracer.   r*   © r7   úQ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/_pytest/tmpdir.pyÚ__init__;   s    
zTempPathFactory.__init__)Úconfigr*   r/   c                C   sn   t |ƒ t| d¡ƒ}|dk r.td|› d�ƒ‚| d¡}|dvrPtd|› d�ƒ‚| |jj|j d¡||d	d
�S )zTCreate a factory according to pytest configuration.

        :meta private:
        Útmp_path_retention_countr   z6tmp_path_retention_count must be >= 0. Current input: Ú.Útmp_path_retention_policyr   zKtmp_path_retention_policy must be either all, failed, none. Current input: ZtmpdirT)r+   r6   r,   r-   r*   )r   ÚintZgetiniÚ
ValueErrorÚoptionr.   r6   Úget)Úclsr:   r*   ÚcountÚpolicyr7   r7   r8   Úfrom_configR   s$    
ÿ

ÿ
ûzTempPathFactory.from_config)Úbasenamer/   c                 C   s8   t j |¡}|  ¡ |  ¡ j|  ¡ kr4t|› d�ƒ‚|S )Nz& is not a normalized and relative path)r1   r2   ÚnormpathÚgetbasetempÚresolveÚparentr?   )r5   rF   r7   r7   r8   Ú_ensure_relative_to_basetempr   s    z,TempPathFactory._ensure_relative_to_basetempT)rF   Ú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ÚprefixrO   Úmktemp)rK   rH   ÚjoinpathÚmkdirr   r$   )r5   rF   rL   Úpr7   r7   r8   rR   x   s    
zTempPathFactory.mktemp©r/   c           	      C   sb  | 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|› �¡}z|jddd� W n* ty¼   | d	¡}|jddd� Y n0 tƒ }|du�r| ¡ }|j|krðtd
|› d�ƒ‚|jd@ dk�rt ||jd@ ¡ | j}| jdk�r(d}td||tdd�}|du�sLJ |ƒ‚|| _ |  d|¡ |S )z�Return the base temporary directory, creating it if needed.

        :returns:
            The base temporary directory.
        NrM   rN   ZPYTEST_DEBUG_TEMPROOTÚunknownz
pytest-of-T)rO   Úexist_okzpytest-of-unknownzThe temporary directory z: is not owned by the current user. Fix this and try again.é?   r   iÀÿÿÿr   zpytest-)rQ   rP   ÚkeepZlock_timeoutrO   znew basetemp)r%   r#   Úexistsr   rT   rI   r1   ÚenvironrA   r   ÚtempfileÚ
gettempdirÚget_userrS   ÚOSErrorr   ÚstatÚst_uidÚst_modeÚchmodr&   r(   r   r   r$   )	r5   r.   Zfrom_envZtemprootÚuserÚrootdirÚuidZrootdir_statrZ   r7   r7   r8   rH   �   sP    







ÿûzTempPathFactory.getbasetemp)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__r   r>   Úboolr9   Úclassmethodr   rE   r4   rK   rR   rH   r7   r7   r7   r8   r"   ,   s6   
 úø÷üûr"   rV   c               	   C   s0   zddl } |  ¡ 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)ro   r7   r7   r8   r_   Ì   s
    
r_   )r:   r/   c                 C   s6   t ƒ }|  |j¡ tj| dd�}|j| d|dd� dS )a  Create a TempPathFactory 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 tmp_path_factory session fixture.
    Tr)   Ú_tmp_path_factoryF)ZraisingN)r!   Zadd_cleanupZundor"   rE   Úsetattr)r:   Úmprs   r7   r7   r8   Úpytest_configureØ   s    rv   )Úparserr/   c                 C   s$   | j dddd� | j dddd� d S )Nr;   zfHow many sessions should we keep the `tmp_path` directories, according to `tmp_path_retention_policy`.é   )ÚhelpÚdefaultr=   zvControls which directories created by the `tmp_path` fixture are kept around, based on test outcome. (all/failed/none)r   )Zaddini)rw   r7   r7   r8   Úpytest_addoptionå   s    ýür{   Úsession)Zscope)Úrequestr/   c                 C   s   | j jS )zGReturn a :class:`pytest.TempPathFactory` instance for the test session.)r:   rs   )r}   r7   r7   r8   Útmp_path_factoryô   s    r~   )r}   Úfactoryr/   c                 C   s4   | j j}t dd|¡}d}|d |… }|j|dd�S )Nz[\W]Ú_é   T)rL   )ÚnodeÚnameÚreÚsubrR   )r}   r   rƒ   ZMAXVALr7   r7   r8   Ú_mk_tmpû   s
    r†   )r}   r~   r/   c                 c   sZ   t | |ƒ}|V  | jjj}|j}| jjt }|dkrL| dd¡rLt	|dd� | jjt= dS )aU  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.
    This behavior can be configured with :confval:`tmp_path_retention_count` and
    :confval:`tmp_path_retention_policy`.
    If ``--basetemp`` is used then it is cleared each session. See :ref:`base
    temporary directory`.

    The returned object is a :class:`pathlib.Path` object.
    r   ÚcallT©Úignore_errorsN)
r†   r|   r:   rs   r(   r‚   ÚstashÚtmppath_result_keyrA   r   )r}   r~   r2   rD   Zresult_dictr7   r7   r8   Útmp_path  s    

rŒ   )Ú
exitstatusc                 C   sb   | j j}|j}|du rdS |j}|dkrN|dkrN|jdu rN| ¡ rNt|dd� | ¡ r^t|ƒ dS )z�After each session, remove base directory if all the tests passed,
    the policy is "failed", and the basetemp is not specified by a user.
    Nr   r   Trˆ   )r:   rs   r%   r(   r#   Úis_dirr   r   )r|   r�   r~   r.   rD   r7   r7   r8   Úpytest_sessionfinish%  s    ÿþýr�   T)ZtryfirstZhookwrapper)Úitemc                 c   s,   d V }|  ¡ }i }|j| j t|¡|j< d S r0   )Z
get_resultZpassedrŠ   Ú
setdefaultr‹   Úwhen)r�   r‡   ÚoutcomeÚresultÚemptyr7   r7   r8   Úpytest_runtest_makereport>  s    r–   )<rk   Zdataclassesr1   r„   r]   Úpathlibr   Úshutilr   Útypingr   r   r   r   r   r	   Z_pytest.nodesr
   Z_pytest.reportsr   Z_pytest.stashr   Ztyping_extensionsr   r'   Z_pytest.config.argparsingr   r   r   r   r   r   Z_pytest.compatr   r   Z_pytest.configr   r   r   Z_pytest.deprecatedr   Z_pytest.fixturesr   r    Z_pytest.monkeypatchr!   r4   rm   r‹   Z	dataclassr"   r_   rv   r{   r~   r†   rŒ   r>   r�   r–   r7   r7   r7   r8   Ú<module>   sd    þ!
