From: stevenknight Date: Tue, 2 Oct 2001 20:01:35 +0000 (+0000) Subject: Add the Command() method. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4f3d8acc57657dd91832570571a3f9ec5a8dc30b;p=scons.git Add the Command() method. git-svn-id: http://scons.tigris.org/svn/scons/trunk@82 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- diff --git a/src/engine/SCons/Builder.py b/src/engine/SCons/Builder.py index c90bc40f..f4dbec41 100644 --- a/src/engine/SCons/Builder.py +++ b/src/engine/SCons/Builder.py @@ -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) diff --git a/src/engine/SCons/BuilderTests.py b/src/engine/SCons/BuilderTests.py index 965df6e1..d2e5768c 100644 --- a/src/engine/SCons/BuilderTests.py +++ b/src/engine/SCons/BuilderTests.py @@ -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 diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index c7c32dd4..e47a0a27 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -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 diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index c503fe23..a60e5584 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -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 diff --git a/src/engine/SCons/Node/__init__.py b/src/engine/SCons/Node/__init__.py index 60269005..5b90eb76 100644 --- a/src/engine/SCons/Node/__init__.py +++ b/src/engine/SCons/Node/__init__.py @@ -10,7 +10,7 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" from SCons.Errors import BuildError import string - +import types class Node: diff --git a/test/Command.py b/test/Command.py index abe060e2..2d6c03d1 100644 --- a/test/Command.py +++ b/test/Command.py @@ -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")