def _get_dependency_tree_for_package(package, parent=None, sync=False, jobs=1): if package.name not in _TREE_DICT: logger.info("Building dependency tree for package: %s..." % package) tree = TreeNode(package, parent=parent) dependencies = [] with parallel.no_daemon_pool(processes=jobs) as pool: dependencies = pool.map(partial(_create_package, **{"sync": sync}), _INFO_DICT[package.name]["dependencies"]) with parallel.no_daemon_pool(processes=jobs) as pool: children = pool.map( partial(_get_dependency_tree_for_package, **{"parent": tree}), dependencies) if children: tree.add_children(*children) _TREE_DICT[package.name] = tree else: logger.info("Using cached dependency tree for package: %s." % package) tree = _TREE_DICT[package.name] tree.parent = parent return tree
def test_node(): tree1 = Node("foo") assert tree1.empty == True assert tree1 == Node("foo") assert str(tree1) == "<Node 'foo'>" assert tree1.render() == \ """\ foo """ tree2 = Node("foo", children=["bar", "baz"]) assert tree2.parent == None assert tree2 != Node("foo", children=["bar", "baz", "boo"]) assert Node("foo", children = ["bar", "baz"]) \ == Node("foo", children = ["bar", "baz"]) assert not Node("foo", children = ["bar", "baz"]) \ == Node("foo", children = ["baz", "boo"]) assert tree2.render() == \ """\ foo bar baz """ assert tree2.render(indent = 4) == \ """\ foo bar baz """ assert tree2.render(indent = 4, formatter = lambda x: "* %s" % x) == \ """\ * foo * bar * baz """ assert tree2.find(lambda x: x.obj == "foo") assert tree2.find(lambda x: x.obj == "bar") assert not tree2.find(lambda x: x.obj == "foobaz") tree3 = Node("foo") tree3.add_children(["bar", "baz"]) tree4 = Node("foo") tree4.children = ["bar", "baz"] with pytest.raises(TypeError): tree4.children = "bar"
def test_node(): tree1 = Node("foo") assert tree1.empty == True assert tree1 == Node("foo") assert str(tree1) == "<Node 'foo'>" assert tree1.render() == \ """\ foo """ tree2 = Node("foo", children=["bar", "baz"]) assert tree2.parent == None assert tree2 != Node("foo", children=["bar", "baz", "boo"]) assert Node("foo", children = ["bar", "baz"]) \ == Node("foo", children = ["bar", "baz"]) assert not Node("foo", children = ["bar", "baz"]) \ == Node("foo", children = ["baz", "boo"]) assert tree2.render() == \ """\ foo bar baz """ assert tree2.render(indent = 4) == \ """\ foo bar baz """ assert tree2.render(indent = 4, formatter = lambda x: "* %s" % x) == \ """\ * foo * bar * baz """ assert tree2.find(lambda x: x.obj == "foo") assert tree2.find(lambda x: x.obj == "bar") assert not tree2.find(lambda x: x.obj == "foobaz") tree3 = Node("foo") tree3.add_children(["bar", "baz"]) tree4 = Node("foo") tree4.children = ["bar", "baz"] with pytest.raises(TypeError): tree4.children = "bar" """ foo -> bar -> baz -> boo -> boo -> bar -> foo """ tree5 = Node("foo") node1 = Node("bar", ["baz", "boo"]) node2 = Node("boo", ["bar", "foo"]) tree5.add_children(node1, node2) assert tree5.find(node2) == node2 tree5.to_json() == dict(foo=dict( bar=dict(baz=None, boo=None), boo=dict(bar=None, foo=None), )) assert hash(tree5) == id(tree5) tree6 = Node("foo") children = [Node("bar"), Node("baz")] tree6.add_children(*children) tree6.children = children assert tree6.children == children tree7 = Node("foo") tree8 = Node("bar") tree8.parent = tree7 assert tree8.parent == tree7 tree8.parent = tree7 assert tree8.parent == tree7