U
    ¸mœdÍ(  ã                   @   sB  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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mZ ddlmZmZ ddlmZ dadadd	„ Zd
d„ Zdd„ Zdd„ Zeg g g dfdd„ƒZeg g g ddfdd„ƒZdadd„ Zdd„ Z dd„ Z!dd„ Z"edd„ ƒZ#G dd„ dƒZ$d d!„ Z%e
j&d"d#„ ƒZ'dS )$z¥
Utility functions for

- building and importing modules on test time, using a temporary location
- detecting if compilers are present
- determining paths to tests

é    N)ÚPath)ÚasbytesÚasstr)ÚtemppathÚIS_WASM)Úimport_modulei  c                   C   sZ   t d k	rVztj t ¡ W n tk
r,   Y nX zt t ¡ W n tk
rP   Y nX d a d S ©N)Ú_module_dirÚsysÚpathÚremoveÚ
ValueErrorÚshutilÚrmtreeÚOSError© r   r   úN/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numpy/f2py/tests/util.pyÚ_cleanup"   s    r   c                   C   s6   t d kr2t ¡ a t t¡ t tjkr2tj dt ¡ t S ©Nr   )	r	   ÚtempfileÚmkdtempÚatexitÚregisterr   r
   r   Úinsertr   r   r   r   Úget_module_dir0   s    

r   c                  C   s,   t ƒ  dt } td7 a| tjkr(tdƒ‚| S )Nz_test_ext_module_%dé   z%Temporary module name already in use.)r   Ú_module_numr
   ÚmodulesÚRuntimeError)Únamer   r   r   Úget_temp_module_name:   s    
r    c                    s   i ‰‡ ‡fdd„}ˆ j |_ |S )Nc               
      sn   t | |fƒ}|ˆkrTzˆ | |Žˆ|< W n, tk
rR } z|ˆ|< ‚ W 5 d }~X Y nX ˆ| }t|tƒrj|‚|S r   )ÚreprÚ	ExceptionÚ
isinstance)ÚaÚkwÚkeyÚeÚret©ÚfuncÚmemor   r   ÚwrapperI   s    
z_memoize.<locals>.wrapper)Ú__name__)r*   r,   r   r)   r   Ú_memoizeF   s    r.   c              
   C   sr  dt j›d�}tƒ }g }g }| D ]j}	tj |	¡s<td|	 ƒ‚tj |tj |	¡¡}
t 	|	|
¡ | 
|
¡ tj |
¡\}}|dkr | 
|
¡ q |s”t‚|dkr¢tƒ }dd|g| | }|rÆ|dg| 7 }|rØ|d	g| 7 }t ¡ }zft |¡ t jd|g| }tj|tjtjd
�}| ¡ \}}|jdk�rDtd|dd… t|ƒf ƒ‚W 5 t |¡ |D ]}	t |	¡ �qVX t|ƒS )zH
    Compile and import a f2py module, built from the given files.

    zimport sys; sys.path = z&; import numpy.f2py; numpy.f2py.main()ú%s is not a file)ú.f90ú.fz.cú.pyfNz-cz-mzskip:zonly:©ÚstdoutÚstderrr   zRunning f2py failed: %s
%sé   )r
   r   r   ÚosÚisfiler   ÚjoinÚbasenamer   ÚcopyfileÚappendÚsplitextÚAssertionErrorr    ÚgetcwdÚchdirÚunlinkÚ
executableÚ
subprocessÚPopenÚPIPEÚSTDOUTÚcommunicateÚ
returncoder   r   )Úsource_filesÚoptionsÚskipÚonlyÚmodule_nameÚcodeÚdÚdst_sourcesZf2py_sourcesÚfnÚdstÚbaseÚextZ	f2py_optsÚcwdÚcmdÚpÚoutÚerrr   r   r   Úbuild_module_   sL    

þÿ

rZ   c              
   C   sb   |dkrd}t |d��B}t|dƒ�}| | ¡ W 5 Q R X t|g||||d�W  5 Q R £ S Q R X dS )z6
    Compile and import Fortran code using f2py.

    Nr1   )ÚsuffixÚw©rJ   rK   rL   rM   )r   ÚopenÚwriterZ   )Zsource_coderJ   rK   rL   r[   rM   r   Úfr   r   r   Ú
build_codeš   s    üra   c            	   	   C   s  t d k	rt S da trt S t dttjƒ› d�¡} | tttjƒd� } t 	¡ }z`tj |d¡}t|dƒ�}| | ¡ W 5 Q R X tjddg}tj|tjtj|d�}| ¡ \}}W 5 t
 |¡ X t d	|¡}|rþtt| d
¡ƒƒtt| d¡ƒƒtt| d¡ƒƒfa t S )N)FFFz8        import os
        import sys
        sys.path = a{  

        def configuration(parent_name='',top_path=None):
            global config
            from numpy.distutils.misc_util import Configuration
            config = Configuration('', parent_name, top_path)
            return config

        from numpy.distutils.core import setup
        setup(configuration=configuration)

        config_cmd = config.get_config_cmd()
        have_c = config_cmd.try_compile('void foo() {}')
        print('COMPILERS:%%d,%%d,%%d' %% (have_c,
                                          config.have_f77c(),
                                          config.have_f90c()))
        sys.exit(99)
        )Zsyspathzsetup.pyr\   Úconfig)r4   r5   rU   s   COMPILERS:(\d+),(\d+),(\d+)r   é   é   )Ú_compiler_statusr   ÚtextwrapÚdedentr!   r
   r   Údictr   r   r   r   r7   r9   r^   r_   rB   rC   rD   rE   rF   rG   ÚreÚsearchÚboolÚintÚgroup)	rN   ZtmpdirÚscriptr`   rV   rW   rX   rY   Úmr   r   r   Ú_get_compiler_status¸   s:    ý
ýýrp   c                   C   s
   t ƒ d S r   ©rp   r   r   r   r   Úhas_c_compilerö   s    rr   c                   C   s
   t ƒ d S )Nr   rq   r   r   r   r   Úhas_f77_compilerú   s    rs   c                   C   s
   t ƒ d S )Nrc   rq   r   r   r   r   Úhas_f90_compilerþ   s    rt   c              
   K   sf  t ƒ }g }| D ]H}tj |¡s*td| ƒ‚tj |tj |¡¡}t ||¡ | 	|¡ qt
 |¡ dd¡}dttjƒ› d|› d�}tj |tƒ d ¡}	| 	|	¡ t|	dƒ�}
|
 t|ƒ¡ W 5 Q R X t ¡ }zdt |¡ tj|	d	d
g}tj|tjtjd�}| ¡ \}}|jdk�r.td|dd… t|ƒf ƒ‚W 5 t |¡ |D ]}t |¡ �q@X t|ƒ tj| S )z6
    Build a module via distutils and import it.

    r/   Ú
z
    z!
import os
import sys
sys.path = z¥

def configuration(parent_name='',top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('', parent_name, top_path)
    z…
    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(configuration=configuration)
    z.pyÚwbÚ	build_extz-ir3   r   z%Running distutils build failed: %s
%sr6   N) r   r7   r   r8   r   r9   r:   r   r;   r<   rf   rg   Úreplacer!   r
   r    r^   r_   r   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   r   Ú
__import__r   )rI   Zconfig_coderM   r%   rO   rP   rQ   rR   rN   rn   r`   rU   rV   rW   rX   rY   r   r   r   Úbuild_module_distutils  sH    ýø

þÿ

rz   c                   @   s<   e Zd ZdZdZg Zg Zg ZdZdZ	e
dd„ ƒZdd„ ZdS )ÚF2PyTestNr1   c                 C   s*   t | ƒ}d|j dd¡d › d|j› d�S )NÚ_Ú.r   éÿÿÿÿZ_ext_module)ÚtypeÚ
__module__Úrsplitr-   )ÚselfÚclsr   r   r   rM   U  s    zF2PyTest.module_namec                 C   sL  t jdkrt d¡ | jd k	r"d S tƒ s2t d¡ g }| jrH| | j¡ | jd k	r^| 	| j
¡ d}d}d}|D ]>}t|ƒ d¡r†d}qnt|ƒ d¡ršd}qnt|ƒ d¡rnd}qn|rÂtƒ sÂt d	¡ |rÖtƒ sÖt d
¡ |rðtƒ sðtƒ sðt d¡ | jd k	�rt| j| j| j| j| j
| jd�| _| jd k	�rHt| j| j| j| j| jd�| _d S )NÚwin32z)Fails with MinGW64 Gfortran (Issue #9673)zNo C compiler availableFr1   Tr0   r2   z No Fortran 77 compiler availablez No Fortran 90 compiler availablezNo Fortran compiler available)rJ   rK   rL   r[   rM   r]   )r
   ÚplatformÚpytestrK   Úmodulerr   ÚsourcesÚextendrN   r<   r[   ÚstrÚendswithrs   rt   ra   rJ   rL   rM   rZ   )r‚   ÚcodesZ	needs_f77Z	needs_f90Z	needs_pyfrQ   r   r   r   Úsetup_methodZ  sX    









ú	ûzF2PyTest.setup_method)r-   r€   Ú__qualname__rN   rˆ   rJ   rK   rL   r[   r‡   ÚpropertyrM   r�   r   r   r   r   r{   L  s   
r{   c                  G   s   t tjjƒj ¡ }|j| Ž S r   )r   ÚnumpyZf2pyÚ__file__ÚparentÚresolveÚjoinpath)r$   rO   r   r   r   Úgetpath–  s    r•   c              	   c   s.   t  ¡ }t | ¡ z
d V  W 5 t |¡ X d S r   )r   rU   r7   r@   )r   Zcurpathr   r   r   Ú	switchdirœ  s
    

r–   )(Ú__doc__r7   r
   rC   r   r   r   rf   ri   r†   Ú
contextlibr�   Úpathlibr   Znumpy.compatr   r   Znumpy.testingr   r   Ú	importlibr   r	   r   r   r   r    r.   rZ   ra   re   rp   rr   rs   rt   rz   r{   r•   Úcontextmanagerr–   r   r   r   r   Ú<module>   sR   
:û>	
DJ