<s> import os . path as op <EOL> import re <EOL> import numpy . f2py <EOL> import numpy . distutils . misc_util <EOL> from waflib import Task <EOL> from waflib . TaskGen import extension , feature , before_method , after_method <EOL> from interface_gen import generate_interface <EOL> CGEN_TEMPLATE = '<STR_LIT>' <EOL> FOBJECT_FILE = '<STR_LIT>' <EOL> FHEADER_FILE = '<STR_LIT>' <EOL> FWRAP_TEMPLATE = '<STR_LIT>' <EOL> F2PY_TEMP_DIR = '<STR_LIT>' <EOL> F2PY_MODNAME_MATCH = re . compile ( r'<STR_LIT>' , <EOL> re . I ) . match <EOL> F2PY_UMODNAME_MATCH = re . compile ( r'<STR_LIT>' '<STR_LIT>' , re . I ) . match <EOL> F2PY_INCLUDE_MATCH = re . compile ( r'<STR_LIT>' , re . I ) <EOL> def is_pyf ( node ) : <EOL> return node . name . endswith ( "<STR_LIT>" ) <EOL> def include_pyf ( node ) : <EOL> includes = [ ] <EOL> for line in node . read ( ) . splitlines ( ) : <EOL> m = F2PY_INCLUDE_MATCH . match ( line ) <EOL> if m : <EOL> includes . append ( m . groupdict ( ) [ "<STR_LIT:name>" ] ) <EOL> return includes <EOL> def f2py_modulename ( node ) : <EOL> """<STR_LIT>""" <EOL> name = None <EOL> for line in node . read ( ) . splitlines ( ) : <EOL> m = F2PY_MODNAME_MATCH ( line ) <EOL> if m : <EOL> if F2PY_UMODNAME_MATCH ( line ) : <EOL> continue <EOL> name = m . group ( '<STR_LIT:name>' ) <EOL> break <EOL> return name <EOL> def generate_fake_interface ( name , node ) : <EOL> """<STR_LIT>""" <EOL> content = """<STR_LIT>""" <EOL> node . write ( content % { "<STR_LIT:name>" : name } ) <EOL> @ feature ( '<STR_LIT>' ) <EOL> @ before_method ( '<STR_LIT>' ) <EOL> def apply_f2py_includes ( task_gen ) : <EOL> includes = task_gen . env [ "<STR_LIT>" ] <EOL> d = op . dirname ( numpy . f2py . __file__ ) <EOL> includes . append ( op . join ( d , '<STR_LIT:src>' ) ) <EOL> includes . extend ( numpy . distutils . misc_util . get_numpy_include_dirs ( ) ) <EOL> task_gen . env [ "<STR_LIT>" ] = includes <EOL> @ feature ( '<STR_LIT>' ) <EOL> @ before_method ( '<STR_LIT>' ) <EOL> def add_fortran_tasks ( task_gen ) : <EOL> assert "<STR_LIT>" in task_gen . features <EOL> if hasattr ( task_gen , "<STR_LIT:name>" ) : <EOL> module_name = task_gen . name <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % task_gen ) <EOL> if len ( task_gen . source ) < <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> sources = task_gen . to_nodes ( task_gen . source ) <EOL> module_node = sources [ <NUM_LIT:0> ] . parent . find_or_declare ( "<STR_LIT>" % module_name ) <EOL> f2py_task = task_gen . create_task ( '<STR_LIT>' , sources , module_node ) <EOL> add_f2py_tasks ( task_gen , f2py_task , module_name , module_node ) <EOL> f2py_task . env . F2PYFLAGS . extend ( [ "<STR_LIT>" , "<STR_LIT>" , module_name ] ) <EOL> def fake_interface_gen_callback ( task_gen , node ) : <EOL> return _interface_gen_callback ( task_gen , node , "<STR_LIT>" ) <EOL> def interface_gen_callback ( task_gen , node ) : <EOL> return _interface_gen_callback ( task_gen , node , "<STR_LIT>" ) <EOL> def _interface_gen_callback ( task_gen , node , interface_task_name ) : <EOL> if not hasattr ( task_gen , "<STR_LIT:name>" ) : <EOL> module_name = f2py_modulename ( node ) <EOL> else : <EOL> module_name = task_gen . name <EOL> intermediate_output = node . parent . find_or_declare ( "<STR_LIT>" % module_name ) <EOL> module_node = node . parent . find_or_declare ( "<STR_LIT>" % module_name ) <EOL> assert intermediate_output . is_bld ( ) <EOL> assert module_node . is_bld ( ) <EOL> interface_task = task_gen . create_task ( interface_task_name , node , intermediate_output ) <EOL> f2py_task = task_gen . create_task ( '<STR_LIT>' , intermediate_output , module_node ) <EOL> add_f2py_tasks ( task_gen , f2py_task , module_name , module_node ) <EOL> def add_f2py_tasks ( task_gen , f2py_task , module_name , module_node ) : <EOL> build_dir = module_node . parent . bldpath ( ) <EOL> f2py_task . env . F2PYFLAGS = task_gen . env . F2PYFLAGS [ : ] <EOL> f2py_task . env . F2PYFLAGS . extend ( [ "<STR_LIT>" , build_dir ] ) <EOL> task_gen . source += f2py_task . outputs <EOL> fobject_node = module_node . parent . find_or_declare ( "<STR_LIT>" % module_name ) <EOL> fobject_task = task_gen . create_task ( "<STR_LIT>" , [ ] , fobject_node ) <EOL> task_gen . source += fobject_task . outputs <EOL> fwrapper_node = module_node . parent . find_or_declare ( FWRAP_TEMPLATE % module_name ) <EOL> fwrapper_task = task_gen . create_task ( "<STR_LIT>" , [ ] , fwrapper_node ) <EOL> task_gen . source += fwrapper_task . outputs <EOL> @ extension ( '<STR_LIT>' ) <EOL> def add_f2py_files ( task_gen , node ) : <EOL> ext = '<STR_LIT>' <EOL> if not is_pyf ( node ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not hasattr ( task_gen , "<STR_LIT:name>" ) : <EOL> module_name = f2py_modulename ( node ) <EOL> else : <EOL> module_name = task_gen . name <EOL> module_node = node . parent . find_or_declare ( "<STR_LIT>" % module_name ) <EOL> f2py_task = task_gen . create_task ( '<STR_LIT>' , node , module_node ) <EOL> add_f2py_tasks ( task_gen , f2py_task , module_name , module_node ) <EOL> @ extension ( '<STR_LIT>' ) <EOL> def add_f2py_files ( task_gen , node ) : <EOL> ext = '<STR_LIT>' <EOL> if not hasattr ( task_gen , "<STR_LIT:name>" ) : <EOL> module_name = f2py_modulename ( node ) <EOL> else : <EOL> module_name = task_gen . name <EOL> intermediate_output = node . parent . find_or_declare ( "<STR_LIT>" % module_name ) <EOL> assert intermediate_output . is_bld ( ) <EOL> if "<STR_LIT>" in task_gen . features : <EOL> interface_task = task_gen . create_task ( "<STR_LIT>" , node , intermediate_output ) <EOL> elif "<STR_LIT>" in task_gen . features : <EOL> interface_task = task_gen . create_task ( "<STR_LIT>" , node , intermediate_output ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> task_gen . source += interface_task . outputs <EOL> class _f2py_interface ( Task . Task ) : <EOL> pass <EOL> class f2py_fake_interface ( _f2py_interface ) : <EOL> def run ( self ) : <EOL> node = self . inputs [ <NUM_LIT:0> ] <EOL> output = self . outputs [ <NUM_LIT:0> ] <EOL> module_name = f2py_modulename ( node ) <EOL> generate_fake_interface ( module_name , output ) <EOL> return <NUM_LIT:0> <EOL> class f2py_interface ( _f2py_interface ) : <EOL> def run ( self ) : <EOL> node = self . inputs [ <NUM_LIT:0> ] <EOL> output = self . outputs [ <NUM_LIT:0> ] <EOL> module_name = f2py_modulename ( node ) <EOL> generate_interface ( module_name , node . abspath ( ) , output . abspath ( ) ) <EOL> return <NUM_LIT:0> <EOL> def scan ( self ) : <EOL> found = [ ] <EOL> missing = [ ] <EOL> node = self . inputs [ <NUM_LIT:0> ] <EOL> if not hasattr ( self , "<STR_LIT:name>" ) : <EOL> module_name = f2py_modulename ( node ) <EOL> else : <EOL> module_name = self . name <EOL> includes = include_pyf ( node ) <EOL> if includes : <EOL> real_pyf = node . parent . find_or_declare ( "<STR_LIT>" % module_name ) <EOL> assert real_pyf . is_bld ( ) <EOL> for inc in includes : <EOL> x = node . parent . find_resource ( inc ) <EOL> if x : <EOL> found . append ( x ) <EOL> else : <EOL> missing . append ( x ) <EOL> user_routines = node . parent . find_resource ( "<STR_LIT>" % module_name ) <EOL> if user_routines : <EOL> found . append ( user_routines ) <EOL> return ( found , missing ) <EOL> class f2py_fortran_object ( Task . Task ) : <EOL> def run ( self ) : <EOL> node = self . generator . bld . bldnode . find_node ( op . join ( F2PY_TEMP_DIR , FOBJECT_FILE ) ) <EOL> output = self . outputs [ <NUM_LIT:0> ] <EOL> output . write ( node . read ( ) ) <EOL> return <NUM_LIT:0> <EOL> class f2py_fwrapper ( Task . Task ) : <EOL> after = [ "<STR_LIT>" ] <EOL> def run ( self ) : <EOL> output = self . outputs [ <NUM_LIT:0> ] <EOL> if not op . exists ( output . abspath ( ) ) : <EOL> output . write ( "<STR_LIT>" ) <EOL> return <NUM_LIT:0> <EOL> class _f2py_task ( Task . Task ) : <EOL> run_str = '<STR_LIT>' <EOL> color = '<STR_LIT>' <EOL> class f2py ( _f2py_task ) : <EOL> ext_in = [ "<STR_LIT>" ] <EOL> ext_out = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class f2py_fortran ( _f2py_task ) : <EOL> ext_in = [ "<STR_LIT>" ] <EOL> ext_out = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def configure ( conf ) : <EOL> if not conf . env . CC and not conf . env . CXX : <EOL> conf . fatal ( '<STR_LIT>' ) <EOL> if not conf . env . PYTHON : <EOL> conf . fatal ( '<STR_LIT>' ) <EOL> conf . find_program ( '<STR_LIT>' , var = '<STR_LIT>' ) <EOL> conf . env . F2PYFLAGS = [ "<STR_LIT>" ] <EOL> f2py_tempdir = conf . bldnode . make_node ( F2PY_TEMP_DIR ) <EOL> f2py_tempdir . mkdir ( ) <EOL> fobject = f2py_tempdir . make_node ( FOBJECT_FILE ) <EOL> d = op . dirname ( numpy . f2py . __file__ ) <EOL> source_c = op . join ( d , '<STR_LIT:src>' , FOBJECT_FILE ) <EOL> fobject . write ( open ( source_c ) . read ( ) ) </s>
