ó
ÿ¼÷Xc           @` s´  d  Z  d d l m Z m Z m 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 m Z d d l Z d d l m Z m Z y d d l m Z Wn! e k
 rý d d l m Z n Xd a d „  Z d	 „  Z d
 „  Z d „  Z e g  g  g  d d „ ƒ Z  e g  g  g  d d d „ ƒ Z! d a" d „  Z# d „  Z$ d „  Z% d „  Z& e d „  ƒ Z' d e( f d „  ƒ  YZ) d S(   sˆ   
Utility functions for

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

i    (   t   divisiont   absolute_importt   print_functionN(   t   asbytest   asstr(   t   SkipTestt   temppath(   t   md5(   t   newc           C` sl   t  d  k	 rh y t j j t  ƒ Wn t k
 r3 n Xy t j t  ƒ Wn t t	 f k
 r^ n Xd  a  n  d  S(   N(
   t   _module_dirt   Nonet   syst   patht   removet
   ValueErrort   shutilt   rmtreet   IOErrort   OSError(    (    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   _cleanup$   s    c           C` sQ   t  d  k rM t j ƒ  a  t j t ƒ t  t j k rM t j j	 d t  ƒ qM n  t  S(   Ni    (
   R	   R
   t   tempfilet   mkdtempt   atexitt   registerR   R   R   t   insert(    (    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   get_module_dir2   s    c          C` s|   t  ƒ  }  x` t d d ƒ D]O } d | } t j j |  | ƒ } | t j k r t j j | d ƒ r | Sq Wt d ƒ ‚ d  S(   Ni  i–˜ s   _test_ext_module_%ds   .pys(   Failed to create a temporary module name(	   R   t   ranget   osR   t   joinR   t   modulest   isfilet   RuntimeError(   t   dt   jt   namet   fn(    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   get_temp_module_name<   s    	
&c         ` s(   i  ‰ ‡  ‡ f d †  } ˆ  j  | _  | S(   Nc          ` s   t  |  | f ƒ } | ˆ k r[ y ˆ  |  | Ž  ˆ | <Wq[ t k
 rW } | ˆ | <‚  q[ Xn  ˆ | } t | t ƒ r} | ‚ n  | S(   N(   t   reprt	   Exceptiont
   isinstance(   t   at   kwt   keyt   et   ret(   t   funct   memo(    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   wrapperJ   s    


	(   t   __name__(   R-   R/   (    (   R-   R.   s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   _memoizeG   s    c         C` sQ  d t  t j ƒ } t ƒ  } g  } xí |  D]å } t j j | ƒ sT t d | ƒ ‚ n  t j j | t j j | ƒ ƒ }	 t	 j
 | |	 ƒ | j |	 ƒ t j j t j j | ƒ d ƒ } t j j | ƒ r) t j j | t j j | ƒ ƒ }	 t j j |	 ƒ st	 j
 | |	 ƒ qq) q) W| d k r*t ƒ  } n  d d | g | | }
 | r[|
 d g | 7}
 n  | ru|
 d g | 7}
 n  t j ƒ  } zŒ t j | ƒ t j d | g |
 } t j | d t j d	 t j ƒ} | j ƒ  \ } } | j d
 k rt d | d t | ƒ f ƒ ‚ n  Wd t j | ƒ x | D] } t j | ƒ q$WXt | ƒ t j | S(   sH   
    Compile and import a f2py module, built from the given files.

    sE   import sys; sys.path = %s; import numpy.f2py as f2py2e; f2py2e.main()s   %s is not a files   .f2py_f2cmaps   -cs   -ms   skip:s   only:t   stdoutt   stderri    s   Running f2py failed: %s
%si   N(   R%   R   R   R   R   R   R   R   t   basenameR   t   copyfilet   appendt   dirnameR
   R$   t   getcwdt   chdirt
   executablet
   subprocesst   Popent   PIPEt   STDOUTt   communicatet
   returncodeR   t   unlinkt
   __import__R   (   t   source_filest   optionst   skipt   onlyt   module_namet   codeR    t   dst_sourcesR#   t   dstt	   f2py_optst   cwdt   cmdt   pt   outt   err(    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   build_module^   sJ    	!!!!
c         C` s{   | d k r d } n  t d | ƒ Q } t | d ƒ  } | j |  ƒ Wd QXt | g d | d | d | d | ƒSWd QXd S(	   s6   
    Compile and import Fortran code using f2py.

    s   .ft   suffixt   wNRD   RE   RF   RG   (   R
   R   t   opent   writeRQ   (   t   source_codeRD   RE   RF   RR   RG   R   t   f(    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt
   build_code™   s    	c          C` s6  t  d  k	 r t  St t t f a  d }  |  t d t t j ƒ ƒ }  t d d ƒ q } t | d ƒ  } | j	 |  ƒ Wd  QXt j
 | d g } t j | d t j d t j ƒ} | j ƒ  \ } } Wd  QXt j t d	 ƒ | ƒ } | r2t t | j d
 ƒ ƒ ƒ t t | j d ƒ ƒ ƒ t t | j d ƒ ƒ ƒ f a  n  t  S(   Ns7  
import os
import sys
sys.path = %(syspath)s

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)
t   syspathRR   s   .pyRS   t   configR2   R3   s   COMPILERS:(\d+),(\d+),(\d+)i   i   i   (   t   _compiler_statusR
   t   Falset   dictR%   R   R   R   RT   RU   R:   R;   R<   R=   R>   R?   t   ret   searchR   t   boolt   intt   group(   RH   t   scriptRW   RM   RN   RO   RP   t   m(    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   _get_compiler_status¯   s"    0!c           C` s   t  ƒ  d S(   Ni    (   Re   (    (    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   has_c_compilerà   s    c           C` s   t  ƒ  d S(   Ni   (   Re   (    (    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   has_f77_compilerä   s    c           C` s   t  ƒ  d S(   Ni   (   Re   (    (    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   has_f90_compilerè   s    c         K` s  d d l  m } d d l m } t ƒ  } g  } xq |  D]i } t j j | ƒ sa t d | ƒ ‚ n  t j j	 | t j j
 | ƒ ƒ }	 t j | |	 ƒ | j |	 ƒ q6 Wt j | ƒ j d d ƒ } d t d | d	 t t j ƒ ƒ }
 t j j	 | t ƒ  d
 ƒ } | j | ƒ t | d ƒ } | j t |
 ƒ ƒ | j ƒ  t j ƒ  } z‹ t j | ƒ t j | d d g } t j | d t j d t j ƒ} | j  ƒ  \ } } | j! d k rËt d | d t" | ƒ f ƒ ‚ n  Wd t j | ƒ x | D] } t j# | ƒ qãWXt$ | ƒ t j% | S(   s6   
    Build a module via distutils and import it.

    i    (   t   Configuration(   t   setups   %s is not a files   
s   
    s`  import os
import sys
sys.path = %(syspath)s

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

if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(configuration=configuration)
t   config_codeRY   s   .pyt   wbt	   build_exts   -iR2   R3   s%   Running distutils build failed: %s
%si   N(&   t   numpy.distutils.misc_utilRi   t   numpy.distutils.coreRj   R   R   R   R   R   R   R4   R   R5   R6   t   textwrapt   dedentt   replaceR]   R%   R   R$   RT   RU   R   t   closeR8   R9   R:   R;   R<   R=   R>   R?   R@   R   RA   RB   R   (   RC   Rk   RG   R)   Ri   Rj   R    RI   R#   RJ   RH   Rc   RW   RL   RM   RN   RO   RP   (    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   build_module_distutilsð   s@    	!"
!
t   F2PyTestc           B` sA   e  Z d Z d Z g  Z g  Z g  Z d  Z d Z	 d Z
 d „  Z RS(   s   .fc         C` s—  |  j  d  k	 r d  St ƒ  s+ t d ƒ ‚ n  g  } |  j rM | j |  j ƒ n  |  j d  k	 ro | j |  j ƒ n  t	 } t	 } x> | D]6 } | j
 d ƒ r  t } q‚ | j
 d ƒ r‚ t } q‚ q‚ W| rÛ t ƒ  rÛ t d ƒ ‚ n  | rú t ƒ  rú t d ƒ ‚ n  |  j d  k	 rKt |  j d |  j d |  j d |  j d	 |  j d
 |  j ƒ|  _  n  |  j d  k	 r“t |  j d |  j d |  j d |  j d
 |  j ƒ|  _  n  d  S(   Ns   No C compiler availables   .fs   .f90s    No Fortran 77 compiler availables    No Fortran 90 compiler availableRD   RE   RF   RR   RG   (   t   moduleR
   Rf   R   t   sourcest   extendRH   R6   RR   R\   t   endswitht   TrueRg   Rh   RX   RD   RE   RF   RG   RQ   (   t   selft   codest	   needs_f77t	   needs_f90R#   (    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   setUpB  s:    				N(   R0   t
   __module__R
   RH   Rw   RD   RE   RF   RR   Rv   RG   R   (    (    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyRu   8  s   (*   t   __doc__t
   __future__R    R   R   R   R   R;   R   R   R   Rp   R^   t   randomt   numpy.compatR   R   t
   numpy.f2pyt   numpyt   numpy.testingR   R   t   hashlibR   t   ImportErrorR   R
   R	   R   R   R$   R1   RQ   RX   R[   Re   Rf   Rg   Rh   Rt   t   objectRu   (    (    (    s4   /tmp/pip-build-2Vcwy8/numpy/numpy/f2py/tests/util.pyt   <module>   sD   		
		:	1			H