From c5f8fde7adb01a19761dc4c6446ecf5c74a665b0 Mon Sep 17 00:00:00 2001 From: stevenknight Date: Fri, 8 Mar 2002 06:01:39 +0000 Subject: [PATCH] Changes to the CommandGenerator functionality. (Charles Crain) git-svn-id: http://scons.tigris.org/svn/scons/trunk@287 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- src/engine/SCons/Action.py | 25 +++++++++++++++++++++---- src/engine/SCons/ActionTests.py | 18 +++++++++++++++--- src/engine/SCons/Script/SConscript.py | 1 + test/CommandGenerator.py | 7 ++++--- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/engine/SCons/Action.py b/src/engine/SCons/Action.py index 7bee0828..26595d66 100644 --- a/src/engine/SCons/Action.py +++ b/src/engine/SCons/Action.py @@ -163,6 +163,10 @@ def SetCommandHandler(func): global spawn spawn = func +def GetCommandHandler(): + global spawn + return spawn + class CommandGenerator: """ Wrappes a command generator function so the Action() factory @@ -264,8 +268,8 @@ class CommandAction(ActionBase): self.command = string def execute(self, **kw): - import SCons.Util dict = apply(self.subst_dict, (), kw) + import SCons.Util cmd_list = SCons.Util.scons_subst_list(self.command, dict, {}, _rm) for cmd_line in cmd_list: if len(cmd_line): @@ -324,10 +328,23 @@ class CommandGeneratorAction(ActionBase): if kw.has_key("target") and not SCons.Util.is_List(kw["target"]): kw["target"] = [kw["target"]] - cmd_list = apply(self.generator, (), kw) + gen_list = apply(self.generator, (), kw) + gen_list = map(lambda x: map(str, x), gen_list) - cmd_list = map(lambda x: map(str, x), cmd_list) - for cmd_line in cmd_list: + # Do environment variable substitution on returned command list + dict = apply(self.subst_dict, (), kw) + cmd_list = [ ] + for gen_line in gen_list: + cmd_list.append([]) + curr_line = cmd_list[-1] + for gen_arg in gen_line: + arg_list = SCons.Util.scons_subst_list(gen_arg, dict, {}) + curr_line.extend(arg_list[0]) + if(len(arg_list) > 1): + cmd_list.extend(arg_list[1:]) + curr_line = cmd_list[-1] + + for cmd_line in filter(lambda x: x, cmd_list): if print_actions: self.show(cmd_line) if execute_actions: diff --git a/src/engine/SCons/ActionTests.py b/src/engine/SCons/ActionTests.py index 461da6e2..592fa82f 100644 --- a/src/engine/SCons/ActionTests.py +++ b/src/engine/SCons/ActionTests.py @@ -156,14 +156,26 @@ class CommandGeneratorActionTestCase(unittest.TestCase): """Test executing a command generator Action """ - def f(dummy, self=self): + def f(dummy, env, self=self): self.dummy = dummy - return [[""]] + return [["$FOO"]] + def ch(cmd, args, env, self=self): + self.cmd.append(cmd) + self.args.append(args) a = SCons.Action.CommandGeneratorAction(f) self.dummy = 0 - a.execute(dummy=1) + old_hdl = SCons.Action.GetCommandHandler() + self.cmd = [] + self.args = [] + try: + SCons.Action.SetCommandHandler(ch) + a.execute(dummy=1, env={ 'FOO' : 'foo baz\nbar ack' }) + finally: + SCons.Action.SetCommandHandler(old_hdl) assert self.dummy == 1 + assert self.cmd == [ 'foo', 'bar'], self.cmd + assert self.args == [ [ 'foo', 'baz' ], [ 'bar', 'ack' ] ], self.args del self.dummy def test_get_contents(self): diff --git a/src/engine/SCons/Script/SConscript.py b/src/engine/SCons/Script/SConscript.py index 7210bed6..d50f521a 100644 --- a/src/engine/SCons/Script/SConscript.py +++ b/src/engine/SCons/Script/SConscript.py @@ -189,4 +189,5 @@ def BuildDefaultGlobals(): globals['Scanner'] = SCons.Scanner.Base globals['SConscript'] = SConscript globals['SetCommandHandler'] = SCons.Action.SetCommandHandler + globals['GetCommandHandler'] = SCons.Action.GetCommandHandler return globals diff --git a/test/CommandGenerator.py b/test/CommandGenerator.py index c1413d64..a85b07a8 100644 --- a/test/CommandGenerator.py +++ b/test/CommandGenerator.py @@ -45,11 +45,12 @@ test.write('SConstruct', """ def g(source, target, env): import sys python = sys.executable - return [[python, "build.py", ".temp"] + source, - [python, "build.py"] + target + [".temp"]] + return [[python, "build.py", "$TEMPFILE"] + source, + [python, "build.py"] + target + ["$TEMPFILE"]] b = Builder(name = 'b', generator=g) -env = Environment(BUILDERS = [b]) +env = Environment(BUILDERS = [b], + TEMPFILE=".temp") env.b(target = 'foo1.out', source = 'foo1.in') env.b(target = 'foo2.out', source = 'foo2.in') env.b(target = 'foo3.out', source = 'foo3.in') -- 2.26.2