a
    zeN                     @   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	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)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.9/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                    s8   t | j t   | jr4| j }tj	| qd S r   )
r0   chdirr2   r   r   r3   poptestsupport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mkdtempr3   r!   )r   dr   r   r   r:   E   s    zTempdirManager.mkdtempxxxc                 C   sJ   t |ttfrtjj| }t|d}z|| W |  n
|  0 dS )zWWrites a file in the given path.


        path can be a string or a sequence.
        wN)	r   listtupler0   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.
        )attrs)r:   r0   r@   rA   mkdirr	   )r   Zpkg_namekwZtmp_dirZpkg_dirdistr   r   r   create_dist\   s
    


zTempdirManager.create_dist)r<   )rG   )
r    r,   r-   __doc__r   r   r:   rF   rL   r.   r   r   r   r   r/   1   s   		
r/   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   kwargsrJ   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-   rM   rS   rT   r   r   r   r   rN   n   s   rN   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   rU   r   r   r0   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   )
rW   rO   r0   rV   getr?   keysr   rU   r   )r   keyvaluer   r   r   r      s    

zEnvironGuard.tearDown)r    r,   r-   r   r   r.   r   r   r   r   rU   y   s   rU   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    
ra   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|rH|  S qHd S )Nsrcdirz
xxmodule.cZModulesz..)	sysconfigget_config_varr0   r@   rA   dirname__file__exists)rb   Z
candidatesr@   r   r   r   r\      s    

r\   c                 C   s~   t jdkrtjd| _n`tdrztd}|du r@dg| _n:tj	dkrRg | _n(|
d\}}}d	d
 |t jD | _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(          z#fixup_build_ext.<locals>.<listcomp>)r0   namesys
executableendswithdebugrc   rd   Zlibrary_dirsplatform	partitionsplitpathsep)cmdZ	runsharedrm   Zequalsr[   r   r   r   fixup_build_ext   s    




rw   )rM   r0   rn   r^   r9   r]   rc   r_   r   Ztest.supportr6   Z	distutilsr   Zdistutils.logr   r   r   r   r   Zdistutils.corer	   objectr
   r/   rN   rU   ra   r\   rw   r   r   r   r   <module>   s$   !=