-
-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathosikeys.py
More file actions
52 lines (44 loc) · 1.64 KB
/
Copy pathosikeys.py
File metadata and controls
52 lines (44 loc) · 1.64 KB
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
import json
import licensedcode.index
import urllib.request
from licensedcode.cache import get_index
from licensedcode.models import load_licenses
def find_osi_map(license: "OSI License Object"):
"""
Return Scancode key mapped to OSI License `license`
"""
idx = get_index()
osi_key = license['id']
# search for Scancode License matching OSI Key
matches = list(idx.match(query_string = osi_key))
if not matches:
return None
return matches[0].rule.license_expression
def add_osi_key(obj: "OSI License Object"):
"""
Add OSI License Key derived from OSI License Object `obj`
"""
licenses = licensedcode.cache.get_licenses_db()
# find matching Scancode License Object for `obj`
result = find_osi_map(obj)
# derive OSI License Key from `obj`
osi_key = obj['id']
if not result: # no matching Scancode License found
return None
mapped_license = licenses[result] # license object from Scancode
licenses_path = "src/licensedcode/data/licenses/" # licenses directory
file_to_modify = open(licenses_path + mapped_license.key + ".yml", "a")
file_to_modify.write("osi_license_key: " + osi_key)
file_to_modify.write("\n")
def add_osi_keys():
"""
For every possible OSI License, match and add OSI Key to existing Scancode License
"""
#retrieve OSI License Objects
contents = urllib.request.urlopen("https://api.opensource.org/licenses/").read().decode()
data = json.loads(contents)
# for each OSI License Object, update matching Scancode License Object
for obj in data:
add_osi_key(obj)
if __name__ == '__main__':
add_osi_keys()