σ
ΨΖχ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-X4mzal/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-X4mzal/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-X4mzal/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-X4mzal/numpy/numpy/f2py/tests/util.pyt   wrapperJ   s    


	(   t   __name__(   R-   R/   (    (   R-   R.   s4   /tmp/pip-build-X4mzal/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-X4mzal/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-X4mzal/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-X4mzal/numpy/numpy/f2py/tests/util.pyt   _get_compiler_status―   s"    0!c           C` s   t    d S(   Ni    (   Re   (    (    (    s4   /tmp/pip-build-X4mzal/numpy/numpy/f2py/tests/util.pyt   has_c_compilerΰ   s    c           C` s   t    d S(   Ni   (   Re   (    (    (    s4   /tmp/pip-build-X4mzal/numpy/numpy/f2py/tests/util.pyt   has_f77_compilerδ   s    c           C` s   t    d S(   Ni   (   Re   (    (    (    s4   /tmp/pip-build-X4mzal/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-X4mzal/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-X4mzal/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-X4mzal/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-X4mzal/numpy/numpy/f2py/tests/util.pyt   <module>   sD   		
		:	1			H