Skip to content

Commit d49d6ed

Browse files
Add workaround for files with permissive binary licenses
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 f57f265 commit d49d6ed

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ def has_permissive_text_in_scancode_output(scancode_output_data_file_licenses):
6565
def has_spdx_text_in_scancode_output(scancode_output_data_file_licenses):
6666
"""Returns true if at least one license in the scancode output has the spdx identifier."""
6767
return any(
68-
'spdx' in scancode_output_data_file_license['matched_rule']['identifier']
68+
('spdx' in scancode_output_data_file_license['matched_rule']['identifier']) and not ('unknown' in
69+
scancode_output_data_file_license[
70+
'matched_rule'][
71+
'identifier'])
6972
for scancode_output_data_file_license in scancode_output_data_file_licenses
7073
)
7174

@@ -75,6 +78,11 @@ def has_spdx_text_in_analysed_file(scanned_file_content):
7578
return bool(re.findall("SPDX-License-Identifier:?", scanned_file_content))
7679

7780

81+
def has_binary_license(scanned_file_content):
82+
"""Returns true if the file analysed by ScanCode contains a Permissive Binary License."""
83+
return bool(re.findall("Permissive Binary License", scanned_file_content))
84+
85+
7886
def license_check(scancode_output_path):
7987
"""Check licenses in the scancode json file for specified directory.
8088
@@ -129,8 +137,9 @@ def license_check(scancode_output_path):
129137
# Ignore files that cannot be decoded
130138
# check the next file in the scancode output
131139
continue
132-
133-
if not has_spdx_text_in_analysed_file(scanned_file_content):
140+
# temporary workaround for files with Permissive Binary Licenses
141+
# TODO raise issue in scancode repo to get PBL added to licenses
142+
if not has_spdx_text_in_analysed_file(scanned_file_content) or has_binary_license(scanned_file_content):
134143
scancode_output_data_file['fail_reason'] = MISSING_SPDX_TEXT
135144
spdx_offenders.append(scancode_output_data_file)
136145

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+
BINARY_HEADER = "/*\
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(BINARY_HEADER)
5367
else:
5468
new_file.write(HEADER_WITH_SPDX)
5569
yield
@@ -81,10 +95,11 @@ 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 'unknown-spdx' in match.identifier and Permissive Binary License in header (error count += 1)
8499
@inputs scancode_test/scancode_test_2.json
85100
@output 3
86101
"""
87-
assert license_check(os.path.join(STUBS_PATH, "scancode_test_3.json")) == 3
102+
assert license_check(os.path.join(STUBS_PATH, "scancode_test_3.json")) == 4
88103

89104
def test_permissive_license_no_spdx(self, create_scanned_files):
90105
""" Multiple `Permissive` licenses in one file but none with `spdx` in

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)