forked from offen/offen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_notice.py
executable file
·90 lines (71 loc) · 2.61 KB
/
create_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
# Copyright 2020-2021 - Offen Authors <[email protected]>
# SPDX-License-Identifier: Apache-2.0
import csv
import argparse
import re
"""
This script is used to automatically generate a NOTICE file from different
csv sources. Right now, input from `npm-license-crawler` [0] (for npm managed
dependencies) and `license_finder` (for Go modules) [1] is supported.
[0]: https://www.npmjs.com/package/npm-license-crawler
[1]: https://github.com/pivotal/LicenseFinder
"""
def normalize_row(row):
is_versioned_go_module = re.compile(r".*/v\d+$")
result = {}
try:
result["name"] = row["module name"]
except KeyError:
repository = row["repository"]
chunks = repository.split("/")
if is_versioned_go_module.match(repository):
result["name"] = chunks[-2]
else:
result["name"] = chunks[-1]
result["licenses"] = row["licenses"]
result["source"] = (
row["repository"]
if row["repository"].startswith("http")
else "https://{}".format(row["repository"])
)
return result
def read_file(filename):
result = []
with open(filename, "r") as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
result.append(normalize_row(row))
return result
def dedupe(dependencies):
result = []
for dep in dependencies:
if not any(existing["name"] == dep["name"] for existing in result):
result.append(dep)
return result
def main(**kwargs):
for key, values in kwargs.items():
deps = []
for source in values:
deps += read_file(source)
deps = dedupe(deps)
if deps:
headline = "{} side:".format(key.title())
print("\n{}\n{}\n".format(headline, "="*len(headline)))
for dep in deps:
print(
'"{}" licensed under {}, available at <{}>'.format(
dep["name"].strip(), dep["licenses"].strip(), dep["source"].strip(),
)
)
print(
"""
This file is generated automatically and - even though we try to prevent it - may
contain errors and mistakes. If you found any, send us an email
at [email protected] containing details about what is incorrect or missing."""
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create a NOTICE file")
parser.add_argument("--client", dest="client", action="append", default=[])
parser.add_argument("--server", dest="server", action="append", default=[])
args = parser.parse_args()
main(client=args.client, server=args.server)