summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/Node
diff options
context:
space:
mode:
authorSteven Knight <knight@baldmt.com>2002-03-24 00:51:23 (GMT)
committerSteven Knight <knight@baldmt.com>2002-03-24 00:51:23 (GMT)
commit94d7ac89ad998937fbbc3c896dffc88cece8e925 (patch)
tree9120c2c8716c1ff5c824a644fb3a967ada0e8fd0 /src/engine/SCons/Node
parent2fa424ece5fd4df18a3aeff1f6e59e2ecb41a28e (diff)
downloadSCons-94d7ac89ad998937fbbc3c896dffc88cece8e925.zip
SCons-94d7ac89ad998937fbbc3c896dffc88cece8e925.tar.gz
SCons-94d7ac89ad998937fbbc3c896dffc88cece8e925.tar.bz2
Move SCons.Util.scons_str2nodes() to SCons.Node/__init__.py and shorten its name.
Diffstat (limited to 'src/engine/SCons/Node')
-rw-r--r--src/engine/SCons/Node/NodeTests.py59
-rw-r--r--src/engine/SCons/Node/__init__.py37
2 files changed, 95 insertions, 1 deletions
diff --git a/src/engine/SCons/Node/NodeTests.py b/src/engine/SCons/Node/NodeTests.py
index 8a166f2..1c92476 100644
--- a/src/engine/SCons/Node/NodeTests.py
+++ b/src/engine/SCons/Node/NodeTests.py
@@ -25,6 +25,7 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
import os
import sys
+import types
import unittest
import SCons.Errors
@@ -683,6 +684,64 @@ class NodeTestCase(unittest.TestCase):
assert tn.scanned[ds]
assert len(tn.implicit[ds]) == 2, tn.implicit
+ def test_arg2nodes(self):
+ """Test the arg2nodes function."""
+ dict = {}
+ class X(SCons.Node.Node):
+ pass
+ def Factory(name, directory = None, create = 1, dict=dict, X=X):
+ if not dict.has_key(name):
+ dict[name] = X()
+ dict[name].name = name
+ return dict[name]
+
+ nodes = SCons.Node.arg2nodes("Util.py UtilTests.py", Factory)
+ assert len(nodes) == 2, nodes
+ assert isinstance(nodes[0], X)
+ assert isinstance(nodes[1], X)
+ assert nodes[0].name == "Util.py"
+ assert nodes[1].name == "UtilTests.py"
+
+ if hasattr(types, 'UnicodeType'):
+ code = """if 1:
+ nodes = SCons.Node.arg2nodes(u"Util.py UtilTests.py", Factory)
+ assert len(nodes) == 2, nodes
+ assert isinstance(nodes[0], X)
+ assert isinstance(nodes[1], X)
+ assert nodes[0].name == u"Util.py"
+ assert nodes[1].name == u"UtilTests.py"
+ \n"""
+ exec code
+
+ nodes = SCons.Node.arg2nodes(["Util.py", "UtilTests.py"], Factory)
+ assert len(nodes) == 2, nodes
+ assert isinstance(nodes[0], X)
+ assert isinstance(nodes[1], X)
+ assert nodes[0].name == "Util.py"
+ assert nodes[1].name == "UtilTests.py"
+
+ n1 = Factory("Util.py")
+ nodes = SCons.Node.arg2nodes([n1, "UtilTests.py"], Factory)
+ assert len(nodes) == 2, nodes
+ assert isinstance(nodes[0], X)
+ assert isinstance(nodes[1], X)
+ assert nodes[0].name == "Util.py"
+ assert nodes[1].name == "UtilTests.py"
+
+ class SConsNode(SCons.Node.Node):
+ pass
+ nodes = SCons.Node.arg2nodes(SConsNode())
+ assert len(nodes) == 1, nodes
+ assert isinstance(nodes[0], SConsNode), node
+
+ class OtherNode:
+ pass
+ nodes = SCons.Node.arg2nodes(OtherNode())
+ assert len(nodes) == 1, nodes
+ assert isinstance(nodes[0], OtherNode), node
+
+
+
if __name__ == "__main__":
suite = unittest.makeSuite(NodeTestCase, 'test_')
if not unittest.TextTestRunner().run(suite).wasSuccessful():
diff --git a/src/engine/SCons/Node/__init__.py b/src/engine/SCons/Node/__init__.py
index c66a1a4..a7c8521 100644
--- a/src/engine/SCons/Node/__init__.py
+++ b/src/engine/SCons/Node/__init__.py
@@ -31,12 +31,14 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
-from SCons.Errors import BuildError
import string
import types
import copy
import sys
+from SCons.Errors import BuildError
+import SCons.Util
+
# Node states
#
# These are in "priority" order, so that the maximum value for any
@@ -318,3 +320,36 @@ class Walker:
def is_done(self):
return not self.stack
+
+
+def arg2nodes(arg, node_factory=None):
+ """This function converts a string or list into a list of Node instances.
+ It follows the rules outlined in the SCons design document by accepting
+ any of the following inputs:
+ - A single string containing names separated by spaces. These will be
+ split apart at the spaces.
+ - A single Node instance,
+ - A list containingg either strings or Node instances. Any strings
+ in the list are not split at spaces.
+ In all cases, the function returns a list of Node instances."""
+
+ narg = arg
+ if SCons.Util.is_String(arg):
+ narg = string.split(arg)
+ elif not SCons.Util.is_List(arg):
+ narg = [arg]
+
+ nodes = []
+ for v in narg:
+ if SCons.Util.is_String(v):
+ if node_factory:
+ nodes.append(node_factory(v))
+ # Do we enforce the following restriction? Maybe, but it
+ # would also restrict what we can do to allow people to
+ # use the engine with alternate Node implementations...
+ #elif not issubclass(v.__class__, Node):
+ # raise TypeError
+ else:
+ nodes.append(v)
+
+ return nodes