forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze.go
224 lines (181 loc) · 5.96 KB
/
analyze.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package cmd
import (
"errors"
"fmt"
"io"
"strings"
"github.com/spf13/cobra"
"github.com/gonum/graph/path"
"github.com/openshift/origin/tools/depcheck/pkg/analyze"
"github.com/openshift/origin/tools/depcheck/pkg/graph"
)
var analyzeImportsExample = `# analyze a dependency graph against one of its vendor packages
%[1]s analyze --root=github.com/openshift/origin --entry=pkg/foo/... --dep=github.com/openshift/origin/vendor/k8s.io/kubernetes
# analyze a dependency graph against one of its vendor packages using OpenShift defaults
%[1]s analyze --root=github.com/openshift/origin --entry=cmd/... --entry=pkg/... --entry=tools/... --entry=test/... --openshift --dep=github.com/openshift/origin/vendor/k8s.io/kubernetes
`
type AnalyzeOptions struct {
GraphOptions *graph.GraphOptions
// Packages to analyze
Dependencies []string
Out io.Writer
ErrOut io.Writer
}
type AnalyzeFlags struct {
GraphFlags *graph.GraphFlags
Dependencies []string
}
func (o *AnalyzeFlags) ToOptions(out, errout io.Writer) (*AnalyzeOptions, error) {
graphOpts, err := o.GraphFlags.ToOptions(out, errout)
if err != nil {
return nil, err
}
return &AnalyzeOptions{
GraphOptions: graphOpts,
Dependencies: o.Dependencies,
Out: out,
ErrOut: errout,
}, nil
}
func NewCmdAnalyzeImports(parent string, out, errout io.Writer) *cobra.Command {
analyzeFlags := &AnalyzeFlags{
GraphFlags: &graph.GraphFlags{},
}
cmd := &cobra.Command{
Use: "analyze --root=github.com/openshift/origin --entry=pkg/foo/... --dep pkg/vendor/bar",
Short: "Creates and analyzes a dependency graph against a specified subpackage",
Long: "Creates and analyzes a dependency graph against a specified subpackage",
Example: fmt.Sprintf(traceImportsExample, parent),
RunE: func(c *cobra.Command, args []string) error {
opts, err := analyzeFlags.ToOptions(out, errout)
if err != nil {
return err
}
if err := opts.Complete(); err != nil {
return err
}
if err := opts.Validate(); err != nil {
return err
}
if err := opts.Run(); err != nil {
return err
}
return nil
},
}
analyzeFlags.GraphFlags.AddFlags(cmd)
cmd.Flags().StringSliceVarP(&analyzeFlags.Dependencies, "dep", "d", analyzeFlags.Dependencies, "import path of the dependency to analyze. Multiple --dep values may be provided.")
return cmd
}
func (o *AnalyzeOptions) Complete() error {
return o.GraphOptions.Complete()
}
func (o *AnalyzeOptions) Validate() error {
if err := o.GraphOptions.Validate(); err != nil {
return err
}
if len(o.Dependencies) == 0 {
return errors.New("at least one --dep package must be specified")
}
return nil
}
func (o *AnalyzeOptions) Run() error {
g, err := o.GraphOptions.BuildGraph()
if err != nil {
return err
}
return o.analyzeGraph(g)
}
// analyzeGraph receives a MutableDirectedGraph and outputs
// - "Yours": a list of every node in the set of dependencies unique to a target (--dep) node.
// - "Mine": a list of every node in the set of dependencies unique to the root nodes.
// - "Ours": a list of every node in the overlapping set between the "Yours" and "Mine" sets.
func (o *AnalyzeOptions) analyzeGraph(g *graph.MutableDirectedGraph) error {
yours, mine, ours, err := o.calculateDependencies(g)
if err != nil {
return err
}
fmt.Printf("Analyzing a total of %v packages\n", len(g.Nodes()))
fmt.Println()
fmt.Printf("\"Yours\": %v dependencies exclusive to %q\n", len(yours), o.Dependencies)
for _, n := range yours {
fmt.Printf(" - %s\n", n)
}
fmt.Println()
fmt.Printf("\"Mine\": %v direct (first-level) dependencies exclusive to the origin repo\n", len(mine))
for _, n := range mine {
fmt.Printf(" - %s\n", n)
}
fmt.Println()
fmt.Printf("\"Ours\": %v shared dependencies between the origin repo and %v\n", len(ours), o.Dependencies)
for _, n := range ours {
fmt.Printf(" - %s\n", n)
}
return nil
}
func (o *AnalyzeOptions) calculateDependencies(g *graph.MutableDirectedGraph) ([]*graph.Node, []*graph.Node, []*graph.Node, error) {
yoursRoots := []*graph.Node{}
for _, dep := range o.Dependencies {
n, exists := g.NodeByName(dep)
if !exists {
return nil, nil, nil, fmt.Errorf("unable to find dependency with import path %q", dep)
}
node, ok := n.(*graph.Node)
if !ok {
return nil, nil, nil, fmt.Errorf("expected node to analyze to be of type *graph.Node. Got: %v", n)
}
yoursRoots = append(yoursRoots, node)
}
yours := analyze.FindExclusiveDependencies(g, yoursRoots)
// calculate root repo packages, as well as their
// immediate vendor package dependencies
unfilteredMine := map[int]*graph.Node{}
for _, n := range g.Nodes() {
node, ok := n.(*graph.Node)
if !ok {
return nil, nil, nil, fmt.Errorf("expected node to analyze to be of type *graph.Node. Got: %v", n)
}
if isVendorPackage(node) {
continue
}
// obtain immediate vendor package deps from the current node
// and aggregate those as well
for _, v := range g.From(n) {
if !isVendorPackage(v.(*graph.Node)) {
continue
}
unfilteredMine[v.ID()] = v.(*graph.Node)
}
}
mine := []*graph.Node{}
ours := []*graph.Node{}
for _, n := range unfilteredMine {
// determine if the current origin node is reachable from any of the "yours" packages
if isReachableFrom(g, yours, n) {
ours = append(ours, n)
continue
}
mine = append(mine, n)
}
return yours, mine, ours, nil
}
// isVendorPackage receives a *graph.Node and
// returns true if the given node's unique name is in the vendor path.
func isVendorPackage(n *graph.Node) bool {
if strings.Contains(n.UniqueName, "/vendor/") {
return true
}
return false
}
// isReachableFrom receives a set of root nodes and determines
// if a given destination node can be reached from any of them.
func isReachableFrom(g *graph.MutableDirectedGraph, roots []*graph.Node, dest *graph.Node) bool {
for _, root := range roots {
// no negative edge weights, use Dijkstra
paths := path.DijkstraFrom(root, g)
if pathTo, _ := paths.To(dest); len(pathTo) > 0 {
return true
}
}
return false
}