Make the new --debug=presub option work for LazyCommandGenerators. (Gary Oberbrunner)
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Sat, 3 Apr 2004 06:23:15 +0000 (06:23 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Sat, 3 Apr 2004 06:23:15 +0000 (06:23 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@942 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/engine/SCons/Action.py
src/engine/SCons/ActionTests.py

index c3ee84a2791c2ba2aae97b055a4ca75c83f88f5d..f7024d344885529a7df74ff01eb4e7f51624e8a7 100644 (file)
@@ -198,11 +198,19 @@ class ActionBase:
         if print_actions:
             sys.stdout.write(s + '\n')
 
-    def presub(self, target):
+    def presub(self, target, env):
         if print_actions_presub:
             if not SCons.Util.is_List(target):
                 target = [target]
+            # CommandGeneratorAction needs a real environment
+            # in order to return the proper string here, since
+            # it may call LazyCmdGenerator, which looks up a key
+            # in that env.  So we temporarily remember the env here,
+            # and CommandGeneratorAction will use this env
+            # when it calls its __generate method.
+            self.presub_env = env
             lines = string.split(str(self), '\n')
+            self.presub_env = None      # don't need this any more
             sys.stdout.write("Building %s with action(s):\n  %s\n"%
                 (string.join(map(lambda x: str(x), target), ' and '),
                  string.join(lines, '\n  ')))
@@ -327,7 +335,7 @@ class CommandAction(ActionBase):
         return 0
 
     def __call__(self, target, source, env):
-        self.presub(target)
+        self.presub(target, env)
         return self._execute(target, source, env)
 
     def get_contents(self, target, source, env, dict=None):
@@ -369,7 +377,11 @@ class CommandGeneratorAction(ActionBase):
         return act.strfunction(target, rsources, env)
 
     def __str__(self):
-        act = self.__generate([], [], {}, 0)
+        try:
+            env = self.presub_env or {}
+        except AttributeError:
+            env = {}
+        act = self.__generate([], [], env, 0)
         return str(act)
 
     def _execute(self, target, source, env):
@@ -384,7 +396,7 @@ class CommandGeneratorAction(ActionBase):
             source = [source]
         rsources = map(rfile, source)
         act = self.__generate(target, source, env, 0)
-        act.presub(target)
+        act.presub(target, env)
         return act._execute(target, source, env)
 
     def get_contents(self, target, source, env, dict=None):
@@ -412,6 +424,9 @@ class LazyCmdGenerator:
             # The variable reference substitutes to nothing.
             return ''
 
+    def __str__(self):
+        return 'LazyCmdGenerator: %s'%str(self.var)
+
     def _execute(self, target, source, env, for_signature):
         try:
             return env[self.var]
@@ -472,7 +487,7 @@ class FunctionAction(ActionBase):
         return r
 
     def __call__(self, target, source, env):
-        self.presub(target)
+        self.presub(target, env)
         return self._execute(target, source, env)
 
     def get_contents(self, target, source, env, dict=None):
@@ -523,7 +538,7 @@ class ListAction(ActionBase):
         return 0
 
     def __call__(self, target, source, env):
-        self.presub(target)
+        self.presub(target, env)
         return self._execute(target, source, env)
 
     def get_contents(self, target, source, env, dict=None):
index e81af0f09c8733562227df8e834f0252da4ab68c..5672dac5aee1e35ae6a46baf7261ab79e538d931 100644 (file)
@@ -291,10 +291,11 @@ class ActionBaseTestCase(unittest.TestCase):
 
         try:
             a = SCons.Action.Action("x")
+            env = Environment()
 
             sio = StringIO.StringIO()
             sys.stdout = sio
-            a.presub("xyzzy")
+            a.presub("xyzzy", env)
             s = sio.getvalue()
             assert s == "", s
 
@@ -302,10 +303,58 @@ class ActionBaseTestCase(unittest.TestCase):
 
             sio = StringIO.StringIO()
             sys.stdout = sio
-            a.presub("foobar")
+            a.presub("foobar", env)
             s = sio.getvalue()
             assert s == "Building foobar with action(s):\n  x\n", s
 
+            a = SCons.Action.Action(["y", "z"])
+
+            sio = StringIO.StringIO()
+            sys.stdout = sio
+            a.presub("foobar", env)
+            s = sio.getvalue()
+            assert s == "Building foobar with action(s):\n  y\n  z\n", s
+
+            def func():
+                pass
+            a = SCons.Action.Action(func)
+
+            sio = StringIO.StringIO()
+            sys.stdout = sio
+            a.presub("foobar", env)
+            s = sio.getvalue()
+            assert s == "Building foobar with action(s):\n  func(env, target, source)\n", s
+
+            def gen(target, source, env, for_signature):
+                return 'generat' + env.get('GEN', 'or')
+            a = SCons.Action.Action(SCons.Action.CommandGenerator(gen))
+
+            sio = StringIO.StringIO()
+            sys.stdout = sio
+            a.presub("foobar", env)
+            s = sio.getvalue()
+            assert s == "Building foobar with action(s):\n  generator\n", s
+
+            sio = StringIO.StringIO()
+            sys.stdout = sio
+            a.presub("foobar", Environment(GEN = 'ed'))
+            s = sio.getvalue()
+            assert s == "Building foobar with action(s):\n  generated\n", s
+
+            a = SCons.Action.Action("$ACT")
+
+            sio = StringIO.StringIO()
+            sys.stdout = sio
+            a.presub("foobar", env)
+            s = sio.getvalue()
+            assert s == "Building foobar with action(s):\n  \n", s
+
+            sio = StringIO.StringIO()
+            sys.stdout = sio
+            a.presub("foobar", Environment(ACT = 'expanded action'))
+            s = sio.getvalue()
+            assert s == "Building foobar with action(s):\n  expanded action\n", s
+
         finally:
             SCons.Action.print_actions_presub = save_print_actions_presub
             sys.stdout = save_stdout