30
30
31
31
userlog = logging .getLogger ("scancode-evaluate" )
32
32
33
+
33
34
class ReturnCode (Enum ):
34
35
"""Return codes."""
35
36
@@ -54,13 +55,12 @@ def path_leaf(path):
54
55
return tail or os .path .basename (head )
55
56
56
57
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"""
60
60
return any (
61
61
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
+ )
64
64
65
65
66
66
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):
76
76
return bool (re .findall ("SPDX-License-Identifier:?" , scanned_file_content ))
77
77
78
78
79
- def has_binary_license (scancode_output_data_file ):
79
+ def has_binary_license (scanned_file_content ):
80
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"""
81
86
file_path = os .path .abspath (scancode_output_data_file ['path' ])
82
87
try :
83
88
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 ()
86
90
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
89
93
90
94
91
95
def license_check (scancode_output_path ):
@@ -98,7 +102,7 @@ def license_check(scancode_output_path):
98
102
99
103
Returns:
100
104
0 if nothing found
101
- >0 - count how many license isses found
105
+ >0 - count how many license issues found
102
106
ReturnCode.ERROR.value if any error in file licenses found
103
107
"""
104
108
@@ -125,25 +129,21 @@ def license_check(scancode_output_path):
125
129
# check the next file in the scancode output
126
130
continue
127
131
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 )
131
137
132
138
if not has_spdx_text_in_scancode_output (scancode_output_data_file ['licenses' ]):
133
139
# Scancode does not recognize license notice in Python file headers.
134
140
# Issue: https://github.com/nexB/scancode-toolkit/issues/1913
135
141
# 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 )
145
143
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 ):
147
147
scancode_output_data_file ['fail_reason' ] = MISSING_SPDX_TEXT
148
148
spdx_offenders .append (scancode_output_data_file )
149
149
0 commit comments