Skip to content

Commit ce5c763

Browse files
rnjudgeNisha K
authored andcommitted
Fix scancode KeyError during license parsing
When running Tern with Scancode, Tern tries to parse Scancode's `declared_license` dictionary `license` key to determine the package license. Sometimes this key is called `License` and sometimes the `declared_license` dictionary is empty, however. In each of the aforementioned cases Tern fails and throws a `KeyError`. This commit fixes the error by checking for an empty dictionary before trying to extract the license value and also checks for the `License` key in addition to the `license` key. Note that an issue[1] has been opened in the scancode repository asking for clarity on whether the declared_license key should be `License` or `license`. If any resolution comes from the scancode issue that has been opened, it may make sense to revert parts of this change. [1] aboutcode-org/scancode-toolkit#2803 Resolves #1063 Signed-off-by: Rose Judge <[email protected]>
1 parent 57c644c commit ce5c763

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

tern/extensions/scancode/executor.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,20 @@ def filter_pkg_license(declared_license):
8181
it will represent the license as a string or list of strings.
8282
Given a scancode declared_license field, extract and return a
8383
license string'''
84-
if isinstance(declared_license, dict):
84+
if isinstance(declared_license, dict) and len(declared_license) != 0:
8585
try:
8686
return declared_license['license']
8787
except KeyError:
88-
# parse classifiers for PyPI licenses
89-
# According to https://pypi.org/pypi?%3Aaction=list_classifiers
90-
# we can always take the value after the last '::'
91-
return declared_license['classifiers'][0].split('::')[-1].strip()
88+
try:
89+
return declared_license['License']
90+
except KeyError:
91+
try:
92+
# parse classifiers for PyPI licenses
93+
# According to pypi.org/pypi?%3Aaction=list_classifiers
94+
# we can always take the value after the last '::'
95+
return declared_license['classifiers'][0].split('::')[-1].strip()
96+
except KeyError:
97+
return None
9298
if isinstance(declared_license, list):
9399
for i, lic in enumerate(declared_license):
94100
# Some license lists from Scancode have dictionary entries

0 commit comments

Comments
 (0)