Fix problems with Python callable objects as Builder actions, the associated test...
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Fri, 7 Dec 2001 00:29:20 +0000 (00:29 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Fri, 7 Dec 2001 00:29:20 +0000 (00:29 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@131 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/engine/SCons/Builder.py
src/engine/SCons/BuilderTests.py
src/engine/SCons/EnvironmentTests.py
src/engine/SCons/Script.py
test/Command.py
test/actions.py

index 2d2d6234387bbeb7f58bc9c47d601ebe536c0e6b..983e32f471ee58d3f58d57391f1e23079856ac52 100644 (file)
@@ -350,7 +350,8 @@ class ActionBase:
             if type(t) is type(""):
                 t = [t]
             dict['TARGETS'] = PathList(map(os.path.normpath, t))
-            dict['TARGET'] = dict['TARGETS'][0]
+           if dict['TARGETS']:
+                dict['TARGET'] = dict['TARGETS'][0]
         if kw.has_key('source'):
             s = kw['source']
             del kw['source']
@@ -407,8 +408,7 @@ class FunctionAction(ActionBase):
        # if print_actions:
        # XXX:  WHAT SHOULD WE PRINT HERE?
        if execute_actions:
-            dict = apply(self.subst_dict, (), kw)
-            return apply(self.function, (), dict)
+            return apply(self.function, (), kw)
 
     def get_contents(self, **kw):
         """Return the signature contents of this callable action.
index 54776ffdbe0653938bec95a76039113a74fca481..4c6ad1f4134afd38b0ec0fe34e05cff7b2109440 100644 (file)
@@ -190,11 +190,11 @@ class BuilderTestCase(unittest.TestCase):
         assert show_string == expect7, show_string
 
        def function1(**kw):
-           open(kw['out'], 'w').write("function1\n")
+           open(kw['target'], 'w').write("function1\n")
            return 1
 
        builder = SCons.Builder.Builder(action = function1)
-       r = builder.execute(out = outfile)
+       r = builder.execute(target = outfile)
        assert r == 1
        c = test.read(outfile, 'r')
        assert c == "function1\n", c
index 3341c8325501f86a716e201727a9b5c12b6d7083..82b19bef268487f2de79575e7f7c08abb5b8d47b 100644 (file)
@@ -234,11 +234,17 @@ class EnvironmentTestCase(unittest.TestCase):
         assert 'foo1.in' in map(lambda x: x.path, t.sources)
         assert 'foo2.in' in map(lambda x: x.path, t.sources)
 
-        def testFunc(ENV, target, source):
+        def testFunc(env, target, source):
             assert target == 'foo.out'
-            assert source == 'foo1.in foo2.in' or source == 'foo2.in foo1.in'
-        env.Command(target='foo.out', source=['foo1.in','foo2.in'],
-                    action=testFunc)
+            assert 'foo1.in' in source and 'foo2.in' in source, source
+            return 0
+        t = env.Command(target='foo.out', source=['foo1.in','foo2.in'],
+                        action=testFunc)
+        assert t.builder
+        assert t.builder.action.__class__.__name__ == 'FunctionAction'
+        t.build()
+        assert 'foo1.in' in map(lambda x: x.path, t.sources)
+        assert 'foo2.in' in map(lambda x: x.path, t.sources)
 
     def test_subst(self):
        """Test substituting construction variables within strings
index 3857df6de6f15825d30d7d1729f3140d6f31cdb8..836fb1aeeb69507388f427b89c208ae60928dd6e 100644 (file)
@@ -80,7 +80,7 @@ class BuildTask(SCons.Taskmaster.Task):
             try:
                 self.target.build()
             except BuildError, e:
-                sys.stderr.write("scons: *** [%s] Error %d\n" % (e.node, e.stat))
+                sys.stderr.write("scons: *** [%s] Error %s\n" % (e.node, str(e.stat)))
                 raise
 
     def failed(self):
index 2b704fc1363141badaabd42486c01e0fbce8e0a8..244b78cb37309a87a7fc71ce0972b0a6d882c770 100644 (file)
@@ -40,16 +40,22 @@ file.close()
 """)
 
 test.write('SConstruct', """
+def buildIt(env, target, source):
+    contents = open(source[0], 'rb').read()
+    file = open(target, 'wb')
+    file.write(contents)
+    file.close()
+    return 0
+
 env = Environment()
 env.Command(target = 'f1.out', source = 'f1.in',
-            action = r'%s build.py $TARGET $SOURCES')
+            action = buildIt)
 env.Command(target = 'f2.out', source = 'f2.in',
             action = r'%s' + " build.py temp2 $SOURCES\\n" + r'%s' + " build.py $TARGET temp2")
 env.Command(target = 'f3.out', source = 'f3.in',
             action = [r'%s build.py temp3 $SOURCES',
                       r'%s build.py $TARGET temp3'])
-# Eventually, add ability to do execute Python code.
-""" % (python, python, python, python, python))
+""" % (python, python, python, python))
 
 test.write('f1.in', "f1.in\n")
 
index cee067e9088163f89bf08f54c4e9da2520d49ace..9ba689b94387a452105c3d0f0d131420c652d583 100644 (file)
@@ -68,16 +68,17 @@ test.up_to_date(arguments = '.')
 
 test.write('SConstruct', """
 import os
-import SCons.Util
-def func(**kw):
-    cmd = SCons.Util.scons_subst(r'%s build.py $TARGET 3 $SOURCES', kw, {})
+import string
+def func(env, target, source):
+    cmd = r'%s build.py %%s 3 %%s' %% (target, string.join(source, ' '))
+    print cmd
     return os.system(cmd)
 B = Builder(name = 'B', action = func)
 env = Environment(BUILDERS = [B])
 env.B(target = 'foo.out', source = 'foo.in')
 """ % python)
 
-test.run(arguments = '.')
+test.run(arguments = '.', stderr = None)
 
 test.fail_test(test.read('foo.out') != "3\nfoo.in\n")
 
@@ -85,16 +86,15 @@ test.up_to_date(arguments = '.')
 
 test.write('SConstruct', """
 import os
-import SCons.Util
 class bld:
     def __init__(self):
-        self.cmd = r'%s build.py $TARGET 4 $SOURCES'
-    def __call__(self, **kw):
-        cmd = SCons.Util.scons_subst(self.cmd, kw, {})
+        self.cmd = r'%s build.py %%s 4 %%s'
+    def __call__(self, env, target, source):
+        cmd = self.get_contents(env, target, source)
+       print cmd
         return os.system(cmd)
-    def get_contents(self, **kw):
-        cmd = SCons.Util.scons_subst(self.cmd, kw, {})
-        return cmd
+    def get_contents(self, env, target, source):
+        return self.cmd %% (target, string.join(source, ' '))
 B = Builder(name = 'B', action = bld())
 env = Environment(BUILDERS = [B])
 env.B(target = 'foo.out', source = 'foo.in')