Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cycles in when traversing graph #522

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/fromager/dependency_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def get_dependency_edges(
visited = set()
for edge in self._depth_first_traversal(
self.nodes[ROOT].children,
set(),
match_dep_types=match_dep_types,
):
if edge.destination_node.key not in visited:
Expand Down Expand Up @@ -277,13 +278,17 @@ def get_install_dependency_versions(

def _depth_first_traversal(
self,
start_node: list[DependencyEdge],
start_edges: list[DependencyEdge],
visited: set[str],
match_dep_types: list[RequirementType] | None = None,
) -> typing.Iterable[DependencyEdge]:
for edge in start_node:
for edge in start_edges:
if edge.destination_node.key in visited:
continue
if match_dep_types and edge.req_type not in match_dep_types:
continue
visited.add(edge.destination_node.key)
yield edge
yield from self._depth_first_traversal(
edge.destination_node.children, match_dep_types
edge.destination_node.children, visited, match_dep_types
)
38 changes: 38 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,41 @@ def test_get_install_dependencies():
for node in graph.get_install_dependencies()
]
assert install_nodes == ["a==2.0", "d==6.0", "b==3.0", "e==6.0"]


def test_cycles_get_install_dependencies():
graph = dependency_graph.DependencyGraph.from_dict(raw_graph)
# create cycle: a depends on d and d depends on a
graph.add_dependency(
parent_name=canonicalize_name("a"),
parent_version=Version("2.0"),
req_type=requirements_file.RequirementType.INSTALL,
req=Requirement("d>=4.0"),
req_version=Version("6.0"),
download_url="url for d",
)

graph.add_dependency(
parent_name=canonicalize_name("d"),
parent_version=Version("6.0"),
req_type=requirements_file.RequirementType.INSTALL,
req=Requirement("a<=2.0"),
req_version=Version("2.0"),
download_url="url for a",
)

# add another duplicate toplevel
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=requirements_file.RequirementType.TOP_LEVEL,
req=Requirement("a<=2.0"),
req_version=Version("2.0"),
download_url="url for a",
)

install_nodes = [
f"{node.to_dict()['canonicalized_name']}=={node.to_dict()['version']}"
for node in graph.get_install_dependencies()
]
assert install_nodes == ["a==2.0", "d==6.0"]
Loading