Fix false positive error in script checking license notices

Remove code block from license_check that evalutes whether a files
licenses are permissive and that there is a SPDX notice. Scancode
sometimes incorrectly attributes a single license notice to permissive
and non-permissive licenses. Removed code block results in many false
positives because it labels any file that has a "non-permissive" license
as such even when there is a permissive license.

Add function spdx_check to scancode-evaluate.py to improve analysis of
copyright and license notice tests.
pull/13745/head
Harrison Mutai 2020-10-06 18:17:49 +01:00 committed by Hugues Kamba
parent e6c31514c6
commit 6fa88f4247
1 changed files with 32 additions and 16 deletions

View File

@ -73,17 +73,10 @@ def license_check(directory_name, file):
continue continue
if not license_offender['file']['licenses']: if not license_offender['file']['licenses']:
license_offender['reason'] = MISSING_LICENSE_TEXT license_offender['reason'] = MISSING_LICENSE_TEXT
offenders.append(license_offender) offenders.append(license_offender.copy())
continue continue
found_spdx = False found_spdx = spdx_check(offenders, license_offender)
for i in range(len(license_offender['file']['licenses'])):
if license_offender['file']['licenses'][i]['category'] != 'Permissive':
license_offender['reason'] = MISSING_PERMISIVE_LICENSE_TEXT
offenders.append(license_offender)
# find SPDX, it shall be one of licenses found
if license_offender['file']['licenses'][i]['matched_rule']['identifier'].find("spdx") != -1:
found_spdx = True
if not found_spdx: if not found_spdx:
try: try:
@ -96,7 +89,7 @@ def license_check(directory_name, file):
if matches: if matches:
continue continue
license_offender['reason'] = MISSING_SPDX_TEXT license_offender['reason'] = MISSING_SPDX_TEXT
offenders.append(license_offender) offenders.append(license_offender.copy())
except UnicodeDecodeError: except UnicodeDecodeError:
# not valid file for license check # not valid file for license check
continue continue
@ -110,6 +103,29 @@ def license_check(directory_name, file):
userlog.warning("File: " + offender['file']['path'][len(directory_name):] + " " + "reason: " + offender['reason']) userlog.warning("File: " + offender['file']['path'][len(directory_name):] + " " + "reason: " + offender['reason'])
return len(offenders) return len(offenders)
def spdx_check(offenders, license_offender):
""" Parse through list of licenses to determine whether licenses are permissive
@input list of offender, individual offender dict
@output none
"""
found_spdx = False
# iterate through licenses, stop once permissive license has been found
for i in range(len(license_offender['file']['licenses'])):
# is any of the licenses permissive ?
if license_offender['file']['licenses'][i]['category'] == 'Permissive':
# confirm that it has spdx license key
if license_offender['file']['licenses'][i]['matched_rule']['identifier'].find("spdx") != -1:
found_spdx = True
# if no spdx found return anyway
return found_spdx
# otherwise file is missing permissive license
license_offender['reason'] = MISSING_PERMISIVE_LICENSE_TEXT
offenders.append(license_offender.copy())
# missing spdx and permissive license
return found_spdx
def parse_args(): def parse_args():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="License check.") description="License check.")
@ -119,8 +135,8 @@ def parse_args():
help='Directory name where are files being checked') help='Directory name where are files being checked')
return parser.parse_args() return parser.parse_args()
if __name__ == "__main__":
if __name__ == "__main__":
args = parse_args() args = parse_args()
if args.file and os.path.isfile(args.file): if args.file and os.path.isfile(args.file):
count = license_check(args.directory_name, args.file) count = license_check(args.directory_name, args.file)