-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgdlsc.go
197 lines (170 loc) · 4.12 KB
/
gdlsc.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"github.com/ryanuber/go-license"
)
const vendorDir = "vendor"
// node describes a directory in the vendor tree
type node struct {
prefix string
name string
golang bool
licenseTxt string
children []*node
reduced bool
}
// holder allows to append while keeping a reference to the slice
type holder struct {
nodes []*node
}
// makeTree parses the vendor tree
func makeTree(p string) *node {
n := &node{prefix: path.Dir(p), name: path.Base(p), children: make([]*node, 0)}
files, err := ioutil.ReadDir(p)
if err != nil {
panic(err)
}
for _, file := range files {
if file.Mode().IsRegular() {
if strings.HasSuffix(file.Name(), ".go") {
n.golang = true
} else if isLicenseFileName(file.Name()) {
license, err := ioutil.ReadFile(path.Join(p, file.Name()))
if err != nil {
panic(err)
}
n.licenseTxt = string(license)
}
} else if file.Mode().IsDir() && !strings.HasPrefix(file.Name(), ".") && file.Name() != "ConnectCorp" {
n.children = append(n.children,
makeTree(path.Join(p, file.Name())))
}
}
return n
}
// Extract takes out some of the minimal licensed subtrees
func (n *node) extract(h *holder) bool {
extracted := false
newChildren := make([]*node, 0, len(n.children))
for i := 0; i < len(n.children); i++ {
child := n.children[i]
license := findLicense(child.children)
if child.licenseTxt != "" && !license {
child.reduced = true
child.children = []*node{}
h.nodes = append(h.nodes, child)
extracted = true
} else {
newChildren = append(newChildren, child)
if child.extract(h) {
extracted = true
}
}
}
n.children = newChildren
return extracted
}
// AttachLicenseType finds the appropriate licence types and attaches it
func (n *holder) attachLicenseType() {
for i := 0; i < len(n.nodes); i++ {
child := n.nodes[i]
if child.licenseTxt != "" {
child.licenseTxt = getLicenseType(child.licenseTxt)
}
}
}
func (n *node) reduce() bool {
reduced := false
for i := 0; i < len(n.children); i++ {
child := n.children[i]
if !n.golang && child.golang && len(child.children) > 0 {
child.reduced = true
child.children = []*node{}
reduced = true
} else {
if child.reduce() {
reduced = true
}
}
}
return reduced
}
// format the given tree for output
func (n *node) format() []string {
r := ""
if n.reduced {
r = "/..."
}
if n.licenseTxt != "" {
r += " ======>"
}
out := make([]string, 0)
if n.golang || n.licenseTxt != "" {
out = append(out, fmt.Sprintf("%v/%v%v %v", n.prefix, n.name, r, n.licenseTxt))
}
for _, child := range n.children {
out = append(out, child.format()...)
}
return out
}
func isLicenseFileName(name string) bool {
name = strings.ToUpper(name)
return strings.Contains(name, "LICENSE") || strings.Contains(name, "COPYING")
}
func isWtfLicense(text string) bool {
match := "do what the fuck you want to public license version 2"
rawText := strings.ToLower(text)
formattedText := regexp.MustCompile("\\s{2,}").ReplaceAllLiteralString(rawText, " ")
return strings.Contains(formattedText, match)
}
func formatLicenseText(text string) string {
data := strings.Split(text, "\n")
if len(data) >= 10 {
data = data[0:10]
}
formatted := "UNKOWN LICENSE\n|\t" + strings.Join(data, "\n|\t") + "\n"
return formatted
}
func getLicenseType(text string) string {
l := new(license.License)
l.Text = text
if err := l.GuessType(); err != nil {
if isWtfLicense(text) {
return "WTFPL-2.0"
}
return formatLicenseText(text)
}
return l.Type
}
// finds a license in a forest
func findLicense(nodes []*node) bool {
for i := 0; i < len(nodes); i++ {
if nodes[i].licenseTxt != "" {
return true
}
if findLicense(nodes[i].children) {
return true
}
}
return false
}
func main() {
n := makeTree(path.Join(os.Args[1], vendorDir))
h := &holder{nodes: make([]*node, 0)}
for n.extract(h) {
}
h.attachLicenseType()
for n.reduce() {
}
fmt.Println("LICENSED")
for _, e := range h.nodes {
fmt.Println(strings.Join(e.format(), "\n"))
}
fmt.Println("UNLICENSED")
fmt.Println(strings.Join(n.format(), "\n"))
}