o
    Ô­jŸ+  ã                   @   sx  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ZddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZ dadaejd	krieeƒjjjZee d
¡ƒZ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)Úasstr)Ú	asunicode)ÚtemppathÚIS_WASM)Úimport_modulei  Úcygwinz**/*.dllc                   C   sZ   t d ur+ztj t ¡ W n	 ty   Y nw zt t ¡ W n	 ty&   Y nw d a d S d S ©N)Ú_module_dirÚsysÚpathÚremoveÚ
ValueErrorÚshutilÚrmtreeÚOSError© r   r   úR/var/www/html/CropPilot/venv/lib/python3.10/site-packages/numpy/f2py/tests/util.pyÚ_cleanup(   s   ÿÿ÷r   c                   C   s6   t d u rt ¡ a t t¡ t tjvrtj dt ¡ t S ©Nr   )	r
   ÚtempfileÚmkdtempÚatexitÚregisterr   r   r   Úinsertr   r   r   r   Úget_module_dir6   s   

r   c                  C   s,   t ƒ  dt } td7 a| tjv 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               
      sh   t | |fƒ}|ˆvr'zˆ | i |¤Žˆ|< W n ty& } z|ˆ|< ‚ d }~ww ˆ| }t|tƒr2|‚|S r	   )ÚreprÚ	ExceptionÚ
isinstance)ÚaÚkwÚkeyÚeÚret©ÚfuncÚmemor   r   ÚwrapperO   s   €þ
z_memoize.<locals>.wrapper)Ú__name__)r+   r-   r   r*   r   Ú_memoizeL   s   r/   c              
   C   sÈ  dt j›d�}tƒ }g }g }| D ]5}	tj |	¡std|	 ƒ‚tj |tj |	¡¡}
t 	|	|
¡ | 
|
¡ tj |
¡\}}|dv rE| 
|
¡ q|sJJ ‚|du rQtƒ }dd|g| | }|rc|dg| 7 }|rl|d	g| 7 }t ¡ }zAt |¡ t jd|g| }tj|tjtjd
�}| ¡ \}}|jdkr¡td|dd… t|ƒf ƒ‚W t |¡ |D ]}	t |	¡ q©nt |¡ |D ]}	t |	¡ q¹w t jdkràt t tj |d |¡¡¡¡ t g d¢t ¡ 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   z{:s}*)z/usr/bin/rebasez
--databasez--obliviousz	--verbose)r   r   r   ÚosÚisfiler   ÚjoinÚbasenamer   ÚcopyfileÚappendÚsplitextr!   ÚgetcwdÚchdirÚ
executableÚ
subprocessÚPopenÚPIPEÚSTDOUTÚcommunicateÚ
returncoder   ÚunlinkÚplatformÚ_module_listÚextendÚglobÚformatÚ
check_callr   )Úsource_filesÚoptionsÚskipÚonlyÚmodule_nameÚcodeÚdÚdst_sourcesÚf2py_sourcesÚfnÚdstÚbaseÚextÚ	f2py_optsÚcwdÚcmdÚpÚoutÚerrr   r   r   Úbuild_modulee   sl   

€
þ
ÿÿ
ÿ
ýÿ
ÿÿÿrb   c              	   C   s‚   |du rd}t |d��,}t|dƒ�}| | ¡ W d  ƒ n1 s!w   Y  t|g||||d�W  d  ƒ S 1 s:w   Y  dS )z6
    Compile and import Fortran code using f2py.

    Nr2   )ÚsuffixÚw©rP   rQ   rR   rS   )r   ÚopenÚwriterb   )Úsource_coderP   rQ   rR   rc   rS   r   Úfr   r   r   Ú
build_code¯   s   ÿü$ýrj   c            	   	   C   s   t d u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 d   ƒ n1 sCw   Y  tjddg}tj|tjtj|d�}| ¡ \}}W t |¡ nt |¡ w 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)
        )Úsyspathzsetup.pyrd   Úconfig)r5   r6   r]   s   COMPILERS:(\d+),(\d+),(\d+)r   é   é   )Ú_compiler_statusr   ÚtextwrapÚdedentr"   r   r   Údictr   r   r8   r:   rf   rg   rA   rB   rC   rD   rE   rF   r   r   ÚreÚsearchÚboolÚintÚgroup)	rT   ÚtmpdirÚscriptri   r^   r_   r`   ra   Úmr   r   r   Ú_get_compiler_statusÍ   s<   
ýÿýýr{   c                   C   ó
   t ƒ d S r   ©r{   r   r   r   r   Úhas_c_compiler  ó   
r~   c                   C   r|   )Nr   r}   r   r   r   r   Úhas_f77_compiler  r   r€   c                   C   r|   )Nrm   r}   r   r   r   r   Úhas_f90_compiler  r   r�   c              
   K   s–  t ƒ }g }| D ]$}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ƒ�}
|
 | d	¡¡ W d
  ƒ n1 shw   Y  t ¡ }z@t |¡ tj|	ddg}tj|tjtjd�}| ¡ \}}|jdkr¡td|dd
… t|ƒf ƒ‚W t |¡ |D ]}t |¡ q©nt |¡ |D ]}t |¡ q¹w t|ƒ tj| S )z6
    Build a module via distutils and import it.

    r0   Ú
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Úlatin1NÚ	build_extz-ir4   r   z%Running distutils build failed: %s
%sr7   ) r   r8   r   r9   r   r:   r;   r   r<   r=   rp   rq   Úreplacer"   r   r!   rf   rg   Úencoder?   r@   rA   rB   rC   rD   rE   rF   rG   r   rH   Ú
__import__r   )rO   Úconfig_coderS   r&   rU   rV   rX   rY   rT   ry   ri   r]   r^   r_   r`   ra   r   r   r   Úbuild_module_distutils  sV   ýø
ÿ
þ
ÿÿ
ÿ
ýÿ
rŠ   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 )ÚF2PyTestNr2   c                 C   s*   t | ƒ}d|j dd¡d › d|j› d�S )NÚ_Ú.r   éÿÿÿÿÚ_ext_module)ÚtypeÚ
__module__Úrsplitr.   )ÚselfÚclsr   r   r   rS   j  s   "zF2PyTest.module_namec                 C   sL  t jdkr
t d¡ | jd urd S tƒ st d¡ g }| jr$| | j¡ | jd ur/| 	| j
¡ d}d}d}|D ]}t|ƒ d¡rCd}q7t|ƒ d¡rMd}q7t|ƒ d¡rVd}q7|ratƒ sat d	¡ |rktƒ skt d
¡ |rxtƒ sxtƒ sxt d¡ | jd urŽt| j| j| j| j| j
| jd�| _| jd ur¤t| j| j| j| j| jd�| _d S d S )NÚwin32z)Fails with MinGW64 Gfortran (Issue #9673)zNo C compiler availableFr2   Tr1   r3   z No Fortran 77 compiler availablez No Fortran 90 compiler availablezNo Fortran compiler available)rP   rQ   rR   rc   rS   re   )r   rI   ÚpytestrQ   Úmoduler~   ÚsourcesrK   rT   r=   rc   ÚstrÚendswithr€   r�   rj   rP   rR   rS   rb   )r“   ÚcodesÚ	needs_f77Ú	needs_f90Ú	needs_pyfrX   r   r   r   Úsetup_methodo  s\   




€





ú
	ûÿzF2PyTest.setup_method)r.   r‘   Ú__qualname__rT   r˜   rP   rQ   rR   rc   r—   ÚpropertyrS   rŸ   r   r   r   r   r‹   a  s    
r‹   c                  G   s   t tjjƒj ¡ }|j| Ž S r	   )r   ÚnumpyÚf2pyÚ__file__ÚparentÚresolveÚjoinpath)r%   rU   r   r   r   Úgetpath«  s   
r¨   c              	   c   s8   � t  ¡ }t | ¡ zd V  W t |¡ d S t |¡ w r	   )r   r]   r8   r@   )r   Úcurpathr   r   r   Ú	switchdir±  s   €
rª   )0Ú__doc__rL   r8   r   rB   r   r   r   rp   rs   r–   Ú
contextlibr¢   Úpathlibr   Únumpy.compatr   Únumpy._utilsr   Únumpy.testingr   r   Ú	importlibr   r
   r   rI   r¤   r¥   ÚNUMPY_INSTALL_ROOTÚlistrJ   r   r   r!   r/   rb   rj   ro   r{   r~   r€   r�   rŠ   r‹   r¨   Úcontextmanagerrª   r   r   r   r   Ú<module>   s^    

Iû>	
DJ