U
    `»{jo,  ã                   @  s   d Z ddlmZ ddlZddlZddlmZ ddlZddlm	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 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*ee+e,f  ƒ Z-ed Z.eej/G dd„ dƒƒƒZ0d d!œd"d#„Z1d$d%d&œd'd(„Z2d)d%d*œd+d,„Z3e!d-d.�d/dd0œd1d2„ƒZ4d/dd3d4œd5d6„Z5e!d/dd7d8œd9d:„ƒZ6d;d<œd=d>„Z7ed?d?d@�dAdBdCœdDdE„ƒZ8dS )Fz>Support for providing temporary directories to test functions.é    )ÚannotationsN)ÚPath)Úrmtree)ÚAny)ÚDict)Úfinal)Ú	Generator)ÚLiteralé   )Úcleanup_dead_symlinks)ÚLOCK_TIMEOUT)Úmake_numbered_dir)Úmake_numbered_dir_with_cleanup)Úrm_rf)Úget_user_id)ÚConfig)ÚExitCode)Úhookimpl)ÚParser)Úcheck_ispytest)Úfixture)ÚFixtureRequest)ÚMonkeyPatch)ÚItem)Ú
TestReport)ÚStashKey©ÚallÚfailedÚnonec                	   @  s¨   e Zd ZU dZded< ded< ded< ded< d	ed
< d#ddœddd	ddddœdd„Zeddœddd dœdd„ƒZdddœdd„Zd$ddddœdd„Z	dd œd!d"„Z
dS )%ÚTempPathFactoryz“Factory for temporary directories under the common base temp directory,
    as discussed at :ref:`temporary directory location and retention`.
    zPath | NoneÚ_given_basetempr   Ú_traceÚ	_basetempÚintÚ_retention_countÚRetentionTypeÚ_retention_policyNF©Ú	_ispytestÚboolÚNone)Úgiven_basetempÚretention_countÚretention_policyÚbasetempr)   Úreturnc                C  sJ   t |ƒ |d k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)   © r8   úM/var/www/html/bwave/python/venv/lib/python3.8/site-packages/_pytest/tmpdir.pyÚ__init__7   s    
zTempPathFactory.__init__r   )Úconfigr)   r0   c                C  sn   t |ƒ t| d¡ƒ}|dk r.td|› d�ƒ‚| d¡}|dk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,   r7   r-   r.   r)   )r   r$   ZgetiniÚ
ValueErrorÚoptionr/   r7   Úget)Úclsr;   r)   ÚcountÚpolicyr8   r8   r9   Úfrom_configN   s$    
ÿ

ÿ
ûzTempPathFactory.from_configr5   )Úbasenamer0   c                 C  s8   t j |¡}|  ¡ |  ¡ j|  ¡ kr4t|› d�ƒ‚|S )Nz& is not a normalized and relative path)r2   r3   ÚnormpathÚgetbasetempÚresolveÚparentr?   )r6   rF   r8   r8   r9   Ú_ensure_relative_to_basetempn   s    z,TempPathFactory._ensure_relative_to_basetempTr   )rF   Únumberedr0   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"   )r6   rF   rL   Úpr8   r8   r9   rR   t   s    
zTempPathFactory.mktemp©r0   c           	      C  sd  | j dk	r| j S | jdk	rF| j}| ¡ r0t|ƒ |jdd� | ¡ }nötj d¡}t	|p^t
 ¡ ƒ ¡ }tƒ pnd}| d|› �¡}z|jddd� W n, tk
r¾   | d	¡}|jddd� Y nX tƒ }|dk	�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k	�sNt|ƒ‚|| _ |  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   r2   ÚenvironrA   r   ÚtempfileÚ
gettempdirÚget_userrS   ÚOSErrorr   ÚstatÚst_uidÚst_modeÚchmodr%   r'   r   r   ÚAssertionErrorr"   )	r6   r/   Zfrom_envZtemprootÚuserÚrootdirÚuidZrootdir_statrZ   r8   r8   r9   rH   Œ   sP    







ÿûzTempPathFactory.getbasetemp)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r:   ÚclassmethodrE   rK   rR   rH   r8   r8   r8   r9   r    )   s    
 úøür    z
str | NonerV   c               
   C  s4   zddl } |  ¡ W S  tttfk
r.   Y 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ÚImportErrorr`   ÚKeyError)ro   r8   r8   r9   r_   È   s
    
r_   r   r+   )r;   r0   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   r8   r8   r9   Úpytest_configureÔ   s    rv   r   )Úparserr0   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   r8   r8   r9   Úpytest_addoptioná   s    ýür{   Úsession)Úscoper   )Úrequestr0   c                 C  s   | j jS )zGReturn a :class:`pytest.TempPathFactory` instance for the test session.)r;   rs   )r~   r8   r8   r9   Útmp_path_factoryð   s    r   r   )r~   Úfactoryr0   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MAXVALr8   r8   r9   Ú_mk_tmp÷   s
    r‡   zGenerator[Path])r~   r   r0   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 )a;  Return a temporary directory (as :class:`pathlib.Path` object)
    which is unique to each test function invocation.
    The temporary directory is created as a subdirectory
    of the base temporary directory, with configurable retention,
    as discussed in :ref:`temporary directory location and retention`.
    r   ÚcallT©Úignore_errorsN)
r‡   r|   r;   rs   r'   rƒ   ÚstashÚtmppath_result_keyrA   r   )r~   r   r3   rD   Zresult_dictr8   r8   r9   Útmp_pathÿ   s    


r�   zint | ExitCode)Ú
exitstatusc                 C  sb   | j j}|j}|dkrdS |j}|dkrN|dkrN|jdk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   r8   r8   r9   Úpytest_sessionfinish  s    ÿþýr�   T)ÚwrapperZtryfirstr   z'Generator[None, TestReport, TestReport])Úitemr0   c                 c  s2   d V }|j d k	st‚i }|j| j t|¡|j < |S r1   )Úwhenre   Zpassedr‹   Ú
setdefaultrŒ   )r’   rˆ   ÚrepÚemptyr8   r8   r9   Úpytest_runtest_makereport2  s
    r—   )9rl   Ú
__future__r   Údataclassesr2   Úpathlibr   r…   Úshutilr   r]   Útypingr   r   r   r   r	   r   r   r   r   r   Z_pytest.compatr   Z_pytest.configr   r   r   Z_pytest.config.argparsingr   Z_pytest.deprecatedr   Z_pytest.fixturesr   r   Z_pytest.monkeypatchr   Z_pytest.nodesr   Z_pytest.reportsr   Z_pytest.stashr   r5   r*   rŒ   r&   Ú	dataclassr    r_   rv   r{   r   r‡   r�   r�   r—   r8   r8   r8   r9   Ú<module>   s\    
