File size: 1,303 Bytes
f764cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Composition-oriented graph operations over RepoInfo."""

from __future__ import annotations

from collections import defaultdict

from .repo import RepoInfo


def dependency_components(info: RepoInfo) -> tuple[tuple[str, ...], ...]:
    """Return weakly connected hard-dependency components in topo order."""
    neighbors: dict[str, set[str]] = defaultdict(set)
    for edge in info.edges:
        if edge.status != "resolved" or edge.source == edge.target:
            continue
        neighbors[edge.source].add(edge.target)
        neighbors[edge.target].add(edge.source)
    position = {path: index for index, path in enumerate(info.topological_order)}
    unseen = set(position)
    result: list[tuple[str, ...]] = []
    for start in info.topological_order:
        if start not in unseen:
            continue
        unseen.remove(start)
        members: set[str] = {start}
        stack = [start]
        while stack:
            current = stack.pop()
            for neighbor in neighbors.get(current, ()):
                if neighbor in unseen:
                    unseen.remove(neighbor)
                    members.add(neighbor)
                    stack.append(neighbor)
        result.append(tuple(path for path in info.topological_order if path in members))
    return tuple(result)