forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openshift#18256 from juanvallejo/jvallejo/deptool-…
…filters Automatic merge from submit-queue (batch tested with PRs 18310, 18239, 18256, 18327, 18328). add vendor packages filter Adds graph filter
- Loading branch information
Showing
4 changed files
with
437 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package trace | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/gonum/graph/concrete" | ||
|
||
depgraph "github.com/openshift/origin/tools/depcheck/pkg/graph" | ||
) | ||
|
||
// FilterPackages receives a graph and a set of packagePrefixes contained within the graph. | ||
// Returns a new graph with the sub-tree for each node matching the packagePrefix collapsed | ||
// into just that node. Relationships among packagePrefixes are kept: edges originating from | ||
// packagePrefix subpackages are re-written to originate from the packagePrefix, and edges | ||
// terminating at packagePrefix subpackages are re-written to terminate at the packagePrefix. | ||
func FilterPackages(g *depgraph.MutableDirectedGraph, packagePrefixes []string) (*depgraph.MutableDirectedGraph, error) { | ||
collapsedGraph := depgraph.NewMutableDirectedGraph(concrete.NewDirectedGraph()) | ||
|
||
// copy all nodes to new graph | ||
for _, n := range g.Nodes() { | ||
node, ok := n.(*depgraph.Node) | ||
if !ok { | ||
continue | ||
} | ||
|
||
collapsedNodeName := getFilteredNodeName(packagePrefixes, node.UniqueName) | ||
_, exists := collapsedGraph.NodeByName(collapsedNodeName) | ||
if exists { | ||
continue | ||
} | ||
|
||
err := collapsedGraph.AddNode(&depgraph.Node{ | ||
UniqueName: collapsedNodeName, | ||
Id: n.ID(), | ||
LabelName: labelNameForNode(collapsedNodeName), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// add edges to collapsed graph | ||
for _, from := range g.Nodes() { | ||
node, ok := from.(*depgraph.Node) | ||
if !ok { | ||
return nil, fmt.Errorf("expected nodes in graph to be of type *depgraph.Node") | ||
} | ||
|
||
fromNodeName := getFilteredNodeName(packagePrefixes, node.UniqueName) | ||
fromNode, exists := collapsedGraph.NodeByName(fromNodeName) | ||
if !exists { | ||
return nil, fmt.Errorf("expected node with name %q to exist in collapsed graph", fromNodeName) | ||
} | ||
|
||
for _, to := range g.From(from) { | ||
node, ok := to.(*depgraph.Node) | ||
if !ok { | ||
return nil, fmt.Errorf("expected nodes in graph to be of type *depgraph.Node") | ||
} | ||
|
||
toNodeName := getFilteredNodeName(packagePrefixes, node.UniqueName) | ||
if fromNodeName == toNodeName { | ||
continue | ||
} | ||
|
||
toNode, exists := collapsedGraph.NodeByName(toNodeName) | ||
if !exists { | ||
return nil, fmt.Errorf("expected node with name %q to exist in collapsed graph", toNodeName) | ||
} | ||
|
||
if collapsedGraph.HasEdgeFromTo(fromNode, toNode) { | ||
continue | ||
} | ||
|
||
collapsedGraph.SetEdge(concrete.Edge{ | ||
F: fromNode, | ||
T: toNode, | ||
}, 0) | ||
} | ||
} | ||
|
||
return collapsedGraph, nil | ||
} | ||
|
||
func getFilteredNodeName(collapsedPrefixes []string, packageName string) string { | ||
for _, prefix := range collapsedPrefixes { | ||
if strings.HasPrefix(packageName, prefix) { | ||
return prefix | ||
} | ||
} | ||
|
||
return packageName | ||
} |
Oops, something went wrong.