Add the Command() method.
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 2 Oct 2001 20:01:35 +0000 (20:01 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 2 Oct 2001 20:01:35 +0000 (20:01 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@82 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/engine/SCons/Builder.py
src/engine/SCons/BuilderTests.py
src/engine/SCons/Environment.py
src/engine/SCons/EnvironmentTests.py
src/engine/SCons/Node/__init__.py
test/Command.py

index c90bc40fc9f3ddbd011b27e4a40d71e9d8cabacb..f4dbec41eacdd62c37e03fe1de65183b7fe73532 100644 (file)
@@ -70,7 +70,7 @@ def Action(act):
        l = string.split(act, "\n")
        if len(l) > 1:
            act = l
-    if type(act) == types.FunctionType:
+    if callable(act):
        return FunctionAction(act)
     elif type(act) == types.StringType:
        return CommandAction(act)
index 965df6e13466b4c1b0c49892ffe9f17ac3aaa8f7..d2e5768c420dad94c258bbf72e21d67bdad38ffb 100644 (file)
@@ -80,16 +80,16 @@ class BuilderTestCase(unittest.TestCase):
        one is an internal Python function, one is a list
        containing one of each.
        """
+
        cmd1 = "python %s %s xyzzy" % (act_py, outfile)
+
        builder = SCons.Builder.Builder(action = cmd1)
        r = builder.execute()
        assert r == 0
        assert test.read(outfile, 'r') == "act.py: xyzzy\n"
 
        def function1(kw):
-           f = open(kw['out'], 'w')
-           f.write("function1\n")
-           f.close()
+           open(kw['out'], 'w').write("function1\n")
            return 1
 
        builder = SCons.Builder.Builder(action = function1)
@@ -97,14 +97,44 @@ class BuilderTestCase(unittest.TestCase):
        assert r == 1
        assert test.read(outfile, 'r') == "function1\n"
 
+       class class1a:
+           def __init__(self, kw):
+               open(kw['out'], 'w').write("class1a\n")
+
+       builder = SCons.Builder.Builder(action = class1a)
+       r = builder.execute(out = outfile)
+       assert r.__class__ == class1a
+       assert test.read(outfile, 'r') == "class1a\n"
+
+       class class1b:
+           def __call__(self, kw):
+               open(kw['out'], 'w').write("class1b\n")
+               return 2
+
+       builder = SCons.Builder.Builder(action = class1b())
+       r = builder.execute(out = outfile)
+       assert r == 2
+       assert test.read(outfile, 'r') == "class1b\n"
+
        cmd2 = "python %s %s syzygy" % (act_py, outfile)
+
        def function2(kw):
            open(kw['out'], 'a').write("function2\n")
-           return 2
-       builder = SCons.Builder.Builder(action = [cmd2, function2])
+           return 0
+
+       class class2a:
+           def __call__(self, kw):
+               open(kw['out'], 'a').write("class2a\n")
+               return 0
+
+       class class2b:
+           def __init__(self, kw):
+               open(kw['out'], 'a').write("class2b\n")
+
+       builder = SCons.Builder.Builder(action = [cmd2, function2, class2a(), class2b])
        r = builder.execute(out = outfile)
-       assert r == 2
-       assert test.read(outfile, 'r') == "act.py: syzygy\nfunction2\n"
+       assert r.__class__ == class2b
+       assert test.read(outfile, 'r') == "act.py: syzygy\nfunction2\nclass2a\nclass2b\n"
 
     def test_insuffix(self):
        """Test Builder creation with a specified input suffix
index c7c32dd4a11dcfc2a7e26467d3ff49b530d66252..e47a0a278b2726cd31181a7c8be5248c85dc8da1 100644 (file)
@@ -12,7 +12,7 @@ import copy
 import re
 import types
 import SCons.Util
-
+import SCons.Builder
 
 
 def Command():
@@ -139,6 +139,14 @@ class Environment:
            dlist = dlist[0]
        return dlist
 
+    def Command(self, target, source, action):
+        """Builds the supplied target files from the supplied
+        source files using the supplied action.  Action may
+        be any type that the Builder constructor will accept
+        for an action."""
+        bld = SCons.Builder.Builder(name="Command", action=action)
+        return bld(self, target, source)
+
     def subst(self, string):
        """Recursively interpolates construction variables from the
        Environment into the specified string, returning the expanded
index c503fe23ba4609ede298cc7be024184f334bf824..a60e5584834f3fd7de6118874b6f996e8a558c22 100644 (file)
@@ -145,6 +145,23 @@ class EnvironmentTestCase(unittest.TestCase):
        assert d.__class__.__name__ == 'File'
        assert d.path == 'Environment.py'
 
+    def test_Command(self):
+        """Test the Command() method."""
+        env = Environment()
+        t = env.Command(target='foo.out', source=['foo1.in', 'foo2.in'],
+                        action='buildfoo %(target)s %(source)s')
+        assert t.derived
+        assert t.builder.action.__class__.__name__ == 'CommandAction'
+        assert t.builder.action.command == 'buildfoo %(target)s %(source)s'
+        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):
+            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)
+
     def test_subst(self):
        """Test substituting construction variables within strings
        
index 60269005b0c430e74f97df8596cef53b72955df6..5b90eb76c74c96bf200be31738188adc40e0c5e4 100644 (file)
@@ -10,7 +10,7 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 from SCons.Errors import BuildError
 import string
-
+import types
 
 
 class Node:
index abe060e2eb8cd824852f8224c6e64665a2983d39..2d6c03d18651038efdb45055cf47730935e2ba69 100644 (file)
@@ -7,11 +7,9 @@ import TestSCons
 
 test = TestSCons.TestSCons()
 
-test.pass_test()       #XXX Short-circuit until this is implemented.
-
 test.write('build.py', r"""
 import sys
-contents = open(sys.argv[2], 'r').read() + open(sys.argv[3], 'r').read()
+contents = open(sys.argv[2], 'r').read()
 file = open(sys.argv[1], 'w')
 file.write(contents)
 file.close()
@@ -22,7 +20,7 @@ env = Environment()
 env.Command(target = 'f1.out', source = 'f1.in',
                action = "python build.py %(target)s %(source)s")
 env.Command(target = 'f2.out', source = 'f2.in',
-               action = "python build.py temp2 %(source)s\npython build.py %(target)s temp2")
+               action = "python build.py temp2 %(source)s\\npython build.py %(target)s temp2")
 env.Command(target = 'f3.out', source = 'f3.in',
                action = ["python build.py temp3 %(source)s",
                          "python build.py %(target)s temp3"])
@@ -35,7 +33,8 @@ test.write('f2.in', "f2.in\n")
 
 test.write('f3.in', "f3.in\n")
 
-test.run(arguments = '.')
+#XXXtest.run(arguments = '.')
+test.run(arguments = 'f1.out f2.out f3.out')
 
 test.fail_test(test.read('f1.out') != "f1.in\n")
 test.fail_test(test.read('f2.out') != "f2.in\n")