Skip to content

Commit e93a3e2

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 e93a3e2

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

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

Lines changed: 24 additions & 24 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,12 @@ 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"""
59-
# temporary workaround for files with Permissive Binary Licenses
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"""
6060
return any(
6161
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)
62+
for scancode_output_data_file_license in scancode_output_data_file_licenses
63+
)
6464

6565

6666
def has_spdx_text_in_scancode_output(scancode_output_data_file_licenses):
@@ -76,16 +76,20 @@ def has_spdx_text_in_analysed_file(scanned_file_content):
7676
return bool(re.findall("SPDX-License-Identifier:?", scanned_file_content))
7777

7878

79-
def has_binary_license(scancode_output_data_file):
79+
def has_binary_license(scanned_file_content):
8080
"""Returns true if the file analysed by ScanCode contains a Permissive Binary License."""
81+
return bool(re.findall("Permissive Binary License", scanned_file_content))
82+
83+
84+
def get_file_text(scancode_output_data_file):
85+
"""Returns file text for scancode output file"""
8186
file_path = os.path.abspath(scancode_output_data_file['path'])
8287
try:
8388
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))
89+
return read_file.read()
8690
except UnicodeDecodeError:
87-
userlog.warning("Unable to look for PBL text in `{}`:".format(file_path))
88-
return False
91+
userlog.warning("Unable to decode file text in: %s" % file_path)
92+
# Ignore files that cannot be decoded
8993

9094

9195
def license_check(scancode_output_path):
@@ -98,7 +102,7 @@ def license_check(scancode_output_path):
98102
99103
Returns:
100104
0 if nothing found
101-
>0 - count how many license isses found
105+
>0 - count how many license issues found
102106
ReturnCode.ERROR.value if any error in file licenses found
103107
"""
104108

@@ -125,25 +129,21 @@ def license_check(scancode_output_path):
125129
# check the next file in the scancode output
126130
continue
127131

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)
132+
if not has_permissive_text_in_scancode_output(scancode_output_data_file['licenses']):
133+
scanned_file_content = get_file_text(scancode_output_data_file)
134+
if not (scanned_file_content and has_binary_license(scanned_file_content)):
135+
scancode_output_data_file['fail_reason'] = MISSING_PERMISSIVE_LICENSE_TEXT
136+
license_offenders.append(scancode_output_data_file)
131137

132138
if not has_spdx_text_in_scancode_output(scancode_output_data_file['licenses']):
133139
# Scancode does not recognize license notice in Python file headers.
134140
# Issue: https://github.com/nexB/scancode-toolkit/issues/1913
135141
# 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
142+
scanned_file_content = get_file_text(scancode_output_data_file)
145143

146-
if not has_spdx_text_in_analysed_file(scanned_file_content):
144+
if not scanned_file_content:
145+
continue
146+
elif not has_spdx_text_in_analysed_file(scanned_file_content):
147147
scancode_output_data_file['fail_reason'] = MISSING_SPDX_TEXT
148148
spdx_offenders.append(scancode_output_data_file)
149149

tools/test/travis-ci/scancode_evaluate_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* limitations under the License.\
3535
*/"
3636

37-
BINARY_HEADER = "/*\
37+
HEADER_WITH_BINARY_LICENSE = "/*\
3838
* Copyright (c) 2019, Arm Limited, All Rights Reserved\
3939
* SPDX-License-Identifier: LicenseRef-PBL\
4040
*\
@@ -63,7 +63,7 @@ def create_scanned_files():
6363
if file_path in [os.path.join(STUBS_PATH, "test3.h")]:
6464
new_file.write(HEADER_WITHOUT_SPDX)
6565
elif file_path in [os.path.join(STUBS_PATH, "test6.h")]:
66-
new_file.write(BINARY_HEADER)
66+
new_file.write(HEADER_WITH_BINARY_LICENSE)
6767
else:
6868
new_file.write(HEADER_WITH_SPDX)
6969
yield
@@ -95,7 +95,7 @@ def test_missing_license_permissive_license_and_spdx(self, create_scanned_files)
9595
test3.h: Missing `Permissive` license text and `spdx` in match.identifier and not in file tested by ScanCode (error count += 1)
9696
test4.h: Missing `Permissive` license text and `spdx` in match.identifier but found in file tested by ScanCode (error count += 1)
9797
test5.h: Missing `spdx` in match.identifier but found in file tested by ScanCode. (error count += 0)
98-
test6.h: Matching 'unknown-spdx' in match.identifier and Permissive Binary License in header (error count += 1)
98+
test6.h: Matching `spdx` in match.identifier but Permissive Binary License header (error count += 0)
9999
@inputs scancode_test/scancode_test_2.json
100100
@output 3
101101
"""
@@ -107,4 +107,4 @@ def test_permissive_license_no_spdx(self, create_scanned_files):
107107
@inputs scancode_test/scancode_test_2.json
108108
@outputs 0
109109
"""
110-
assert license_check(os.path.join(STUBS_PATH, "scancode_test_4.json")) == 0
110+
assert license_check(os.path.join(STUBS_PATH, "scancode_test_4.json")) == 0

0 commit comments

Comments
 (0)