Skip to content

Commit 74ab164

Browse files
Refactor workaround for scancode evaluation of PBL
Add function "has_binary_license" to check if a file has a non-permissive license contains one. PBL is not recognized by scancode, causing it to be flagged as a non-permissive license. CI doesn't allow any non-permissive licenses, although, files flageed as SPDX are allowed. Workaround causes all files with a valid PBL to be flagged as missing an SPDX. Add condition in "has_spdx_text_in_scancode_output" to ignore any spdx identifier with "unknown" in the name. Scancode erroneously matches PBL to matched_rule.identifer "spdx-license-identifier: unknown-spdx". This prevents the workaround from working.
1 parent d085e9f commit 74ab164

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
userlog = logging.getLogger("scancode-evaluate")
3232

33+
3334
class ReturnCode(Enum):
3435
"""Return codes."""
3536

@@ -54,13 +55,13 @@ def path_leaf(path):
5455
return tail or os.path.basename(head)
5556

5657

57-
def has_permissive_text_in_scancode_output(scancode_output_data_file):
58-
"""Returns true if at least one license in the scancode output is permissive or is a Permissive Binary License"""
58+
def has_permissive_text_in_scancode_output(scancode_output_data_file_licenses):
59+
"""Returns true if at least one license in the scancode output is permissive"""
5960
# temporary workaround for files with Permissive Binary Licenses
6061
return any(
6162
scancode_output_data_file_license['category'] == 'Permissive'
62-
for scancode_output_data_file_license in scancode_output_data_file['licenses']
63-
) or has_binary_license(scancode_output_data_file)
63+
for scancode_output_data_file_license in scancode_output_data_file_licenses
64+
)
6465

6566

6667
def has_spdx_text_in_scancode_output(scancode_output_data_file_licenses):
@@ -76,16 +77,20 @@ def has_spdx_text_in_analysed_file(scanned_file_content):
7677
return bool(re.findall("SPDX-License-Identifier:?", scanned_file_content))
7778

7879

79-
def has_binary_license(scancode_output_data_file):
80+
def has_binary_license(scanned_file_content):
8081
"""Returns true if the file analysed by ScanCode contains a Permissive Binary License."""
82+
return bool(re.findall("Permissive Binary License", scanned_file_content))
83+
84+
85+
def get_file_text(scancode_output_data_file):
86+
"""Returns file text for scancode output file"""
8187
file_path = os.path.abspath(scancode_output_data_file['path'])
8288
try:
8389
with open(file_path, 'r') as read_file:
84-
scanned_file_content = read_file.read()
85-
return bool(re.findall("Permissive Binary License", scanned_file_content))
90+
return read_file.read()
8691
except UnicodeDecodeError:
87-
userlog.warning("Unable to look for PBL text in `{}`:".format(file_path))
88-
return False
92+
userlog.warning("Unable to decode file text in: %s" % file_path)
93+
# Ignore files that cannot be decoded
8994

9095

9196
def license_check(scancode_output_path):
@@ -98,7 +103,7 @@ def license_check(scancode_output_path):
98103
99104
Returns:
100105
0 if nothing found
101-
>0 - count how many license isses found
106+
>0 - count how many license issues found
102107
ReturnCode.ERROR.value if any error in file licenses found
103108
"""
104109

@@ -125,25 +130,22 @@ def license_check(scancode_output_path):
125130
# check the next file in the scancode output
126131
continue
127132

128-
if not has_permissive_text_in_scancode_output(scancode_output_data_file):
129-
scancode_output_data_file['fail_reason'] = MISSING_PERMISSIVE_LICENSE_TEXT
130-
license_offenders.append(scancode_output_data_file)
133+
if not (has_permissive_text_in_scancode_output(scancode_output_data_file['licenses'])):
134+
scanned_file_content = get_file_text(scancode_output_data_file)
135+
136+
if not (scanned_file_content is not None and has_binary_license(scanned_file_content)):
137+
scancode_output_data_file['fail_reason'] = MISSING_PERMISSIVE_LICENSE_TEXT
138+
license_offenders.append(scancode_output_data_file)
131139

132140
if not has_spdx_text_in_scancode_output(scancode_output_data_file['licenses']):
133141
# Scancode does not recognize license notice in Python file headers.
134142
# Issue: https://github.com/nexB/scancode-toolkit/issues/1913
135143
# Therefore check if the file tested by ScanCode actually has a licence notice.
136-
file_path = os.path.abspath(scancode_output_data_file['path'])
137-
try:
138-
with open(file_path, 'r') as read_file:
139-
scanned_file_content = read_file.read()
140-
except UnicodeDecodeError:
141-
userlog.warning("Unable to look for SPDX text in `{}`:".format(file_path))
142-
# Ignore files that cannot be decoded
143-
# check the next file in the scancode output
144-
continue
144+
scanned_file_content = get_file_text(scancode_output_data_file)
145145

146-
if not has_spdx_text_in_analysed_file(scanned_file_content):
146+
if scanned_file_content is None:
147+
continue
148+
elif not has_spdx_text_in_analysed_file(scanned_file_content):
147149
scancode_output_data_file['fail_reason'] = MISSING_SPDX_TEXT
148150
spdx_offenders.append(scancode_output_data_file)
149151

0 commit comments

Comments
 (0)