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

numpy-svn@scip... numpy-svn@scip...
Thu Oct 18 01:54:49 CDT 2007


Author: cdavid
Date: 2007-10-18 01:54:21 -0500 (Thu, 18 Oct 2007)
New Revision: 4225

Added:
   branches/numpy.scons/numpy/distutils/scons/tools/
   branches/numpy.scons/numpy/distutils/scons/tools/__init__.py
   branches/numpy.scons/numpy/distutils/scons/tools/substinfile.py
Log:
Actually add the code for subst in file builder in scons command....

Added: branches/numpy.scons/numpy/distutils/scons/tools/__init__.py
===================================================================

Added: branches/numpy.scons/numpy/distutils/scons/tools/substinfile.py
===================================================================
--- branches/numpy.scons/numpy/distutils/scons/tools/substinfile.py	2007-10-18 06:21:48 UTC (rev 4224)
+++ branches/numpy.scons/numpy/distutils/scons/tools/substinfile.py	2007-10-18 06:54:21 UTC (rev 4225)
@@ -0,0 +1,73 @@
+# Last Change: Thu Oct 18 03:00 PM 2007 J
+
+import re
+import SCons.Action
+from SCons.Builder import Builder
+from SCons.Script import Depends
+
+def TOOL_SUBST(env):
+    """Adds SubstInFile builder, which substitutes the keys->values of SUBST_DICT
+    from the source to the target.
+    The values of SUBST_DICT first have any construction variables expanded
+    (its keys are not expanded).
+    If a value of SUBST_DICT is a python callable function, it is called and
+    the result is expanded as the value.
+    If there's more than one source and more than one target, each target gets
+    substituted from the corresponding source.
+    """
+    env.Append(TOOLS = 'SUBST')
+    def do_subst_in_file(targetfile, sourcefile, dict):
+        """Replace all instances of the keys of dict with their values.
+        For example, if dict is {'%VERSION%': '1.2345', '%BASE%': 'MyProg'},
+        then all instances of %VERSION% in the file will be replaced with 1.2345 etc.
+        """
+        try:
+            f = open(sourcefile, 'rb')
+            contents = f.read()
+            f.close()
+        except:
+            raise SCons.Errors.UserError, "Can't read source file %s"%sourcefile
+        for (k,v) in dict.items():
+            contents = re.sub(k, v, contents)
+        try:
+            f = open(targetfile, 'wb')
+            f.write(contents)
+            f.close()
+        except:
+            raise SCons.Errors.UserError, "Can't write target file %s"%targetfile
+        return 0 # success
+
+    def subst_in_file(target, source, env):
+        if not env.has_key('SUBST_DICT'):
+            raise SCons.Errors.UserError, "SubstInFile requires SUBST_DICT to be set."
+        d = dict(env['SUBST_DICT']) # copy it
+        for (k,v) in d.items():
+            if callable(v):
+                d[k] = env.subst(v())
+            elif SCons.Util.is_String(v):
+                d[k]=env.subst(v)
+            else:
+                raise SCons.Errors.UserError, "SubstInFile: key %s: %s must be a string or callable"%(k, repr(v))
+        for (t,s) in zip(target, source):
+            return do_subst_in_file(str(t), str(s), d)
+
+    def subst_in_file_string(target, source, env):
+        """This is what gets printed on the console."""
+        return '\n'.join(['Substituting vars from %s into %s'%(str(s), str(t))
+                          for (t,s) in zip(target, source)])
+
+    def subst_emitter(target, source, env):
+        """Add dependency from substituted SUBST_DICT to target.
+        Returns original target, source tuple unchanged.
+        """
+        d = env['SUBST_DICT'].copy() # copy it
+        for (k,v) in d.items():
+            if callable(v):
+                d[k] = env.subst(v())
+            elif SCons.Util.is_String(v):
+                d[k]=env.subst(v)
+        Depends(target, SCons.Node.Python.Value(d))
+        return target, source
+
+    subst_action=SCons.Action.Action(subst_in_file, subst_in_file_string)
+    env['BUILDERS']['SubstInFile'] = Builder(action=subst_action, emitter=subst_emitter)



More information about the Numpy-svn mailing list