-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
walker.go
386 lines (329 loc) · 8.66 KB
/
walker.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/emicklei/dot"
"github.com/google/licensecheck"
log "github.com/sirupsen/logrus"
)
const (
unknownLicense string = "UNKNOWN"
)
var (
// FileNames used to search for licenses
// "LICENSE.docs" and "LICENCE.docs" are excluded from the list as we only care about source code in the repo.
FileNames = []string{
"COPYING",
"COPYING.md",
"COPYING.markdown",
"COPYING.txt",
"LICENCE",
"LICENCE.md",
"LICENCE.markdown",
"LICENCE.txt",
"LICENSE",
"LICENSE.md",
"LICENSE.markdown",
"LICENSE.txt",
"LICENSE-2.0.txt",
"LICENCE-2.0.txt",
"LICENSE-APACHE",
"LICENCE-APACHE",
"LICENSE-APACHE-2.0.txt",
"LICENCE-APACHE-2.0.txt",
"LICENSE-MIT",
"LICENCE-MIT",
"LICENSE.MIT",
"LICENCE.MIT",
"LICENSE.code",
"LICENCE.code",
"LICENSE.rst",
"LICENCE.rst",
"MIT-LICENSE",
"MIT-LICENCE",
"MIT-LICENSE.md",
"MIT-LICENCE.md",
"MIT-LICENSE.markdown",
"MIT-LICENCE.markdown",
"MIT-LICENSE.txt",
"MIT-LICENCE.txt",
"MIT_LICENSE",
"MIT_LICENCE",
"UNLICENSE",
"UNLICENCE",
}
)
// fileNamesLowercase has all the entries of FileNames, lower cased and made a set
// for fast case-insensitive matching.
var fileNamesLowercase = map[string]bool{}
func init() {
for _, f := range FileNames {
fileNamesLowercase[strings.ToLower(f)] = true
}
}
// dependencies are tracked as a graph, but the graph itself is not used to build the node list
type dependencies struct {
nodes []*node
nodesList map[string]bool
edges map[node][]*node
dotGraph *dot.Graph
checkTest bool
sync.RWMutex
}
type node struct {
pkg string
dir string
vendor string
}
func newGraph(checkTest bool) *dependencies {
var g dependencies
g.nodesList = make(map[string]bool)
g.checkTest = checkTest
return &g
}
// AddNode adds a node to the graph
func (g *dependencies) addNode(n *node) error {
log.Debugf("[%s] current nodesList status %+v", n.pkg, g.nodesList)
// check if Node has been visited, this is done raw by caching it in a global hashtable
if !g.nodesList[n.pkg] {
g.Lock()
g.nodes = append(g.nodes, n)
g.nodesList[n.pkg] = true
g.Unlock()
return nil
}
return fmt.Errorf("[%s] node already visited", n.pkg)
}
// addEdge adds an edge to the graph
func (g *dependencies) addEdge(n1, n2 *node) {
g.Lock()
if g.edges == nil {
g.edges = make(map[node][]*node)
}
g.edges[*n1] = append(g.edges[*n1], n2)
g.edges[*n2] = append(g.edges[*n2], n1)
g.Unlock()
}
func (g *dependencies) getDotGraph() string {
g.dotGraph = dot.NewGraph(dot.Directed)
g.generateDotGraph(func(n *node) {})
return g.dotGraph.String()
}
type nodeQueue struct {
items []node
sync.RWMutex
}
func (s *nodeQueue) new() *nodeQueue {
s.Lock()
s.items = []node{}
s.Unlock()
return s
}
func (s *nodeQueue) enqueue(t node) {
s.Lock()
s.items = append(s.items, t)
s.Unlock()
}
func (s *nodeQueue) dequeue() *node {
s.Lock()
item := s.items[0]
s.items = s.items[1:len(s.items)]
s.Unlock()
return &item
}
func (s *nodeQueue) isEmpty() bool {
s.RLock()
defer s.RUnlock()
return len(s.items) == 0
}
// do a BFS on the graph and generate dot.Graph
func (g *dependencies) generateDotGraph(f func(*node)) {
g.RLock()
q := nodeQueue{}
q.new()
n := g.nodes[0]
q.enqueue(*n)
visited := make(map[*node]bool)
for {
if q.isEmpty() {
break
}
node := q.dequeue()
// add dotGraph node after dequeing
dGN := g.dotGraph.Node(node.pkg)
visited[node] = true
near := g.edges[*node]
for i := 0; i < len(near); i++ {
j := near[i]
if !visited[j] {
// add unvisited node to dotGraph
edGN := g.dotGraph.Node(j.pkg)
// add an edge in the dotGraph between ancestor and descendant
g.dotGraph.Edge(dGN, edGN)
q.enqueue(*j)
visited[j] = true
}
}
if f != nil {
f(node)
}
}
g.RUnlock()
}
func (g *dependencies) WalkNode(n *node) {
var walkFn = func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
log.Debugf("walking %q", path)
// check if we need to skip this
if ok, err := shouldSkip(path, info, g.checkTest); ok {
return err
}
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, path, nil, parser.ImportsOnly)
if err != nil {
return err
}
for _, s := range f.Imports {
vendorpkg := strings.Replace(s.Path.Value, "\"", "", -1)
log.Debugf("found import %q", vendorpkg)
pkgdir := filepath.Join(n.vendor, "vendor", vendorpkg)
if _, err := os.Stat(pkgdir); !os.IsNotExist(err) {
// Add imported pkg to the graph
var vendornode = node{pkg: vendorpkg, dir: pkgdir, vendor: n.vendor}
log.Debugf("[%s] adding node", vendornode.pkg)
if err := g.addNode(&vendornode); err != nil {
log.Debug(err.Error())
continue
}
log.Debugf("[%s] adding node as edge of %s", vendornode.pkg, n.pkg)
g.addEdge(n, &vendornode)
log.Debugf("[%s] walking node", vendornode.pkg)
g.WalkNode(&vendornode)
}
}
return nil
}
if err := filepath.Walk(n.dir, walkFn); err != nil {
return
}
}
func WalkImports(root string, checkTest bool) (map[string]bool, error) {
graph := newGraph(checkTest)
rootNode := node{pkg: "root", dir: root, vendor: root}
if err := graph.addNode(&rootNode); err != nil {
log.Debug(err.Error())
}
log.Debugf("[%s] walking root node", rootNode.pkg)
graph.WalkNode(&rootNode)
return graph.nodesList, nil
}
func GraphImports(root string, checkTest bool) (string, error) {
graph := newGraph(checkTest)
rootNode := node{pkg: "root", dir: root, vendor: root}
if err := graph.addNode(&rootNode); err != nil {
log.Debug(err.Error())
}
log.Debugf("[%s] walking root node", rootNode.pkg)
graph.WalkNode(&rootNode)
return graph.getDotGraph(), nil
}
func GetLicenses(root string, list map[string]bool, threshold float64) map[string]string {
checker, err := licensecheck.NewScanner(licensecheck.BuiltinLicenses())
if err != nil {
log.Fatal("Cannot initialize LicenseChecker")
}
var lics = make(map[string]string)
if !strings.HasSuffix(root, "vendor") {
root = filepath.Join(root, "vendor")
}
log.Debug("Start walking paths for LICENSE discovery")
for k := range list {
var fpath = filepath.Join(root, k)
pkg, err := os.Stat(fpath)
if err != nil {
continue
}
if pkg.IsDir() {
log.Debugf("Walking path: %s", fpath)
waa := scanDir(checker, fpath, threshold)
if waa != "" {
lics[k] = waa
}
}
}
return lics
}
func scanDir(checker *licensecheck.Scanner, fpath string, threshold float64) string {
var license = ""
filesInDir, err := ioutil.ReadDir(fpath)
if err != nil {
return ""
}
for _, f := range filesInDir {
log.Debugf("Evaluating: %s", f.Name())
// if it's a directory or not in the list of well-known license files, we skip
if f.IsDir() || !fileNamesLowercase[strings.ToLower(f.Name())] {
log.Debugf("Skipping...")
continue
}
// Read the license file
text, err := ioutil.ReadFile(filepath.Join(fpath, f.Name()))
if err != nil {
log.Errorf("Cannot read file: %s because: %s", filepath.Join(fpath, f.Name()), err.Error())
continue
}
// Verify against the checker
cov := checker.Scan(text)
log.Debugf("%.1f%% of text covered by licenses:\n", cov.Percent)
for _, m := range cov.Match {
log.Debugf("%s at [%d:%d] IsURL=%v\n", m.ID, m.Start, m.End, m.IsURL)
}
// If the threshold is met, we qualify the license
if cov.Percent >= threshold {
license = cov.Match[0].ID
}
}
// if we didn't find any licenses after walking the path, we pop one out from it
if license == "" {
pak := strings.Split(filepath.ToSlash(fpath), "/")
// if we're 1 directories removed from vendor/ that means we couldn't find a decent license file
if pak[len(pak)-2] != "vendor" {
log.Debugf("Recursive call to scanDir starting from: %s going to: %s", fpath, filepath.FromSlash(strings.Join(pak[:len(pak)-1], "/")))
license = scanDir(checker, filepath.FromSlash(strings.Join(pak[:len(pak)-1], "/")), threshold)
}
}
if license == "" {
license = unknownLicense
}
return license
}
func shouldSkip(path string, info os.FileInfo, checkTest bool) (bool, error) {
if info.IsDir() {
name := info.Name()
// check if directory is in the blocklist
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") || name == "testdata" || name == "vendor" {
log.Debugf("skipping %q: directory in blocklist", path)
return true, filepath.SkipDir
}
return true, nil
}
// if it's not a .go file, skip
if filepath.Ext(path) != ".go" {
log.Debugf("skipping %q: not a go file", path)
return true, nil
}
// if it's a test file, skip
if strings.HasSuffix(path, "_test.go") && !checkTest {
log.Debugf("skipping %q: test file", path)
return true, nil
}
return false, nil
}