13
13
distributed under the License is distributed on an "AS IS" BASIS,
14
14
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
See the License for the specific language governing permissions and
16
- limitations
16
+ limitations
17
17
"""
18
18
19
- # Asumptions for this script:
19
+ # Asumptions for this script:
20
20
# 1. directory_name is scanned directory.
21
21
# Files are copied to this directory with full tree. As result, if we find
22
22
# license offender, we can have full path (just scrape directory_name). We do this
@@ -45,11 +45,11 @@ def license_check(directory_name, file):
45
45
46
46
This function does not verify if file exists, should be done prior the call.
47
47
48
- Args:
49
- directory_name - where scancode was run, used to scrape this from paths
48
+ Args:
49
+ directory_name - where scancode was run, used to scrape this from paths
50
50
file - scancode json output file (output from scancode --license --json-pp)
51
51
52
- Returns:
52
+ Returns:
53
53
0 if nothing found
54
54
>0 - count how many license isses found
55
55
-1 if any error in file licenses found
@@ -73,17 +73,10 @@ def license_check(directory_name, file):
73
73
continue
74
74
if not license_offender ['file' ]['licenses' ]:
75
75
license_offender ['reason' ] = MISSING_LICENSE_TEXT
76
- offenders .append (license_offender )
76
+ offenders .append (license_offender . copy () )
77
77
continue
78
78
79
- found_spdx = False
80
- for i in range (len (license_offender ['file' ]['licenses' ])):
81
- if license_offender ['file' ]['licenses' ][i ]['category' ] != 'Permissive' :
82
- license_offender ['reason' ] = MISSING_PERMISIVE_LICENSE_TEXT
83
- offenders .append (license_offender )
84
- # find SPDX, it shall be one of licenses found
85
- if license_offender ['file' ]['licenses' ][i ]['matched_rule' ]['identifier' ].find ("spdx" ) != - 1 :
86
- found_spdx = True
79
+ found_spdx = spdx_check (offenders , license_offender )
87
80
88
81
if not found_spdx :
89
82
try :
@@ -96,7 +89,7 @@ def license_check(directory_name, file):
96
89
if matches :
97
90
continue
98
91
license_offender ['reason' ] = MISSING_SPDX_TEXT
99
- offenders .append (license_offender )
92
+ offenders .append (license_offender . copy () )
100
93
except UnicodeDecodeError :
101
94
# not valid file for license check
102
95
continue
@@ -110,6 +103,29 @@ def license_check(directory_name, file):
110
103
userlog .warning ("File: " + offender ['file' ]['path' ][len (directory_name ):] + " " + "reason: " + offender ['reason' ])
111
104
return len (offenders )
112
105
106
+
107
+ def spdx_check (offenders , license_offender ):
108
+ """ Parse through list of licenses to determine whether licenses are permissive
109
+ @input list of offender, individual offender dict
110
+ @output none
111
+ """
112
+ found_spdx = False
113
+ # iterate through licenses, stop once permissive license has been found
114
+ for i in range (len (license_offender ['file' ]['licenses' ])):
115
+ # is any of the licenses permissive ?
116
+ if license_offender ['file' ]['licenses' ][i ]['category' ] == 'Permissive' :
117
+ # confirm that it has spdx license key
118
+ if license_offender ['file' ]['licenses' ][i ]['matched_rule' ]['identifier' ].find ("spdx" ) != - 1 :
119
+ found_spdx = True
120
+ # if no spdx found return anyway
121
+ return found_spdx
122
+ # otherwise file is missing permissive license
123
+ license_offender ['reason' ] = MISSING_PERMISIVE_LICENSE_TEXT
124
+ offenders .append (license_offender .copy ())
125
+
126
+ # missing spdx and permissive license
127
+ return found_spdx
128
+
113
129
def parse_args ():
114
130
parser = argparse .ArgumentParser (
115
131
description = "License check." )
@@ -119,8 +135,8 @@ def parse_args():
119
135
help = 'Directory name where are files being checked' )
120
136
return parser .parse_args ()
121
137
122
- if __name__ == "__main__" :
123
138
139
+ if __name__ == "__main__" :
124
140
args = parse_args ()
125
141
if args .file and os .path .isfile (args .file ):
126
142
count = license_check (args .directory_name , args .file )
0 commit comments