-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen_licenses.py
91 lines (71 loc) · 2.19 KB
/
gen_licenses.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
#!/usr/bin/python3
#
# setup:
# 1. Install tools
# - npm install -g license-checker
# 2. Run script in webapp root dir
# - ./tools/generate_licenses >{basename outputfile}
#
#
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
#
import sys
import json
import subprocess
import pprint
import re
import urllib
import urllib.request
import urllib.error
ignore_modules = {
"^kiwi"
}
def print_import_path():
print("\n".join(sys.path), file=sys.stderr)
def pretty_print(json):
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(json)
def strip_line(line):
l = re.sub("^//\s*", "", line)
return l.rstrip()
def set_proxy():
proxy = urllib.request.ProxyHandler({ 'http': '10.158.0.79:80' })
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
def run_get_stdout(cmd):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
return proc.stdout.read().decode('utf-8')
def fetch_url(mod,url):
try:
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
print("Module: {} HTTPError: {}".format(mod,e.code), file=sys.stderr)
except urllib.error.URLError as e:
print("Module: {} URLError: {}".format(mod,e.reason), file=sys.stderr)
except ValueError as e:
print("Module:{} ValueError: {}".format(mod,e), file=sys.stderr)
else:
# We assume plain text files...
for line in response.read().decode('utf-8').split("\n"):
print(strip_line(line))
def gen_npm():
data = json.loads(run_get_stdout(['license-checker', '--json', '--production']))
for mod, mod_data in sorted(data.items(), key=lambda m: m[0]):
skip = False
for im in ignore_modules:
if re.search(im,mod):
skip = True
if not skip:
print("<h3>" + mod + "</h3>")
print("<p><pre>")
# We assume plain text files...
with open(mod_data["licenseFile"],encoding='utf-8') as f:
for line in f:
print(strip_line(line))
print("</pre></p>")
def main():
#set_proxy()
gen_npm()
if __name__ == "__main__":
main()