-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-licenses.py
51 lines (47 loc) · 1.82 KB
/
check-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
#!/usr/bin/python
import glob
import json
import urllib.request
import sys
from bs4 import BeautifulSoup
from tomark import Tomark
if len(sys.argv) < 2:
print('ERROR: Please provide the root directory containing the package.json files as an command line argument')
exit(1)
if len(sys.argv) > 2:
print('WARING: All command line arguments but the first one will be ignored')
rootDirectory = sys.argv[1]
files = glob.glob(rootDirectory + '/**/package.json', recursive=True)
packageNames = set()
for file in files:
if 'node_modules' in file:
continue
with open(file, 'r') as packageJsonFile:
content = json.load(packageJsonFile)
dependencyKeys = [ 'dependencies', 'devDependencies' ]
for dependencyKey in dependencyKeys:
if not dependencyKey in content.keys():
continue
for packageName in content[dependencyKey].keys():
packageNames.add(packageName)
licenses = []
missingLicenses = []
for packageName in packageNames:
print('Getting license for {}... ({}/{})'.format(packageName, len(licenses) + 1, len(packageNames)))
baseUrl = 'https://www.npmjs.com/package/'
packageUrl = baseUrl + packageName
try:
page = urllib.request.urlopen(packageUrl)
content = page.read()
soup = BeautifulSoup(content,'html.parser')
possibleLicenseDivs = soup.find_all('p', {'class': 'f2874b88'})
license = possibleLicenseDivs[1].text
licenses.append({'Package': packageName, 'License': license})
except Exception as error:
print('ERROR: Cannot get license for {}'.format(packageName))
print(error)
missingLicenses.append(missingLicenses)
markdown = Tomark.table(licenses)
print(markdown)
if len(missingLicenses) > 0:
print('Missing licenses: {}'.format(', '.join(missingLicenses)))