This repository has been archived by the owner on Jul 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotice.py
executable file
·188 lines (164 loc) · 6.4 KB
/
notice.py
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
#!/usr/bin/env python3
import subprocess
import json
import os.path
import sys
import hashlib
raw_mods = subprocess.check_output(["go", "list", "--json", "-m", "all"]).decode('UTF-8')
mods = [json.loads(x+"}") for x in raw_mods.split("\n}\n") if x.strip() != ""]
placeholdersInLicenses = {
"BSD-2-Clause": {
"<year>": "year",
"<owner>": "licensor"
},
"BSD-3-Clause": {
"<year>": "year",
"<owner>": "licensor"
},
"MIT": {
"<year>": "year",
"<copyright holders>": "licensor"
},
"Apache-2.0": {},
"ISC": {
"<YEAR>": "year",
"<OWNER>": "licensor"
},
}
shaLookup = {}
if os.path.isfile('.notice/3rd-party-lookup.json'):
with open('.notice/3rd-party-lookup.json') as file:
shaLookup = json.load(file)
def saveLookup(sha, key, value):
if sha not in shaLookup:
shaLookup[sha] = {}
shaLookup[sha][key] = value
with open('.notice/3rd-party-lookup.json', 'w') as file:
json.dump(shaLookup, file, indent=4)
def lookup(sha, key):
if sha not in shaLookup:
return None
if key not in shaLookup[sha]:
return None
return shaLookup[sha][key]
def sha256(file):
hash = hashlib.sha256()
with open(file, 'r') as file:
hash.update(file.read().encode('UTF-8'))
return hash.hexdigest()
def detect(basedir, key, suffixes, fallback):
for suffix in suffixes:
file = basedir + "/" + suffix
if os.path.isfile(file):
sha = sha256(file)
name = lookup(sha, key)
if name is None:
with open(file, 'r') as file:
print(file.read())
name = input("What %s is this (%s): " % (key, file.name))
saveLookup(sha, key, name)
if name != "":
return name
name = lookup(fallback, key)
if name is None:
name = input("What %s is this '%s': " % (key, fallback))
saveLookup(fallback, key, name)
if name != "":
return name
print("Could not detect %s for gomod %s" % (key, mod))
sys.exit(1)
licenses = [{
"license": "MIT",
"component": "Visual Studio Code extension generator 1.2.11",
"website": "https://github.com/Microsoft/vscode-generator-code",
"licensor": "Microsoft Corporation",
"year": "unspecified",
}, {
"license": "MIT",
"component": "The Visual Studio Code Extension Manager",
"licensor": "Microsoft Corporation",
"website": "https://github.com/microsoft/vscode-vsce",
"year": "unspecified",
}, {
"license": "Apache-2.0",
"component": "Kubernetes OpenAPI Spec",
"licensor": "The Kubernetes Authors",
"website": "https://github.com/kubernetes/kubernetes/blob/master/api/openapi-spec/swagger.json",
"year": "unspecified",
}, {
"license": "Apache-2.0",
"component": "concourse-pipeline-jsonschema",
"licensor": "Shu Kutsuzawa [email protected]",
"website": "https://github.com/cappyzawa/concourse-pipeline-jsonschema",
"year": "2020",
}]
for mod in mods:
if ("Main" in mod and mod["Main"]) or ("Indirect" in mod and mod["Indirect"]):
continue
license = detect(mod["Dir"], "license", ["LICENSE", "License"], mod["Path"])
licensor = detect(mod["Dir"], "licensor", ["NOTICE", "LICENSE", "License"], mod["Path"])
year = detect(mod["Dir"], "year", ["NOTICE", "LICENSE", "License"], mod["Path"])
website = detect(mod["Dir"], "website", [], mod["Path"])
#print(mod, license)
licenses.append({
"license": license,
"licensor": licensor,
"component": "%s %s" % (mod["Path"], mod["Version"]),
"website": website,
"year": year,
})
print(mod)
dependencies = []
with open("vscode/package.json") as file:
package = json.load(file)
if "dependencies" in package:
dependencies.extend(package["dependencies"])
if "devDependencies" in package:
dependencies.extend(package["devDependencies"])
#print(dependencies)
for dep in dependencies:
with open("vscode/node_modules/" + dep + "/package.json") as file:
package = json.load(file)
author = ""
if "author" in package and "name" in package["author"]:
author = package["author"]["name"]
else:
author = detect("vscode/node_modules/" + dep, "licensor", ["NOTICE", "LICENSE", "License"], dep)
year = detect("vscode/node_modules/" + dep, "year", ["NOTICE", "LICENSE", "License"], dep)
licenses.append({
"license": package["license"],
"component": "%s %s" % (package["name"], package["version"]),
"website": package["homepage"],
"licensor": author,
"year": year,
})
with open("THIRD-PARTY-NOTICES.txt", "w") as target:
print("ytt lint uses third-party software or other resources that may be distributed under licenses different than ytt lint software.", file=target)
print("", file=target)
print("In the event that we overlooked to list required notice, please bring this to our attention by contacting us via this email: [email protected]", file=target)
print("", file=target)
print("", file=target)
print("", file=target)
print("------------------------------------------------------------------------------------------", file=target)
print("", file=target)
print("", file=target)
print("Components:", file=target)
for license in licenses:
print("", file=target)
print("Component:", license["component"], file=target)
print("Licensor:", license["licensor"], file=target)
print("Website:", license["website"], file=target)
print("License:", license["license"], file=target)
placeholders = placeholdersInLicenses[license["license"]]
for placeholder in placeholders:
lookupKey = placeholders[placeholder]
print("%s = %s" % (placeholder, license[lookupKey]), file=target)
for license in set([x["license"] for x in licenses]):
#print(license)
cache = "./.notice/license-%s" % license
if not os.path.isfile(cache):
print("License text for %s not found in %s. Try looking at https://github.com/spdx/license-list-data/tree/master/text or https://opensource.org/licenses/alphabetical" % (license, cache))
sys.exit(1)
print("\n\n" + (30*"-") + " " + license + " " + (30*"-") + "\n\n", file=target)
with open(cache, 'r') as file:
print(file.read(), file=target)