-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
188 lines (160 loc) · 4.54 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"github.com/go-enry/go-license-detector/v4/licensedb"
)
// structs
type FileLicenseMapping struct {
Software Category `json:"software"`
Documentation Category `json:"documentation"`
Multimedia Category `json:"multimedia"`
DataSetsAndModels Category `json:"data_sets_and_models"`
}
type Category struct {
Extensions []string `json:"extensions"`
Licenses []string `json:"licenses"`
}
func cloneRepo(repoURL, dir string) error {
cmd := exec.Command("git", "clone", "--depth", "1", repoURL, dir)
return cmd.Run()
}
func loadMapping() (FileLicenseMapping, error) {
var mapping FileLicenseMapping
data, err := ioutil.ReadFile("mapping.json")
if err != nil {
return mapping, err
}
err = json.Unmarshal(data, &mapping)
return mapping, err
}
func getRootLicenses(dir string) ([]string, error) {
results := licensedb.Analyse(dir)
var rootLicenses []string
for _, result := range results {
fileLicenses := make(map[string]licensedb.Match)
for _, match := range result.Matches {
if existingMatch, exists := fileLicenses[match.File]; !exists || match.Confidence > existingMatch.Confidence {
fileLicenses[match.File] = match
}
}
for _, license := range fileLicenses {
rootLicenses = append(rootLicenses, license.License)
}
if len(rootLicenses) == 0 {
return nil, fmt.Errorf("no root licenses found")
}
}
return rootLicenses, nil
}
func contains(slice []string, item string) bool {
for _, sliceItem := range slice {
if sliceItem == item {
return true
}
}
return false
}
func isLicenseApplicable(extension string, rootLicenses []string, mapping FileLicenseMapping) bool {
var categories []Category = []Category{
mapping.Software, mapping.Documentation,
mapping.Multimedia, mapping.DataSetsAndModels,
}
for _, license := range rootLicenses {
for _, category := range categories {
if contains(category.Extensions, extension) && contains(category.Licenses, license) {
return true
}
}
}
return false
}
func checkFiles(dir string, rootLicenses []string, mapping FileLicenseMapping) map[string]bool {
fileChecks := make(map[string]bool)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// skip hidden directories: .git, .github, etc.
if path != dir && info.IsDir() && strings.HasPrefix(info.Name(), ".") {
return filepath.SkipDir
}
// skip files in the root directory: LICENSE, README.md, etc.
if path == dir || !info.IsDir() && filepath.Dir(path) == dir {
return nil
}
if !info.IsDir() {
// check if its license is applicable
relativePath, err := filepath.Rel(dir, path)
if err != nil {
log.Printf("Error getting relative path for %s: %v", path, err)
return nil
}
extension := filepath.Ext(relativePath)
applicable := isLicenseApplicable(extension, rootLicenses, mapping)
fileChecks[relativePath] = applicable
}
return nil
})
if err != nil {
log.Fatalf("Error walking directory: %v", err)
}
return fileChecks
}
func main() {
if len(os.Args) != 2 {
log.Fatal("Usage: go run script.go <repo-url>")
}
repoURL := os.Args[1]
mapping, err := loadMapping()
if err != nil {
log.Fatalf("Failed to load mapping: %v", err)
}
dir := filepath.Join(".", strings.Split(filepath.Base(repoURL), ".")[0])
if err := cloneRepo(repoURL, dir); err != nil {
log.Fatalf("Failed to clone repository: %v", err)
}
defer func() { exec.Command("rm", "-rf", dir).Run() }()
rootLicenses, err := getRootLicenses(dir)
if err != nil {
log.Fatalf("Failed to determine root licenses: %v", err)
}
fmt.Printf("Project: %s\n", strings.Split(filepath.Base(repoURL), ".")[0])
for _, license := range rootLicenses {
fmt.Printf("License: %s\n", license)
}
fileChecks := checkFiles(dir, rootLicenses, mapping)
var files []string
for file := range fileChecks {
files = append(files, file)
}
sort.Strings(files)
// some stats
totalFiles := 0
compliantFiles := 0
notCompliantFiles := 0
fmt.Println("\nFiles:")
for _, file := range files {
applicable := fileChecks[file]
totalFiles++
if applicable {
fmt.Printf("%s: ✅\n", file)
compliantFiles++
} else {
fmt.Printf("%s: ❌\n", file)
notCompliantFiles++
}
}
fmt.Printf("\nTotal files: %d\n", totalFiles)
fmt.Printf("Compliant files: %d\n", compliantFiles)
fmt.Printf("Non-compliant files: %d\n", notCompliantFiles)
score := float64(compliantFiles) / float64(totalFiles) * 100
fmt.Printf("\nScore: %.2f%%\n", score)
}