[Numpy-svn] r4473 - in branches/numpy.scons/numpy/distutils/scons: core tools

numpy-svn@scip... numpy-svn@scip...
Tue Nov 20 02:12:41 CST 2007


Author: cdavid
Date: 2007-11-20 02:12:09 -0600 (Tue, 20 Nov 2007)
New Revision: 4473

Added:
   branches/numpy.scons/numpy/distutils/scons/core/template_generators.py
   branches/numpy.scons/numpy/distutils/scons/tools/npyftpl.py
Modified:
   branches/numpy.scons/numpy/distutils/scons/core/custom_builders.py
   branches/numpy.scons/numpy/distutils/scons/core/numpyenv.py
   branches/numpy.scons/numpy/distutils/scons/tools/npyctpl.py
Log:
Adding from_template scons builders

Modified: branches/numpy.scons/numpy/distutils/scons/core/custom_builders.py
===================================================================
--- branches/numpy.scons/numpy/distutils/scons/core/custom_builders.py	2007-11-20 05:28:48 UTC (rev 4472)
+++ branches/numpy.scons/numpy/distutils/scons/core/custom_builders.py	2007-11-20 08:12:09 UTC (rev 4473)
@@ -35,6 +35,7 @@
 
     In particular, it does not install .exp/.lib files on windows. """
     source = [pjoin(env['build_dir'], i) for i in source]
+
     # XXX: why target is a list ? It is always true ?
     # XXX: handle cases where SHLIBPREFIX is in args
     lib = env.SharedLibrary("$build_dir/%s" % target[0], 
@@ -46,3 +47,26 @@
     inst_lib = env.Install("$distutils_installdir", lib)
     return lib, inst_lib
 
+def NumpyFromCTemplate(env, target, source, *args, **kw):
+    source = [pjoin(env['build_dir'], i) for i in source]
+
+    # XXX: why target is a list ? It is always true ?
+    # XXX: handle cases where SHLIBPREFIX is in args
+    src = env.FromCTemplate("$build_dir/%s" % target[0], 
+                            source, *args, **kw)
+
+    #inst_src = env.Install("$distutils_installdir", src)
+    #return src, inst_src
+    return src
+
+def NumpyFromFTemplate(env, target, source, *args, **kw):
+    source = [pjoin(env['build_dir'], i) for i in source]
+
+    # XXX: why target is a list ? It is always true ?
+    # XXX: handle cases where SHLIBPREFIX is in args
+    src = env.FromFTemplate("$build_dir/%s" % target[0], 
+                            source, *args, **kw)
+
+    #inst_src = env.Install("$distutils_installdir", src)
+    #return src, inst_src
+    return src

Modified: branches/numpy.scons/numpy/distutils/scons/core/numpyenv.py
===================================================================
--- branches/numpy.scons/numpy/distutils/scons/core/numpyenv.py	2007-11-20 05:28:48 UTC (rev 4472)
+++ branches/numpy.scons/numpy/distutils/scons/core/numpyenv.py	2007-11-20 08:12:09 UTC (rev 4473)
@@ -186,6 +186,7 @@
     from SCons.Tool import Tool, FindTool, FindAllTools
     from SCons.Script import BuildDir, Help
     from SCons.Errors import EnvironmentError
+    from SCons.Builder import Builder
 
     # XXX: I would prefer subclassing Environment, because we really expect
     # some different behaviour than just Environment instances...
@@ -250,20 +251,22 @@
         pass
         #print "===== BOOTSTRAPPING, f2py scons tool not available (%s) =====" % e
 
-    t = Tool('npyctpl', 
-             toolpath = [os.path.dirname(numpy.distutils.scons.tools.__file__)])
-    try:
-        t(env)
-    except Exception, e:
-        pass
+    # XXX: understand how registration of source files work before reenabling those
 
-    t = Tool('npyftpl', 
-             toolpath = [os.path.dirname(numpy.distutils.scons.tools.__file__)])
-    try:
-        t(env)
-    except Exception, e:
-        pass
+    # t = Tool('npyctpl', 
+    #          toolpath = [os.path.dirname(numpy.distutils.scons.tools.__file__)])
+    # try:
+    #     t(env)
+    # except Exception, e:
+    #     pass
 
+    # t = Tool('npyftpl', 
+    #          toolpath = [os.path.dirname(numpy.distutils.scons.tools.__file__)])
+    # try:
+    #     t(env)
+    # except Exception, e:
+    #     pass
+
     finalize_env(env)
 
     # Add the tool paths in the environment
@@ -309,12 +312,29 @@
         pass
 
     # Adding custom builder
+
     # XXX: Put them into tools ?
     env['BUILDERS']['NumpySharedLibrary'] = NumpySharedLibrary
     env['BUILDERS']['NumpyCtypes'] = NumpyCtypes
     env['BUILDERS']['PythonExtension'] = PythonExtension
     env['BUILDERS']['NumpyPythonExtension'] = NumpyPythonExtension
 
+    from template_generators import generate_from_c_template, \
+                                    generate_from_f_template, \
+                                    generate_from_template_emitter
+
+    env['BUILDERS']['FromCTemplate'] = Builder(
+                action = generate_from_c_template, 
+                emitter = generate_from_template_emitter)
+
+    env['BUILDERS']['FromFTemplate'] = Builder(
+                action = generate_from_f_template, 
+                emitter = generate_from_template_emitter)
+
+    from custom_builders import NumpyFromCTemplate, NumpyFromFTemplate
+    env['BUILDERS']['NumpyFromCTemplate'] = NumpyFromCTemplate
+    env['BUILDERS']['NumpyFromFTemplate'] = NumpyFromFTemplate
+
     # Setting build directory according to command line option
     if len(env['src_dir']) > 0:
         BuildDir(env['build_dir'], env['src_dir'])

Added: branches/numpy.scons/numpy/distutils/scons/core/template_generators.py
===================================================================
--- branches/numpy.scons/numpy/distutils/scons/core/template_generators.py	2007-11-20 05:28:48 UTC (rev 4472)
+++ branches/numpy.scons/numpy/distutils/scons/core/template_generators.py	2007-11-20 08:12:09 UTC (rev 4473)
@@ -0,0 +1,40 @@
+from os.path import basename as pbasename, splitext, join as pjoin, dirname as pdirname
+
+from numpy.distutils.conv_template import process_str as c_process_str
+from numpy.distutils.from_template import process_str as f_process_str
+
+def do_generate_from_c_template(targetfile, sourcefile, env):
+    t = open(targetfile, 'w')
+    s = open(sourcefile, 'r')
+    allstr = s.read()
+    s.close()
+    writestr = c_process_str(allstr)
+    t.write(writestr)
+    t.close()
+    return 0
+
+def do_generate_from_f_template(targetfile, sourcefile, env):
+    t = open(targetfile, 'w')
+    s = open(sourcefile, 'r')
+    allstr = s.read()
+    s.close()
+    writestr = f_process_str(allstr)
+    t.write(writestr)
+    t.close()
+    return 0
+
+def generate_from_c_template(target, source, env):
+    for t, s in zip(target, source):
+        do_generate_from_c_template(str(t), str(s), env)
+    return 0
+
+def generate_from_f_template(target, source, env):
+    for t, s in zip(target, source):
+        do_generate_from_f_template(str(t), str(s), env)
+    return 0
+
+def generate_from_template_emitter(target, source, env):
+    base, ext = splitext(pbasename(str(source[0])))
+    t = pjoin(pdirname(str(target[0])), base)
+    return ([t], source)
+    

Modified: branches/numpy.scons/numpy/distutils/scons/tools/npyctpl.py
===================================================================
--- branches/numpy.scons/numpy/distutils/scons/tools/npyctpl.py	2007-11-20 05:28:48 UTC (rev 4472)
+++ branches/numpy.scons/numpy/distutils/scons/tools/npyctpl.py	2007-11-20 08:12:09 UTC (rev 4473)
@@ -1,4 +1,4 @@
-"""npytpl Tool
+"""npyctpl Tool
 
 Tool-specific initialization for npyctpl, a tool to generate C source file from
 .c.src files.
@@ -44,27 +44,17 @@
     """Add Builders and construction variables for npytpl to an Environment."""
     c_file, cxx_file = SCons.Tool.createCFileBuilders(env)
 
-    #c_file.suffix['.src'] = _generate_from_template_emitter
-
     c_file.add_action('.c.src', SCons.Action.Action(_generate_from_template))
     c_file.add_emitter('.c.src', _generate_from_template_emitter)
 
-    env['NPYTPLOPTIONS']      = SCons.Util.CLVar('')
-    #env['NPYTPLBUILDDIR']     = ''
-    #env['NPYTPLCFILESUFFIX']  = 'module$CFILESUFFIX'
-    #env['NPYTPLINCLUDEDIR']   = os.path.join(d, 'src')
+    env['NPYCTPLOPTIONS']     = SCons.Util.CLVar('')
 
-    # # XXX: adding a scanner using c_file.add_scanner does not work...
-    # expr = '(<)include_file=(\S+)>'
-    # scanner = SCons.Scanner.ClassicCPP("F2PYScan", ".pyf", "F2PYPATH", expr)
-    # env.Append(SCANNERS = scanner)
-
 def exists(env):
     try:
         import numpy.distutils.conv_template
         st = 1
     except ImportError, e:
-        print "Warning : npytpl tool not found, error was %s" % e
+        print "Warning : npyctpl tool not found, error was %s" % e
         st = 0
 
     return st

Copied: branches/numpy.scons/numpy/distutils/scons/tools/npyftpl.py (from rev 4472, branches/numpy.scons/numpy/distutils/scons/tools/npyctpl.py)
===================================================================
--- branches/numpy.scons/numpy/distutils/scons/tools/npyctpl.py	2007-11-20 05:28:48 UTC (rev 4472)
+++ branches/numpy.scons/numpy/distutils/scons/tools/npyftpl.py	2007-11-20 08:12:09 UTC (rev 4473)
@@ -0,0 +1,60 @@
+"""npyftpl Tool
+
+Tool-specific initialization for npyftpl, a tool to generate fortran/f2py
+source file from .xxx.src where xxx is f, f90 or pyf.
+
+There normally shouldn't be any need to import this module directly.
+It will usually be imported through the generic SCons.Tool.Tool()
+selection method.
+
+"""
+
+from os.path import basename as pbasename, splitext, join as pjoin, \
+                    dirname as pdirname
+
+import SCons.Action
+#import SCons.Defaults
+import SCons.Scanner
+import SCons.Tool
+
+from numpy.distutils.from_template import process_str
+
+# XXX: this is general and can be used outside numpy.core.
+def _do_generate_from_template(targetfile, sourcefile, env):
+    t = open(targetfile, 'w')
+    s = open(sourcefile, 'r')
+    allstr = s.read()
+    s.close()
+    writestr = process_str(allstr)
+    t.write(writestr)
+    t.close()
+    return 0
+
+def _generate_from_template(target, source, env):
+    for t, s in zip(target, source):
+        _do_generate_from_template(str(t), str(s), env)
+    return 0
+
+def _generate_from_template_emitter(target, source, env):
+    base, ext = splitext(pbasename(str(source[0])))
+    t = pjoin(pdirname(str(target[0])), base)
+    return ([t], source)
+    
+def generate(env):
+    """Add Builders and construction variables for npytpl to an Environment."""
+    f_file = SCons.Builder.Builder(action = {}, emitter = {}, suffix = {None: ['.f']})
+
+    f_file.add_action('.f.src', SCons.Action.Action(_generate_from_template))
+    f_file.add_emitter('.f.src', _generate_from_template_emitter)
+
+    env['NPYFTPLOPTIONS']     = SCons.Util.CLVar('')
+
+def exists(env):
+    try:
+        import numpy.distutils.from_template
+        st = 1
+    except ImportError, e:
+        print "Warning : npyftpl tool not found, error was %s" % e
+        st = 0
+
+    return st



More information about the Numpy-svn mailing list