Skip to content

Commit 3c6c8ae

Browse files
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.
1 parent 648a0ce commit 3c6c8ae

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

tools/test/travis-ci/scancode-evaluate.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
distributed under the License is distributed on an "AS IS" BASIS,
1414
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
See the License for the specific language governing permissions and
16-
limitations
16+
limitations
1717
"""
1818

19-
# Asumptions for this script:
19+
# Asumptions for this script:
2020
# 1. directory_name is scanned directory.
2121
# Files are copied to this directory with full tree. As result, if we find
2222
# license offender, we can have full path (just scrape directory_name). We do this
@@ -45,11 +45,11 @@ def license_check(directory_name, file):
4545
4646
This function does not verify if file exists, should be done prior the call.
4747
48-
Args:
49-
directory_name - where scancode was run, used to scrape this from paths
48+
Args:
49+
directory_name - where scancode was run, used to scrape this from paths
5050
file - scancode json output file (output from scancode --license --json-pp)
5151
52-
Returns:
52+
Returns:
5353
0 if nothing found
5454
>0 - count how many license isses found
5555
-1 if any error in file licenses found
@@ -73,17 +73,10 @@ def license_check(directory_name, file):
7373
continue
7474
if not license_offender['file']['licenses']:
7575
license_offender['reason'] = MISSING_LICENSE_TEXT
76-
offenders.append(license_offender)
76+
offenders.append(license_offender.copy())
7777
continue
7878

79-
found_spdx = False
80-
for i in range(len(license_offender['file']['licenses'])):
81-
if license_offender['file']['licenses'][i]['category'] != 'Permissive':
82-
license_offender['reason'] = MISSING_PERMISIVE_LICENSE_TEXT
83-
offenders.append(license_offender)
84-
# find SPDX, it shall be one of licenses found
85-
if license_offender['file']['licenses'][i]['matched_rule']['identifier'].find("spdx") != -1:
86-
found_spdx = True
79+
found_spdx = spdx_check(offenders, license_offender)
8780

8881
if not found_spdx:
8982
try:
@@ -96,7 +89,7 @@ def license_check(directory_name, file):
9689
if matches:
9790
continue
9891
license_offender['reason'] = MISSING_SPDX_TEXT
99-
offenders.append(license_offender)
92+
offenders.append(license_offender.copy())
10093
except UnicodeDecodeError:
10194
# not valid file for license check
10295
continue
@@ -110,6 +103,29 @@ def license_check(directory_name, file):
110103
userlog.warning("File: " + offender['file']['path'][len(directory_name):] + " " + "reason: " + offender['reason'])
111104
return len(offenders)
112105

106+
107+
def spdx_check(offenders, license_offender):
108+
""" Parse through list of licenses to determine whether licenses are permissive
109+
@input list of offender, individual offender dict
110+
@output none
111+
"""
112+
found_spdx = False
113+
# iterate through licenses, stop once permissive license has been found
114+
for i in range(len(license_offender['file']['licenses'])):
115+
# is any of the licenses permissive ?
116+
if license_offender['file']['licenses'][i]['category'] == 'Permissive':
117+
# confirm that it has spdx license key
118+
if license_offender['file']['licenses'][i]['matched_rule']['identifier'].find("spdx") != -1:
119+
found_spdx = True
120+
# if no spdx found return anyway
121+
return found_spdx
122+
# otherwise file is missing permissive license
123+
license_offender['reason'] = MISSING_PERMISIVE_LICENSE_TEXT
124+
offenders.append(license_offender.copy())
125+
126+
# missing spdx and permissive license
127+
return found_spdx
128+
113129
def parse_args():
114130
parser = argparse.ArgumentParser(
115131
description="License check.")
@@ -119,8 +135,8 @@ def parse_args():
119135
help='Directory name where are files being checked')
120136
return parser.parse_args()
121137

122-
if __name__ == "__main__":
123138

139+
if __name__ == "__main__":
124140
args = parse_args()
125141
if args.file and os.path.isfile(args.file):
126142
count = license_check(args.directory_name, args.file)

0 commit comments

Comments
 (0)