Checkpoint refactorings to remove CommandGenerator and ToolSpec classes.
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Sun, 6 Mar 2005 18:00:40 +0000 (18:00 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Sun, 6 Mar 2005 18:00:40 +0000 (18:00 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@1248 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/engine/SCons/Action.py
src/engine/SCons/ActionTests.py
src/engine/SCons/Builder.py
src/engine/SCons/Tool/__init__.py
src/engine/SCons/Tool/mingw.py
src/engine/SCons/Tool/mwld.py
src/test_setup.py

index 077ddfb60d959f603bee66ca160b9bbd2103fa72..3b0230c7dc5d07e6076959a06b035b8c1ac5886e 100644 (file)
@@ -143,20 +143,6 @@ def _actionAppend(act1, act2):
         else:
             return ListAction([ a1, a2 ])
 
-class CommandGenerator:
-    """
-    Wraps a command generator function so the Action() factory
-    function can tell a generator function from a function action.
-    """
-    def __init__(self, generator):
-        self.generator = generator
-
-    def __add__(self, other):
-        return _actionAppend(self, other)
-
-    def __radd__(self, other):
-        return _actionAppend(other, self)
-
 def _do_create_action(act, *args, **kw):
     """This is the actual "implementation" for the
     Action factory method, below.  This handles the
@@ -172,10 +158,17 @@ def _do_create_action(act, *args, **kw):
         return act
     if SCons.Util.is_List(act):
         return apply(CommandAction, (act,)+args, kw)
-    if isinstance(act, CommandGenerator):
-        return apply(CommandGeneratorAction, (act.generator,)+args, kw)
     if callable(act):
-        return apply(FunctionAction, (act,)+args, kw)
+        try:
+            gen = kw['generator']
+            del kw['generator']
+        except KeyError:
+            gen = 0
+        if gen:
+            action_type = CommandGeneratorAction
+        else:
+            action_type = FunctionAction
+        return apply(action_type, (act,)+args, kw)
     if SCons.Util.is_String(act):
         var=SCons.Util.get_environment_var(act)
         if var:
index 36117a4cac5167b489b75947379f948aba3bb76d..3790c8cc16bd01d2492c5b45bf210a38fe5080c5 100644 (file)
@@ -281,13 +281,11 @@ class ActionTestCase(unittest.TestCase):
             pass
         def bar():
             pass
-        cg = SCons.Action.CommandGenerator(foo)
-
-        a1 = SCons.Action.Action(cg)
+        a1 = SCons.Action.Action(foo, generator=1)
         assert isinstance(a1, SCons.Action.CommandGeneratorAction), a1
         assert a1.generator is foo, a1.generator
 
-        a2 = SCons.Action.Action(cg, strfunction=bar)
+        a2 = SCons.Action.Action(foo, strfunction=bar, generator=1)
         assert isinstance(a2, SCons.Action.CommandGeneratorAction), a2
         assert a2.generator is foo, a2.generator
 
@@ -578,7 +576,7 @@ class _ActionActionTestCase(unittest.TestCase):
 
         def gen(target, source, env, for_signature):
             return 'generat' + env.get('GEN', 'or')
-        a = SCons.Action.Action(SCons.Action.CommandGenerator(gen))
+        a = SCons.Action.Action(gen, generator=1)
         s = a.presub_lines(env)
         assert s == ["generator"], s
         s = a.presub_lines(Environment(GEN = 'ed'))
@@ -597,7 +595,7 @@ class _ActionActionTestCase(unittest.TestCase):
         # containing all the Actions.
         def bar():
             return None
-        baz = SCons.Action.CommandGenerator(bar)
+        baz = SCons.Action.Action(bar, generator=1)
         act1 = SCons.Action.Action('foo bar')
         act2 = SCons.Action.Action([ 'foo', bar ])
 
@@ -1419,7 +1417,7 @@ class ListActionTestCase(unittest.TestCase):
             pass
         def g(target,source,env,for_signature):
             return 'generated %s %s' % (target[0], source[0])
-        g = SCons.Action.CommandGenerator(g)
+        g = SCons.Action.Action(g, generator=1)
         a = SCons.Action.ListAction([f, g, "XXX", f])
         s = a.genstring(['foo.x'], ['bar.y'], Environment())
         assert s == "f(target, source, env)\ngenerated foo.x bar.y\nXXX\nf(target, source, env)", s
@@ -1464,7 +1462,7 @@ class ListActionTestCase(unittest.TestCase):
             s.foo=1
             return "y"
         a = SCons.Action.ListAction(["x",
-                                     SCons.Action.CommandGenerator(gen),
+                                     SCons.Action.Action(gen, generator=1),
                                      "z"])
         c = a.get_contents(target=[], source=[], env=Environment(s = self))
         assert self.foo==1, self.foo
index 8d99a32583041ec717a5df432bf512713ca5b9a0..a20757e360082feca32af88041ccedc37e4f0d69 100644 (file)
@@ -243,12 +243,15 @@ def Builder(**kw):
     if kw.has_key('generator'):
         if kw.has_key('action'):
             raise UserError, "You must not specify both an action and a generator."
-        kw['action'] = SCons.Action.CommandGenerator(kw['generator'])
+        kw['action'] = SCons.Action.CommandGeneratorAction(kw['generator'])
         del kw['generator']
-    elif kw.has_key('action') and SCons.Util.is_Dict(kw['action']):
-        composite = DictCmdGenerator(kw['action'])
-        kw['action'] = SCons.Action.CommandGenerator(composite)
-        kw['src_suffix'] = composite.src_suffixes()
+    elif kw.has_key('action'):
+        if SCons.Util.is_Dict(kw['action']):
+            composite = DictCmdGenerator(kw['action'])
+            kw['action'] = SCons.Action.CommandGeneratorAction(composite)
+            kw['src_suffix'] = composite.src_suffixes()
+        else:
+            kw['action'] = SCons.Action.Action(kw['action'])
 
     if kw.has_key('emitter'):
         emitter = kw['emitter']
@@ -397,7 +400,7 @@ class BuilderBase:
                         is_explicit = 1,
                         **overrides):
         if __debug__: logInstanceCreation(self, 'Builder.BuilderBase')
-        self.action = SCons.Action.Action(action)
+        self.action = action
         self.multi = multi
         if SCons.Util.is_Dict(prefix):
             prefix = CallableSelector(prefix)
index d9b1c30e72c020354bf2a438e74d26e9be40e2ec..b4e8ad58b237427d8d5848a43530e56dd7883b19 100644 (file)
@@ -68,12 +68,52 @@ for suffix in CSuffixes:
 for suffix in DSuffixes:
     SourceFileScanner.add_scanner(suffix, DScanner)
 
-class ToolSpec:
-    def __init__(self, name, **kw):
+class Tool:
+    def __init__(self, name, toolpath=[], **kw):
         self.name = name
+        self.toolpath = toolpath
         # remember these so we can merge them into the call
         self.init_kw = kw
 
+        module = self._tool_module()
+        self.generate = module.generate
+        self.exists = module.exists
+
+    def _tool_module(self):
+        oldpythonpath = sys.path
+        sys.path = self.toolpath + sys.path
+
+        try:
+            try:
+                file, path, desc = imp.find_module(self.name, self.toolpath)
+                try:
+                    return imp.load_module(self.name, file, path, desc)
+                finally:
+                    if file:
+                        file.close()
+            except ImportError, e:
+                pass
+        finally:
+            sys.path = oldpythonpath
+
+        full_name = 'SCons.Tool.' + self.name
+        try:
+            return sys.modules[full_name]
+        except KeyError:
+            try:
+                smpath = sys.modules['SCons.Tool'].__path__
+                file, path, desc = imp.find_module(self.name, smpath)
+                try:
+                    module = imp.load_module(full_name, file, path, desc)
+                    setattr(SCons.Tool, self.name, module)
+                    return module
+                finally:
+                    if file:
+                        file.close()
+            except ImportError, e:
+                m = "No tool named '%s': %s" % (self.name, e)
+                raise SCons.Errors.UserError, m
+
     def __call__(self, env, *args, **kw):
         if self.init_kw is not None:
             # Merge call kws into init kws;
@@ -89,46 +129,6 @@ class ToolSpec:
 
     def __str__(self):
         return self.name
-    
-def Tool(name, toolpath=[], **kw):
-    "Select a canned Tool specification, optionally searching in toolpath."
-
-    oldpythonpath = sys.path
-    sys.path = toolpath + sys.path
-
-    try:
-        try:
-            file, path, desc = imp.find_module(name, toolpath)
-            try:
-                module = imp.load_module(name, file, path, desc)
-                spec = apply(ToolSpec, (name,), kw)
-                spec.generate = module.generate
-                spec.exists = module.exists
-                return spec
-            finally:
-                if file:
-                    file.close()
-        except ImportError, e:
-            pass
-    finally:
-        sys.path = oldpythonpath
-
-    
-    full_name = 'SCons.Tool.' + name
-    if not sys.modules.has_key(full_name):
-        try:
-            file, path, desc = imp.find_module(name,
-                                        sys.modules['SCons.Tool'].__path__)
-            mod = imp.load_module(full_name, file, path, desc)
-            setattr(SCons.Tool, name, mod)
-        except ImportError, e:
-            raise SCons.Errors.UserError, "No tool named '%s': %s" % (name, e)
-        if file:
-            file.close()
-    spec = apply(ToolSpec, (name,), kw)
-    spec.generate = sys.modules[full_name].generate
-    spec.exists = sys.modules[full_name].exists
-    return spec
 
 def createProgBuilder(env):
     """This is a utility function that creates the Program
index 78cffde4e9b8991b34d7d96ee34ad3bb88bb94ef..1fd1530c00d8ca5482fe6a730526f1acb9f30b97 100644 (file)
@@ -94,7 +94,7 @@ def shlib_emitter(target, source, env):
     return (target, source)
                          
 
-shlib_action = SCons.Action.CommandGenerator(shlib_generator)
+shlib_action = SCons.Action.Action(shlib_generator, generator=1)
 
 res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
 
index 5e90a9ccdfad6e0ca8cb113bae14f19cd21b4a07..e2b1827ca82ec58cd7846f0256a56c27cf86608b 100644 (file)
@@ -98,4 +98,4 @@ def shlib_emitter(target, source, env):
     return target, source
 
 
-shlib_action = SCons.Action.CommandGenerator(shlib_generator)
+shlib_action = SCons.Action.Action(shlib_generator, generator=1)
index ab5b6513a1f51a7272879f1a365f42c2e700939c..73b6588d303d9e33b3dc28dbbe551312c62c261b 100644 (file)
@@ -107,6 +107,10 @@ try:
 except KeyError:
     cwd = os.getcwd()
 
+test = MyTestSCons()
+
+test.subdir(test.root)
+
 tar_gz = os.path.join(cwd, 'build', 'dist', '%s.tar.gz' % scons_version)
 
 if not os.path.isfile(tar_gz):
@@ -114,10 +118,6 @@ if not os.path.isfile(tar_gz):
     print "Cannot test package installation."
     test.no_result(1)
 
-test = MyTestSCons()
-
-test.subdir(test.root)
-
 # Unpack the .tar.gz file.  This should create the scons_version/
 # subdirectory from which we execute the setup.py script therein.
 os.system("gunzip -c %s | tar xf -" % tar_gz)