# this method is serialized, but execute isn't:
if print_tree and self.top:
print
- print SCons.Util.render_tree(self.targets[0], get_all_children)
+ SCons.Util.print_tree(self.targets[0], get_all_children)
if print_dtree and self.top:
print
- print SCons.Util.render_tree(self.targets[0], get_derived_children)
+ SCons.Util.print_tree(self.targets[0], get_derived_children)
if print_includes and self.top:
t = self.targets[0]
tree = t.render_include_tree()
or in the whole tree if prune.
"""
- if visited.has_key(root):
+ rname = str(root)
+
+ if visited.has_key(rname):
return ""
children = child_func(root)
else:
retval = retval + " "
- retval = retval + "+-" + str(root) + "\n"
+ retval = retval + "+-" + rname + "\n"
if not prune:
visited = copy.copy(visited)
- visited[root] = 1
+ visited[rname] = 1
for i in range(len(children)):
margin.append(i<len(children)-1)
return retval
+def print_tree(root, child_func, prune=0, margin=[0], visited={}):
+ """
+ Print a tree of nodes. This is like render_tree, except it prints
+ lines directly instead of creating a string representation in memory,
+ so that huge trees can be printed.
+
+ root - the root node of the tree
+ child_func - the function called to get the children of a node
+ prune - don't visit the same node twice
+ margin - the format of the left margin to use for children of root.
+ 1 results in a pipe, and 0 results in no pipe.
+ visited - a dictionary of visited nodes in the current branch if not prune,
+ or in the whole tree if prune.
+ """
+
+ rname = str(root)
+
+ if visited.has_key(rname):
+ return
+
+ def MMM(m):
+ return [" ","| "][m]
+ print string.join(map(MMM, margin[:-1]), '') + "+-" + rname
+
+ if prune:
+ visited[rname] = 1
+
+ children = child_func(root)
+ if children:
+ margin.append(1)
+ map(lambda C, cf=child_func, p=prune, m=margin, v=visited:
+ print_tree(C, cf, p, m, v),
+ children[:-1])
+ margin[-1] = 0
+ print_tree(children[-1], child_func, prune, margin, visited)
+ margin.pop()
+
+
def is_Dict(e):
return type(e) is types.DictType or isinstance(e, UserDict)
import os
import os.path
import string
+import StringIO
import sys
import types
import unittest
q = quote_spaces('x\tx')
assert q == '"x\tx"', q
- def test_render_tree(self):
+ def tree_case_1(self):
+ """Fixture for the render_tree() and print_tree() tests."""
class Node:
def __init__(self, name, children=[]):
self.children = children
def __str__(self):
return self.name
- def get_children(node):
- return node.children
-
windows_h = Node("windows.h")
stdlib_h = Node("stdlib.h")
stdio_h = Node("stdio.h")
+-windows.h
"""
- actual = render_tree(foo, get_children)
- assert expect == actual, (expect, actual)
+ return foo, expect
+
+ def tree_case_2(self):
+ """Fixture for the render_tree() and print_tree() tests."""
+ class Node:
+ def __init__(self, name, children=[]):
+ self.children = children
+ self.name = name
+ def __str__(self):
+ return self.name
+ stdlib_h = Node("stdlib.h")
bar_h = Node('bar.h', [stdlib_h])
blat_h = Node('blat.h', [stdlib_h])
blat_c = Node('blat.c', [blat_h, bar_h])
+-bar.h
"""
- actual = render_tree(blat_o, get_children, 1)
+ return blat_o, expect
+
+ def test_render_tree(self):
+ """Test the render_tree() function"""
+ def get_children(node):
+ return node.children
+
+ node, expect = self.tree_case_1()
+ actual = render_tree(node, get_children)
assert expect == actual, (expect, actual)
+ node, expect = self.tree_case_2()
+ actual = render_tree(node, get_children, 1)
+ assert expect == actual, (expect, actual)
+
+ def test_print_tree(self):
+ """Test the print_tree() function"""
+ def get_children(node):
+ return node.children
+
+ save_stdout = sys.stdout
+
+ try:
+ sys.stdout = StringIO.StringIO()
+ node, expect = self.tree_case_1()
+ print_tree(node, get_children)
+ actual = sys.stdout.getvalue()
+ assert expect == actual, (expect, actual)
+
+ sys.stdout = StringIO.StringIO()
+ node, expect = self.tree_case_2()
+ print_tree(node, get_children, 1)
+ actual = sys.stdout.getvalue()
+ assert expect == actual, (expect, actual)
+ finally:
+ sys.stdout = save_stdout
+
def test_is_Dict(self):
assert is_Dict({})
assert is_Dict(UserDict())
def test_LogicalLines(self):
"""Test the LogicalLines class"""
- import StringIO
-
fobj = StringIO.StringIO(r"""
foo \
bar \