forked from boyter/lc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guesser.go
369 lines (296 loc) · 9.77 KB
/
guesser.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
package parsers
import (
"encoding/base64"
"encoding/json"
"fmt"
vectorspace "github.com/boyter/golangvectorspace"
"github.com/briandowns/spinner"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
)
// Shared all over the place
var ToolName = "licensechecker"
var ToolVersion = "1.0.1"
// Set by user as command line arguments
var confidence = 0.0
var Confidence = ""
var PossibleLicenceFiles = ""
var DirPath = "."
var PathBlacklist = ""
var deepGuess = true
var DeepGuess = ""
var Format = ""
var FileOutput = ""
var ExtentionBlacklist = ""
var maxSize = 0
var MaxSize = ""
var DocumentName = ""
var PackageName = ""
var DocumentNamespace = ""
var spdxLicenceRegex = regexp.MustCompile(`SPDX-License-Identifier:\s+(.*)[ |\n|\r\n]`)
var alphaNumericRegex = regexp.MustCompile("[^a-zA-Z0-9 ]")
var multipleSpacesRegex = regexp.MustCompile("\\s+")
func cleanText(content string) string {
content = strings.ToLower(content)
content = alphaNumericRegex.ReplaceAllString(content, " ")
content = multipleSpacesRegex.ReplaceAllString(content, " ")
return content
}
// Identify licenses in the text which is using the SPDX indicator
// Can return multiple license matches
func identifierGuessLicence(content string, licenses []License) []LicenseMatch {
matchingLicenses := []LicenseMatch{}
matches := spdxLicenceRegex.FindAllStringSubmatch(content, -1)
for _, val := range matches {
for _, license := range licenses {
if license.LicenseId == strings.TrimSpace(val[1]) {
matchingLicenses = append(matchingLicenses, LicenseMatch{LicenseId: license.LicenseId, Percentage: 1})
}
}
}
return matchingLicenses
}
// Fast method of checking if supplied content contains a licence using
// matching keyword ngrams to find if the licence is a match or not
// returns the maching licences with shortname and the percentage of match.
func keywordGuessLicense(content string, licenses []License) []LicenseMatch {
content = cleanText(content)
matchingLicenses := []LicenseMatch{}
for _, license := range licenses {
keywordmatch := 0
contains := false
for _, keyword := range license.Keywords {
contains = strings.Contains(content, strings.ToLower(keyword))
if contains == true {
keywordmatch++
}
}
if keywordmatch > 0 {
percentage := (float64(keywordmatch) / float64(len(license.Keywords))) * 100
matchingLicenses = append(matchingLicenses, LicenseMatch{LicenseId: license.LicenseId, Percentage: percentage})
}
}
return matchingLicenses
}
// Parses the supplied file content against the list of licences and
// returns the matching licences with the shortname and the percentage of match.
// If fast lookup methods fail it will try deep matching which is slower.
func guessLicense(content string, deepguess bool, licenses []License) []LicenseMatch {
matchingLicenses := []LicenseMatch{}
for _, license := range keywordGuessLicense(content, licenses) {
matchingLicense := License{}
for _, l := range licenses {
if l.LicenseId == license.LicenseId {
matchingLicense = l
break
}
}
runecontent := []rune(content)
trimto := utf8.RuneCountInString(matchingLicense.LicenseText)
if trimto > len(runecontent) {
trimto = len(runecontent)
}
contentConcordance := vectorspace.BuildConcordance(string(runecontent[:trimto]))
relation := vectorspace.Relation(matchingLicense.Concordance, contentConcordance)
if relation >= confidence {
matchingLicenses = append(matchingLicenses, LicenseMatch{LicenseId: license.LicenseId, Percentage: relation})
}
}
if len(matchingLicenses) == 0 && deepguess == true {
for _, license := range licenses {
runecontent := []rune(content)
trimto := utf8.RuneCountInString(license.LicenseText)
if trimto > len(runecontent) {
trimto = len(runecontent)
}
contentConcordance := vectorspace.BuildConcordance(string(runecontent[:trimto]))
relation := vectorspace.Relation(license.Concordance, contentConcordance)
if relation >= confidence {
matchingLicenses = append(matchingLicenses, LicenseMatch{LicenseId: license.LicenseId, Percentage: relation})
}
}
}
sort.Slice(matchingLicenses, func(i, j int) bool {
return matchingLicenses[i].Percentage > matchingLicenses[j].Percentage
})
return matchingLicenses
}
func findPossibleLicenseFiles(fileList []string) []string {
possibleList := []string{}
for _, filename := range fileList {
possible := false
for _, indicator := range strings.Split(PossibleLicenceFiles, ",") {
if strings.Contains(strings.ToLower(filename), indicator) {
possible = true
}
}
for _, license := range loadDatabase() {
if strings.Split(filename, ".")[0] == strings.ToLower(license.LicenseId) {
possible = true
}
}
if possible == true {
possibleList = append(possibleList, filename)
}
}
return possibleList
}
// Caching the database load result reduces processing time by about 3x for this repository
var Database = []License{}
func loadDatabase() []License {
if len(Database) != 0 {
return Database
}
var database []License
data, _ := base64.StdEncoding.DecodeString(database_keywords)
_ = json.Unmarshal(data, &database)
for i, v := range database {
database[i].Concordance = vectorspace.BuildConcordance(strings.ToLower(v.LicenseText))
}
Database = database
return database
}
func walkDirectory(directory string, rootLicenses [][]LicenseMatch) []FileResult {
fileResults := []FileResult{}
all, _ := ioutil.ReadDir(directory)
directories := []string{}
files := []string{}
// Work out which directories and files we want to investigate
for _, f := range all {
if f.IsDir() {
add := true
for _, black := range strings.Split(PathBlacklist, ",") {
if f.Name() == black {
add = false
}
}
if add == true {
directories = append(directories, f.Name())
}
} else {
files = append(files, f.Name())
}
}
// Determine any possible licence files which would classify everything else
possibleLicenses := findPossibleLicenseFiles(files)
identifiedRootLicense := []LicenseMatch{}
for _, possibleLicense := range possibleLicenses {
content := string(readFile(filepath.Join(directory, possibleLicense)))
guessLicenses := guessLicense(content, deepGuess, loadDatabase())
if len(guessLicenses) != 0 {
identifiedRootLicense = append(identifiedRootLicense, guessLicenses[0])
}
}
if len(identifiedRootLicense) != 0 {
rootLicenses = append(rootLicenses, identifiedRootLicense)
}
// TODO fan this out to many GoRoutines and process in parallel
for _, file := range files {
rootLicense := []LicenseMatch{}
if len(rootLicenses) != 0 {
rootLicense = rootLicenses[len(rootLicenses)-1]
}
fileResult := processFile(directory, file, rootLicense)
fileResults = append(fileResults, fileResult)
if strings.ToLower(Format) == "progress" {
toProgress(directory, file, rootLicense, fileResult.LicenseGuesses, fileResult.LicenseIdentified)
}
}
for _, newdirectory := range directories {
results := walkDirectory(filepath.Join(directory, newdirectory), rootLicenses)
fileResults = append(fileResults, results...)
}
return fileResults
}
func processFile(directory string, file string, rootLicenses []LicenseMatch) FileResult {
process := true
for _, ext := range strings.Split(ExtentionBlacklist, ",") {
if strings.HasSuffix(file, "."+ext) {
// Needs to be smarter we should skip reading the contents but it should still be under the license in the root folders
process = false
}
}
content := readFile(filepath.Join(directory, file))
licenseGuesses := []LicenseMatch{}
licenseIdentified := []LicenseMatch{}
if len(content) > maxSize {
process = false
}
if process == true {
licenseGuesses = guessLicense(string(content), deepGuess, loadDatabase())
licenseIdentified = identifierGuessLicence(string(content), loadDatabase())
}
fileResult := FileResult{
Directory: directory,
Filename: file,
LicenseGuesses: licenseGuesses,
LicenseRoots: rootLicenses,
LicenseIdentified: licenseIdentified,
Md5Hash: getMd5Hash(content),
Sha1Hash: getSha1Hash(content),
Sha256Hash: getSha256Hash(content),
BytesHuman: bytesToHuman(len(content)),
Bytes: len(content)}
return fileResult
}
func processArguments() {
conf, err := strconv.ParseFloat(Confidence, 64)
if err == nil {
confidence = conf
} else {
fmt.Println("Using default confidence value")
}
size, err := strconv.ParseInt(MaxSize, 10, 32)
if err == nil {
maxSize = int(size)
} else {
fmt.Println("Using default filesize value")
}
deep, err := strconv.ParseBool(DeepGuess)
if err == nil {
deepGuess = deep
} else {
fmt.Println("Using default deepguess value")
}
}
func Process() {
processArguments()
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Writer = os.Stderr
s.Prefix = "Processing... "
if strings.ToLower(Format) != "progress" && runtime.GOOS != "windows" {
s.Start()
}
fileResults := []FileResult{}
if DirPath == "" {
DirPath = "."
}
if info, err := os.Stat(DirPath); err == nil && info.IsDir() {
fileResults = walkDirectory(DirPath, [][]LicenseMatch{})
} else {
fmt.Println("Argument " + DirPath + " should be a directory not a file")
}
s.Stop()
switch strings.ToLower(Format) {
case "csv":
toCSV(fileResults)
case "json":
toJSON(fileResults)
case "tabular":
toTabular(fileResults)
case "spdx21":
toSPDX21(fileResults)
case "spdx":
toSPDX21(fileResults)
default:
fmt.Println("")
}
}