B
     S]                 @   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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__ (lib/python3.7/distutils/tests/support.pyr      s
    


zLoggingSilencer.setUpc                s$   t | j | jt j_t   d S )N)r   r   r   r   r   r   r   tearDown)r   )r   r   r   r      s    
zLoggingSilencer.tearDownc             C   sT   |t ttttfk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   dd } fdd| j D S )Nc             S   s   | | S )Nr   )r    r!   r   r   r   _format)   s    z)LoggingSilencer.get_logs.<locals>._formatc                s"   g | ]\}}}| kr|| qS r   r   ).0r   r    r!   )levelsr   r   
<listcomp>+   s    z,LoggingSilencer.get_logs.<locals>.<listcomp>)r   )r   r$   r"   r   )r$   r   get_logs(   s    
zLoggingSilencer.get_logsc             C   s
   g | _ d S )N)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 )N)r   r   osgetcwdold_cwdtempdirs)r   )r   r   r   r   8   s    

zTempdirManager.setUpc                sB   t | j t   x&| jr<| j }t|t j	dk qW d S )N)ntcygwin)
r,   chdirr.   r   r   r/   popshutilZrmtreename)r   d)r   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mkdtempr/   r   )r   r6   r   r   r   r8   F   s    zTempdirManager.mkdtempxxxc             C   sB   t |ttfrtjj| }t|d}z|| W d|  X dS )zWWrites a file in the given path.


        path can be a string or a sequence.
        wN)	r   listtupler,   pathjoinopenwriteclose)r   r=   Zcontentfr   r   r   
write_fileO   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)r8   r,   r=   r>   mkdirr	   )r   pkg_namekwZtmp_dirZpkg_dirZdistr   r   r   create_dist]   s
    


zTempdirManager.create_dist)r9   )rD   )
r   r(   r)   __doc__r   r   r8   rC   rH   r*   r   r   )r   r   r+   2   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&   x |  D ]\}}t| || q
W d S )N)itemssetattr)r   kwargsrG   valr   r   r   __init__r   s    zDummyCommand.__init__c             C   s   d S )Nr   )r   r   r   r   ensure_finalizedv   s    zDummyCommand.ensure_finalizedN)r   r(   r)   rI   rO   rP   r   r   r   r   rJ   o   s   rJ   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 )N)r   rQ   r   r   r,   environold_environ)r   )r   r   r   r   |   s    zEnvironGuard.setUpc                sn   x0| j  D ]"\}}tj||kr|tj|< qW x(ttj D ]}|| j krBtj|= qBW tt| 	  d S )N)
rS   rK   r,   rR   getr<   keysr   rQ   r   )r   keyvalue)r   r   r   r      s    
zEnvironGuard.tearDown)r   r(   r)   r   r   r*   r   r   )r   r   rQ   z   s   rQ   c             C   s(   t  }|dk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SkipTestr4   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}x|D ]}tj|rJ|S qJW d S )Nsrcdirz
xxmodule.cZModulesz..)	sysconfigget_config_varr,   r=   r>   dirname__file__exists)r]   Z
candidatesr=   r   r   r   rX      s    

rX   c             C   s~   t jdkrtjd| _n`tdrztd}|dk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.
    r0   z_d.exeZPy_ENABLE_SHAREDZ	RUNSHAREDN.darwin=c             S   s   g | ]}|r|qS r   r   )r#   r6   r   r   r   r%      s    z#fixup_build_ext.<locals>.<listcomp>)r,   r5   sys
executableendswithdebugr^   r_   Zlibrary_dirsplatform	partitionsplitpathsep)cmdZ	runsharedr5   ZequalsrW   r   r   r   fixup_build_ext   s    




ro   )rI   r,   rf   r4   r7   rY   r^   rZ   r   Z	distutilsr   Zdistutils.logr   r   r   r   r   Zdistutils.corer	   objectr
   r+   rJ   rQ   r\   rX   ro   r   r   r   r   <module>   s"   #=