-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
308 lines (266 loc) · 10.9 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
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
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/datawire/go-mkopensource/pkg/scanningerrors"
"io"
"net/textproto"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/datawire/dlib/derror"
"github.com/datawire/go-mkopensource/pkg/dependencies"
. "github.com/datawire/go-mkopensource/pkg/detectlicense"
)
type tuple struct {
Name string
Version string
License string
}
//nolint:gochecknoglobals // Would be 'const'.
var hardcodedPythonDependencies = map[tuple][]License{
// These are packages that don't have sufficient metadata to get
// the license normally. Either the license isn't specified in
// the metadata, or the license string that is specified is
// ambiguous (for example: "BSD" is too ambiguous, which variant
// of the BSD license is it?). We pin the exact versions so
// that a human has to go make sure that the license didn't
// change when upgrading.
{"Babel", "2.8.0", "BSD"}: {BSD3},
{"CacheControl", "0.12.6", "UNKNOWN"}: {Apache2},
{"CacheControl", "0.12.10", "UNKNOWN"}: {Apache2},
{"Click", "7.0", "BSD"}: {BSD3},
{"Flask", "1.0.2", "BSD"}: {BSD3},
{"GitPython", "3.1.8", "UNKNOWN"}: {BSD3},
{"GitPython", "3.1.11", "UNKNOWN"}: {BSD3},
{"Jinja2", "2.10.1", "BSD"}: {BSD3},
{"Pygments", "2.7.1", "BSD License"}: {BSD2},
{"Sphinx", "3.2.1", "BSD"}: {BSD2, BSD3, MIT},
{"chardet", "3.0.4", "LGPL"}: {LGPL21OrLater},
{"colorama", "0.4.3", "BSD"}: {BSD3},
{"colorama", "0.4.4", "BSD"}: {BSD3},
{"decorator", "4.4.2", "new BSD License"}: {BSD2},
{"gitdb", "4.0.5", "BSD License"}: {BSD3},
{"idna", "2.6", "BSD-like"}: {BSD3, PSF, Unicode2015},
{"idna", "2.7", "BSD-like"}: {BSD3, PSF, Unicode2015},
{"idna", "2.8", "BSD-like"}: {BSD3, PSF, Unicode2015},
{"idna", "2.9", "BSD-like"}: {BSD3, PSF, Unicode2015},
{"importlib-resources", "5.4.0", "UNKNOWN"}: {Apache2},
{"itsdangerous", "1.1.0", "BSD"}: {BSD3},
{"jsonpatch", "1.30", "Modified BSD License"}: {BSD3},
{"jsonpatch", "1.32", "Modified BSD License"}: {BSD3},
{"jsonpointer", "2.0", "Modified BSD License"}: {BSD3},
{"jsonpointer", "2.2", "Modified BSD License"}: {BSD3},
{"jsonschema", "3.2.0", "UNKNOWN"}: {MIT},
{"lockfile", "0.12.2", "UNKNOWN"}: {MIT},
{"oauthlib", "3.1.0", "BSD"}: {BSD3},
{"oauthlib", "3.2.0", "BSD"}: {BSD3},
{"pep517", "0.12.0", "UNKNOWN"}: {MIT},
{"pep517", "0.8.2", "UNKNOWN"}: {MIT},
{"pip-tools", "5.3.1", "BSD"}: {BSD3},
{"ptyprocess", "0.6.0", "UNKNOWN"}: {ISC},
{"pyasn1", "0.4.8", "BSD"}: {BSD2},
{"pycparser", "2.20", "BSD"}: {BSD3},
{"python-dateutil", "2.8.1", "Dual License"}: {BSD3, Apache2},
{"python-dateutil", "2.8.2", "Dual License"}: {BSD3, Apache2},
{"python-json-logger", "2.0.1", "BSD"}: {BSD2},
{"python-json-logger", "2.0.2", "BSD"}: {BSD2},
{"scout.py", "0.3.0", "UNKNOWN"}: {AmbassadorProprietary},
{"rsa", "4.8", "Apache-2.0"}: {Apache2},
{"semantic-version", "2.6.0", "BSD"}: {BSD2},
{"semantic-version", "2.8.5", "BSD"}: {BSD2},
{"smmap", "3.0.4", "BSD"}: {BSD3},
{"tomli", "1.2.2", "UNKNOWN"}: {MIT},
{"webencodings", "0.5.1", "BSD"}: {BSD3},
{"websocket-client", "0.57.0", "BSD"}: {BSD3},
{"websocket-client", "1.2.3", "Apache-2.0"}: {Apache2},
{"zipp", "3.6.0", "UNKNOWN"}: {MIT},
// These are packages with non-trivial strings to parse, and
// it's easier to just hard-code it.
{"docutils", "0.15.2", "public domain, Python, 2-Clause BSD, GPL 3 (see COPYING.txt)"}: {PublicDomain, PSF, BSD2, GPL3OrLater},
{"docutils", "0.17.1", "public domain, Python, 2-Clause BSD, GPL 3 (see COPYING.txt)"}: {PublicDomain, PSF, BSD2, GPL3OrLater},
{"docutils", "0.18.1", "public domain, Python, 2-Clause BSD, GPL 3 (see COPYING.txt)"}: {PublicDomain, PSF, BSD2, GPL3OrLater},
{"orjson", "3.3.1", "Apache-2.0 OR MIT"}: {Apache2, MIT},
{"orjson", "3.6.0", "Apache-2.0 OR MIT"}: {Apache2, MIT},
{"orjson", "3.6.6", "Apache-2.0 OR MIT"}: {Apache2, MIT},
{"packaging", "20.4", "BSD-2-Clause or Apache-2.0"}: {BSD2, Apache2},
{"packaging", "20.9", "BSD-2-Clause or Apache-2.0"}: {BSD2, Apache2},
}
func parseLicenses(name, version, license string) map[License]struct{} {
override, ok := hardcodedPythonDependencies[tuple{name, version, license}]
if ok {
ret := make(map[License]struct{}, len(override))
for _, l := range override {
ret[l] = struct{}{}
}
return ret
}
static, ok := map[string][]License{
"AGPL-3.0-or-later": {AGPL3OrLater},
"ASL 2": {Apache2},
"Apache": {Apache2},
"Apache 2": {Apache2},
"Apache 2.0": {Apache2},
"Apache-2.0": {Apache2},
"Apache-2.0 license": {Apache2},
"Apache License": {Apache2},
"Apache License 2.0": {Apache2},
"Apache License Version 2.0": {Apache2},
"Apache License, Version 2.0": {Apache2},
"Apache Software License": {Apache2},
"Apache Software License 2.0": {Apache2},
"BSD-2-Clause": {BSD2},
"3-Clause BSD License": {BSD3},
"BSD 3 Clause": {BSD3},
"BSD-3-Clause": {BSD3},
"ISC license": {ISC},
"ISC": {ISC},
"MIT License": {MIT},
"MIT license": {MIT},
"MIT": {MIT},
"MIT-LICENSE": {MIT},
"Mozilla Public License 2.0 (MPL 2.0)": {MPL2},
"MPL-2.0": {MPL2},
"PSF License": {PSF},
"PSF": {PSF},
"Python license": {PSF},
}[license]
if ok {
ret := make(map[License]struct{}, len(static))
for _, l := range static {
ret[l] = struct{}{}
}
return ret
}
return nil
}
func Main(outputType OutputType, applicationType ApplicationType, r io.Reader, w io.Writer) error {
distribs := make(map[string]textproto.MIMEHeader)
input := textproto.NewReader(bufio.NewReader(r))
for {
distrib, err := input.ReadMIMEHeader()
if err != nil {
if err == io.EOF {
break
}
return err
}
distribs[distrib.Get("Name")+"@"+distrib.Get("Version")] = distrib
}
distribNames := make([]string, 0, len(distribs))
for distribName := range distribs {
distribNames = append(distribNames, distribName)
}
sort.Strings(distribNames)
licenseRestriction := getLicenseRestriction(applicationType)
dependencyInfo, err := getDependencies(distribNames, distribs, licenseRestriction)
if err != nil {
return err
}
switch outputType {
case jsonOutputType:
if err := jsonOutput(w, dependencyInfo); err != nil {
return err
}
default:
if err := markdownOutput(w, dependencyInfo); err != nil {
return err
}
}
return nil
}
func jsonOutput(w io.Writer, dependencyInfo dependencies.DependencyInfo) error {
jsonString, marshalErr := json.Marshal(dependencyInfo)
if marshalErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "Could not generate JSON output: %v\n", marshalErr)
os.Exit(int(MarshallJsonError))
}
if _, err := w.Write(jsonString); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Could not write JSON output: %v\n", err)
os.Exit(int(WriteError))
}
_, _ = fmt.Fprintf(w, "\n")
return nil
}
func markdownOutput(w io.Writer, dependencyInfo dependencies.DependencyInfo) error {
table := tabwriter.NewWriter(w, 0, 8, 2, ' ', 0)
_, _ = io.WriteString(table, " \tName\tVersion\tLicense(s)\n")
_, _ = io.WriteString(table, " \t----\t-------\t----------\n")
for _, dependency := range dependencyInfo.Dependencies {
distribName := dependency.Name
distribVersion := dependency.Version
distribLicense := strings.Join(dependency.Licenses, ", ")
if _, err := fmt.Fprintf(table, "\t%s\t%s\t%s\n", distribName, distribVersion, distribLicense); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Could not write Markdown output: %v\n", err)
os.Exit(int(WriteError))
}
}
if _, err := fmt.Fprintf(w, "The Emissary-ingress Python code makes use of the following Free and Open Source\nlibraries:\n\n"); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Could not write Markdown output: %v\n", err)
os.Exit(int(WriteError))
}
if err := table.Flush(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Could not write Markdown output: %v\n", err)
os.Exit(int(WriteError))
}
return nil
}
func getDependencies(distribNames []string, distribs map[string]textproto.MIMEHeader,
licenseRestriction LicenseRestriction) (dependencies.DependencyInfo, error) {
dependencyInfo := dependencies.NewDependencyInfo()
var errs derror.MultiError
for _, versionedName := range distribNames {
distribName := strings.Split(versionedName, "@")[0]
distrib := distribs[versionedName]
distribVersion := distrib.Get("Version")
dependencyDetails := dependencies.Dependency{
Name: distribName,
Version: distribVersion,
}
licenses := parseLicenses(distribName, distribVersion, distrib.Get("License"))
if licenses == nil {
errs = append(errs, fmt.Errorf("distrib %q %q: Could not parse license-string %q", distribName, distribVersion, distrib.Get("License")))
continue
}
licenseList := make([]string, 0, len(licenses))
for license := range licenses {
licenseList = append(licenseList, license.Name)
if err := dependencies.CheckLicenseRestrictions(dependencyDetails, license.Name, licenseRestriction); err != nil {
errs = append(errs, err)
}
}
sort.Strings(licenseList)
dependencyDetails.Licenses = licenseList
dependencyInfo.Dependencies = append(dependencyInfo.Dependencies, dependencyDetails)
}
if len(errs) > 0 {
return dependencyInfo, scanningerrors.ExplainErrors(errs)
}
if err := dependencyInfo.UpdateLicenseList(); err != nil {
return dependencyInfo, fmt.Errorf("Could not generate list of license URLs: %v\n", err)
}
return dependencyInfo, nil
}
func main() {
cliArgs, err := parseArgs()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s: %v\nTry '%s --help' for more information.\n", os.Args[0], err, os.Args[0])
os.Exit(int(InvalidArgumentsError))
}
if err := Main(*cliArgs.outputType, *cliArgs.applicationType, os.Stdin, os.Stdout); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s: fatal: %v\n", os.Args[0], err)
os.Exit(int(DependencyGenerationError))
}
}
func getLicenseRestriction(applicationType ApplicationType) LicenseRestriction {
var LicenseRestriction LicenseRestriction
switch applicationType {
case internalApplication:
LicenseRestriction = AmbassadorServers
default:
LicenseRestriction = Unrestricted
}
return LicenseRestriction
}