Skip to content

Commit 20573c4

Browse files
committed
Fix Scancode collection of package licenses
There was a traceback error running Tern with Scancode for multiple report formats due to Scancode reporting licenses as a list containing a single string or a list containing a license dictionary when Tern was expecting just a string. This commit resolves the issue by parsing the license list to get the license text before assigning it to a Package object's 'pkg_license' value. Resolves #985 #844 Signed-off-by: Rose Judge <[email protected]>
1 parent b8e7837 commit 20573c4

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tern/extensions/scancode/executor.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ def get_scancode_file(file_dict):
7878
def filter_pkg_license(declared_license):
7979
'''When scancode detects python package licenses, it attaches classifiers
8080
to the declared_license field as a dictionary object, otherwise
81-
it will represent the license as a string. Given a scancode
82-
declared_license field, extract and return a license string'''
81+
it will represent the license as a string or list of strings.
82+
Given a scancode declared_license field, extract and return a
83+
license string'''
8384
if isinstance(declared_license, dict):
8485
try:
8586
return declared_license['license']
@@ -88,7 +89,15 @@ def filter_pkg_license(declared_license):
8889
# According to https://pypi.org/pypi?%3Aaction=list_classifiers
8990
# we can always take the value after the last '::'
9091
return declared_license['classifiers'][0].split('::')[-1].strip()
91-
92+
if isinstance(declared_license, list):
93+
for i, lic in enumerate(declared_license):
94+
# Some license lists from Scancode have dictionary entries
95+
# Extract license 'types' from license dictionaries
96+
if isinstance(lic, dict):
97+
declared_license[i] = lic["type"]
98+
# Get rid of duplicate licenses
99+
dec_lic = set(declared_license)
100+
return ', '.join(list(dec_lic))
92101
return declared_license
93102

94103

0 commit comments

Comments
 (0)