Skip to content

Commit f6535bb

Browse files
rnjudgeNisha K
authored andcommitted
scancode: filter license from pip pkg classifiers
When scancode detects python package licenses it attaches the license classifiers to the declared_license dictionary, if applicable. This is a problem when Tern tries to report the package license by adding it to a set of licenses in get_package_licenses(), as dictionary objects cannot be added to sets. This commit filters out the declared license string from declared license dictionary containing the classifier values in order to fix this issue. Resolves #964 Signed-off-by: Rose Judge <[email protected]>
1 parent 7fb3d1b commit f6535bb

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

tern/extensions/scancode/executor.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,29 @@ def get_scancode_file(file_dict):
7575
return fd
7676

7777

78+
def filter_pkg_license(declared_license):
79+
'''When scancode detects python package licenses, it attaches classifiers
80+
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'''
83+
if isinstance(declared_license, dict):
84+
try:
85+
return declared_license['license']
86+
except KeyError:
87+
# parse classifiers for PyPI licenses
88+
# According to https://pypi.org/pypi?%3Aaction=list_classifiers
89+
# we can always take the value after the last '::'
90+
return declared_license['classifiers'][0].split('::')[-1].strip()
91+
92+
return declared_license
93+
94+
7895
def get_scancode_package(package_dict):
7996
'''Given a package dictionary from the scancode results, return a Package
8097
object with the results'''
8198
package = Package(package_dict['name'])
8299
package.version = package_dict['version']
83-
package.pkg_license = package_dict['declared_license']
100+
package.pkg_license = filter_pkg_license(package_dict['declared_license'])
84101
package.copyright = package_dict['copyright']
85102
package.proj_url = package_dict['repository_homepage_url']
86103
package.download_url = package_dict['download_url']

0 commit comments

Comments
 (0)