Skip to content

Commit afb3706

Browse files
authored
Merge pull request #13847 from harmut01/pbl_scancode_fix
Add workaround for files with permissive binary licenses
2 parents f9737a2 + e93a3e2 commit afb3706

File tree

3 files changed

+88
-16
lines changed

3 files changed

+88
-16
lines changed

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

Lines changed: 27 additions & 14 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

@@ -55,7 +56,7 @@ def path_leaf(path):
5556

5657

5758
def has_permissive_text_in_scancode_output(scancode_output_data_file_licenses):
58-
"""Returns true if at list one license in the scancode output is permissive."""
59+
"""Returns true if at least one license in the scancode output is permissive"""
5960
return any(
6061
scancode_output_data_file_license['category'] == 'Permissive'
6162
for scancode_output_data_file_license in scancode_output_data_file_licenses
@@ -75,6 +76,22 @@ def has_spdx_text_in_analysed_file(scanned_file_content):
7576
return bool(re.findall("SPDX-License-Identifier:?", scanned_file_content))
7677

7778

79+
def has_binary_license(scanned_file_content):
80+
"""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"""
86+
file_path = os.path.abspath(scancode_output_data_file['path'])
87+
try:
88+
with open(file_path, 'r') as read_file:
89+
return read_file.read()
90+
except UnicodeDecodeError:
91+
userlog.warning("Unable to decode file text in: %s" % file_path)
92+
# Ignore files that cannot be decoded
93+
94+
7895
def license_check(scancode_output_path):
7996
"""Check licenses in the scancode json file for specified directory.
8097
@@ -85,7 +102,7 @@ def license_check(scancode_output_path):
85102
86103
Returns:
87104
0 if nothing found
88-
>0 - count how many license isses found
105+
>0 - count how many license issues found
89106
ReturnCode.ERROR.value if any error in file licenses found
90107
"""
91108

@@ -113,24 +130,20 @@ def license_check(scancode_output_path):
113130
continue
114131

115132
if not has_permissive_text_in_scancode_output(scancode_output_data_file['licenses']):
116-
scancode_output_data_file['fail_reason'] = MISSING_PERMISSIVE_LICENSE_TEXT
117-
license_offenders.append(scancode_output_data_file)
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)
118137

119138
if not has_spdx_text_in_scancode_output(scancode_output_data_file['licenses']):
120139
# Scancode does not recognize license notice in Python file headers.
121140
# Issue: https://github.com/nexB/scancode-toolkit/issues/1913
122141
# Therefore check if the file tested by ScanCode actually has a licence notice.
123-
file_path = os.path.abspath(scancode_output_data_file['path'])
124-
try:
125-
with open(file_path, 'r') as read_file:
126-
scanned_file_content = read_file.read()
127-
except UnicodeDecodeError:
128-
userlog.warning("Unable to look for SPDX text in `{}`:".format(file_path))
129-
# Ignore files that cannot be decoded
130-
# check the next file in the scancode output
131-
continue
142+
scanned_file_content = get_file_text(scancode_output_data_file)
132143

133-
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):
134147
scancode_output_data_file['fail_reason'] = MISSING_SPDX_TEXT
135148
spdx_offenders.append(scancode_output_data_file)
136149

tools/test/travis-ci/scancode_evaluate_test.py

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

37+
HEADER_WITH_BINARY_LICENSE = "/*\
38+
* Copyright (c) 2019, Arm Limited, All Rights Reserved\
39+
* SPDX-License-Identifier: LicenseRef-PBL\
40+
*\
41+
* This file and the related binary are licensed under the\
42+
* Permissive Binary License, Version 1.0 (the \"License\");\
43+
* you may not use these files except in compliance with the License.\
44+
*\
45+
*/"
46+
3747
@pytest.fixture()
3848
def create_scanned_files():
3949
"""Create stub files.
4050
test3.h missing license notice
4151
test4.h with license notice
4252
test5.h with license notice
53+
test6.h with permissive binary license
4354
"""
4455
file_paths = [
4556
os.path.join(STUBS_PATH, "test3.h"),
4657
os.path.join(STUBS_PATH, "test4.h"),
47-
os.path.join(STUBS_PATH, "test5.h")
58+
os.path.join(STUBS_PATH, "test5.h"),
59+
os.path.join(STUBS_PATH, "test6.h")
4860
]
4961
for file_path in file_paths:
5062
with open(file_path, "w") as new_file:
5163
if file_path in [os.path.join(STUBS_PATH, "test3.h")]:
5264
new_file.write(HEADER_WITHOUT_SPDX)
65+
elif file_path in [os.path.join(STUBS_PATH, "test6.h")]:
66+
new_file.write(HEADER_WITH_BINARY_LICENSE)
5367
else:
5468
new_file.write(HEADER_WITH_SPDX)
5569
yield
@@ -81,6 +95,7 @@ def test_missing_license_permissive_license_and_spdx(self, create_scanned_files)
8195
test3.h: Missing `Permissive` license text and `spdx` in match.identifier and not in file tested by ScanCode (error count += 1)
8296
test4.h: Missing `Permissive` license text and `spdx` in match.identifier but found in file tested by ScanCode (error count += 1)
8397
test5.h: Missing `spdx` in match.identifier but found in file tested by ScanCode. (error count += 0)
98+
test6.h: Matching `spdx` in match.identifier but Permissive Binary License header (error count += 0)
8499
@inputs scancode_test/scancode_test_2.json
85100
@output 3
86101
"""
@@ -92,4 +107,4 @@ def test_permissive_license_no_spdx(self, create_scanned_files):
92107
@inputs scancode_test/scancode_test_2.json
93108
@outputs 0
94109
"""
95-
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

tools/test/travis-ci/scancode_test/scancode_test_3.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,50 @@
170170
],
171171
"scan_errors":[
172172

173+
]
174+
},
175+
{
176+
"path":"tools/test/travis-ci/scancode_test/test6.h",
177+
"type":"file",
178+
"licenses":[
179+
{
180+
"key": "unknown-spdx",
181+
"score": 100.0,
182+
"name": "Unknown SPDX license detected but not recognized",
183+
"short_name": "unknown SPDX",
184+
"category": "Unstated License",
185+
"is_exception": false,
186+
"owner": "Unspecified",
187+
"homepage_url": null,
188+
"text_url": "",
189+
"reference_url": "https://enterprise.dejacode.com/urn/urn:dje:license:unknown-spdx",
190+
"spdx_license_key": null,
191+
"spdx_url": "",
192+
"start_line": 3,
193+
"end_line": 3,
194+
"matched_rule": {
195+
"identifier": "spdx-license-identifier: unknown-spdx",
196+
"license_expression": "unknown-spdx",
197+
"licenses": [
198+
"unknown-spdx"
199+
],
200+
"is_license_text": false,
201+
"is_license_notice": false,
202+
"is_license_reference": false,
203+
"is_license_tag": true,
204+
"matcher": "1-spdx-id",
205+
"rule_length": 1,
206+
"matched_length": 1,
207+
"match_coverage": 100.0,
208+
"rule_relevance": 100
209+
}
210+
}
211+
],
212+
"license_expressions":[
213+
"unknown-spdx"
214+
],
215+
"scan_errors":[
216+
173217
]
174218
}
175219
]

0 commit comments

Comments
 (0)