o
    JAfZ                     @   s   d 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 ddl	m
Z
 ddlmZ ddlmZmZmZmZmZ ddlmZ G dd	 d	eZG d
d deZG dd dZG dd deZdd Zdd Zdd ZdS )z&Support code for distutils test cases.    N)deepcopy)	os_helper)log)DEBUGINFOWARNERRORFATAL)Distributionc                       s@   e Zd Z fddZ fddZdd Zdd Zd	d
 Z  ZS )LoggingSilencerc                    s6   t    ttj| _tjj| _| jtj_g | _	d S N)
supersetUpr   set_thresholdr	   	thresholdLog_log_old_loglogsself	__class__ ./usr/lib/python3.10/distutils/tests/support.pyr      s
   



zLoggingSilencer.setUpc                    s$   t | j | jt j_t   d S r   )r   r   r   r   r   r   r   tearDownr   r   r   r   r      s   
zLoggingSilencer.tearDownc                 C   sT   |t ttttfvrtdt| t|tstdt	|j
 | j|||f d S )Nz%s wrong log levelzmsg should be str, not '%.200s')r   r   r   r   r	   
ValueErrorstr
isinstance	TypeErrortype__name__r   append)r   levelmsgargsr   r   r   r   !   s   
zLoggingSilencer._logc                    s    fdd| j D S )Nc                    s"   g | ]\}}}| v r|| qS r   r   ).0r#   r$   r%   levelsr   r   
<listcomp>*   s    z,LoggingSilencer.get_logs.<locals>.<listcomp>r   )r   r(   r   r'   r   get_logs)   s   
zLoggingSilencer.get_logsc                 C   s
   g | _ d S r   r*   r   r   r   r   
clear_logs-   s   
zLoggingSilencer.clear_logs)	r!   
__module____qualname__r   r   r   r+   r,   __classcell__r   r   r   r   r      s    
r   c                       sH   e Zd ZdZ fddZ fddZdd Zdd	d
ZdddZ  Z	S )TempdirManagerz}Mix-in class that handles temporary directories for test cases.

    This is intended to be used with unittest.TestCase.
    c                    s   t    t | _g | _d S r   )r   r   osgetcwdold_cwdtempdirsr   r   r   r   r   7   s   


zTempdirManager.setUpc                    s>   t | j t   | jr| j }t| | jsd S d S r   )	r1   chdirr3   r   r   r4   popr   rmtree)r   Ztmpdirr   r   r   r   <   s   


zTempdirManager.tearDownc                 C   s   t  }| j| |S )zjCreate a temporary directory that will be cleaned up.

        Returns the path of the directory.
        )tempfilemkdtempr4   r"   )r   dr   r   r   r9   E   s   zTempdirManager.mkdtempxxxc                 C   sH   t |ttfrtjj| }t|d}z|| W |  dS |  w )zWWrites a file in the given path.


        path can be a string or a sequence.
        wN)	r   listtupler1   pathjoinopenwriteclose)r   r?   Zcontentfr   r   r   
write_fileN   s   
zTempdirManager.write_filefooc                 K   s2   |   }tj||}t| t|d}||fS )a  Will generate a test environment.

        This function creates:
         - a Distribution instance using keywords
         - a temporary directory with a package structure

        It returns the package directory and the distribution
        instance.
        )Zattrs)r9   r1   r?   r@   mkdirr
   )r   Zpkg_namekwZtmp_dirZpkg_dirdistr   r   r   create_dist\   s
   


zTempdirManager.create_dist)r;   )rF   )
r!   r-   r.   __doc__r   r   r9   rE   rJ   r/   r   r   r   r   r0   1   s    	
	r0   c                   @   s    e Zd ZdZdd Zdd ZdS )DummyCommandzAClass to store options for retrieval via set_undefined_options().c                 K   s"   |  D ]
\}}t| || qd S r   )itemssetattr)r   kwargsrH   valr   r   r   __init__q   s   zDummyCommand.__init__c                 C   s   d S r   r   r   r   r   r   ensure_finalizedu   s   zDummyCommand.ensure_finalizedN)r!   r-   r.   rK   rQ   rR   r   r   r   r   rL   n   s    rL   c                       s(   e Zd Z fddZ fddZ  ZS )EnvironGuardc                    s   t t|   ttj| _d S r   )r   rS   r   r   r1   environold_environr   r   r   r   r   {   s   zEnvironGuard.setUpc                    sf   | j  D ]\}}tj||kr|tj|< qttj D ]}|| j vr)tj|= qtt| 	  d S r   )
rU   rM   r1   rT   getr>   keysr   rS   r   )r   keyvaluer   r   r   r      s   

zEnvironGuard.tearDown)r!   r-   r.   r   r   r/   r   r   r   r   rS   y   s    rS   c                 C   s(   t  }|du rtdt||  dS )ak  Helper for tests that need the xxmodule.c source file.

    Example use:

        def test_compile(self):
            copy_xxmodule_c(self.tmpdir)
            self.assertIn('xxmodule.c', os.listdir(self.tmpdir))

    If the source file can be found, it will be copied to *directory*.  If not,
    the test will be skipped.  Errors during copy are not caught.
    Nz>cannot find xxmodule.c (test must run in the python build dir))_get_xxmodule_pathunittestZSkipTestshutilcopy)Z	directoryfilenamer   r   r   copy_xxmodule_c   s   
r_   c               
   C   sf   t d} tjtjtdtj| ddtj| dddddg}|D ]}tj|r0|  S q$d S )Nsrcdirz
xxmodule.cZModulesz..)	sysconfigget_config_varr1   r?   r@   dirname__file__exists)r`   Z
candidatesr?   r   r   r   rZ      s   

rZ   c                 C   s   t jdkrtjd| _dS tdrBtd}|du r"dg| _dS tj	dkr,g | _dS |
d\}}}d	d
 |t jD | _dS dS )a  Function needed to make build_ext tests pass.

    When Python was built with --enable-shared on Unix, -L. is not enough to
    find libpython<blah>.so, because regrtest runs in a tempdir, not in the
    source directory where the .so lives.

    When Python was built with in debug mode on Windows, build_ext commands
    need their debug attribute set, and it is not done automatically for
    some reason.

    This function handles both of these things.  Example use:

        cmd = build_ext(dist)
        support.fixup_build_ext(cmd)
        cmd.ensure_finalized()

    Unlike most other Unix platforms, Mac OS X embeds absolute paths
    to shared libraries into executables, so the fixup is not needed there.
    ntz_d.exeZPy_ENABLE_SHAREDZ	RUNSHAREDN.darwin=c                 S   s   g | ]}|r|qS r   r   )r&   r:   r   r   r   r)      s    z#fixup_build_ext.<locals>.<listcomp>)r1   namesys
executableendswithdebugra   rb   Zlibrary_dirsplatform	partitionsplitpathsep)cmdZ	runsharedrj   ZequalsrY   r   r   r   fixup_build_ext   s   




rt   )rK   r1   rk   r\   r8   r[   ra   r]   r   Ztest.supportr   Z	distutilsr   Zdistutils.logr   r   r   r   r	   Zdistutils.corer
   objectr   r0   rL   rS   r_   rZ   rt   r   r   r   r   <module>   s&    !=