Eliminate if-tests for Types in Builder.execute().
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Mon, 10 Sep 2001 12:55:33 +0000 (12:55 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Mon, 10 Sep 2001 12:55:33 +0000 (12:55 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@34 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/scons/Builder.py
src/scons/BuilderTests.py

index 76c5512f9d590e50bf13aa1aa749b9aa372df2f6..ba489ec2ad144f9c99c1877219366ac1f98e9180 100644 (file)
@@ -9,7 +9,7 @@ __revision__ = "Builder.py __REVISION__ __DATE__ __DEVELOPER__"
 
 
 import os
-from types import *
+import types
 from scons.Node.FS import Dir, File, lookup
 
 
@@ -25,7 +25,7 @@ class Builder:
                        output_suffix = None,
                        node_class = File):
        self.name = name
-       self.action = action
+       self.action = Action(action)
        self.insuffix = input_suffix
        self.outsuffix = output_suffix
        self.node_class = node_class
@@ -46,13 +46,45 @@ class Builder:
     def execute(self, **kw):
        """Execute a builder's action to create an output object.
        """
-       # XXX THIS SHOULD BE DONE BY TURNING Builder INTO A FACTORY
-       # FOR SUBCLASSES FOR StringType AND FunctionType
-       t = type(self.action)
-       if t == StringType:
-           cmd = self.action % kw
-           print cmd
-           os.system(cmd)
-       elif t == FunctionType:
-           # XXX WHAT SHOULD WE PRINT HERE
-           self.action(kw)
+       apply(self.action.execute, (), kw)
+
+
+
+def Action(act):
+    """A factory for action objects."""
+    if type(act) == types.FunctionType:
+       return FunctionAction(act)
+    elif type(act) == types.StringType:
+       return CommandAction(act)
+    else:
+       return None
+
+class ActionBase:
+    """Base class for actions that create output objects.
+    
+    We currently expect Actions will only be accessible through
+    Builder objects, so they don't yet merit their own module."""
+    def __cmp__(self, other):
+       return cmp(self.__dict__, other.__dict__)
+
+    def show(self, string):
+       print string
+
+class CommandAction(ActionBase):
+    """Class for command-execution actions."""
+    def __init__(self, string):
+       self.command = string
+
+    def execute(self, **kw):
+       cmd = self.command % kw
+       self.show(cmd)
+       os.system(cmd)
+
+class FunctionAction(ActionBase):
+    """Class for Python function actions."""
+    def __init__(self, function):
+       self.function = function
+
+    def execute(self, **kw):
+       # XXX:  WHAT SHOULD WE PRINT HERE?
+       self.function(kw)
index 1effa8954b0526955575f13678443b11a2fe2228..822b64c0dcf7eec18c2555aa7ac02c2b3e7b1462 100644 (file)
@@ -3,8 +3,8 @@ __revision__ = "BuilderTests.py __REVISION__ __DATE__ __DEVELOPER__"
 import sys
 import unittest
 
-from scons.Builder import Builder
-from TestCmd import TestCmd
+import TestCmd
+from scons.Builder import Builder, CommandAction, FunctionAction
 
 
 # Initial setup of the common environment for all tests,
@@ -14,7 +14,7 @@ from TestCmd import TestCmd
 # We don't do this as a setUp() method because it's
 # unnecessary to create a separate directory and script
 # for each test, they can just use the one.
-test = TestCmd(workdir = '')
+test = TestCmd.TestCmd(workdir = '')
 
 test.write('act.py', """import os, string, sys
 f = open(sys.argv[1], 'w')
@@ -35,7 +35,7 @@ class BuilderTestCase(unittest.TestCase):
        Verify that we can retrieve the supplied action attribute.
        """
        builder = Builder(action = "foo")
-       assert builder.action == "foo"
+       assert builder.action.command == "foo"
 
     def test_cmp(self):
        """Test simple comparisons of Builder objects