blob: 18cb9f5b3d3d6900a82a65d42b74f81ba0e08dd8 [file] [log] [blame]
Bert Vermeulend0259c42021-01-21 09:54:12 +01001#!/usr/bin/env python3
Thomas Gleixner5385a292018-04-26 15:54:27 +02002# SPDX-License-Identifier: GPL-2.0
3# Copyright Thomas Gleixner <tglx@linutronix.de>
4
5from argparse import ArgumentParser
6from ply import lex, yacc
Jeremy Clinebed95c42018-08-17 15:44:01 -07007import locale
Thomas Gleixner5385a292018-04-26 15:54:27 +02008import traceback
Thomas Gleixner0509b272022-05-16 12:27:29 +02009import fnmatch
Thomas Gleixner5385a292018-04-26 15:54:27 +020010import sys
11import git
12import re
13import os
14
15class ParserException(Exception):
16 def __init__(self, tok, txt):
17 self.tok = tok
18 self.txt = txt
19
20class SPDXException(Exception):
21 def __init__(self, el, txt):
22 self.el = el
23 self.txt = txt
24
25class SPDXdata(object):
26 def __init__(self):
27 self.license_files = 0
28 self.exception_files = 0
29 self.licenses = [ ]
30 self.exceptions = { }
31
Thomas Gleixnera377ce72022-05-16 12:27:24 +020032class dirinfo(object):
33 def __init__(self):
34 self.missing = 0
35 self.total = 0
Thomas Gleixner67924b72022-05-16 12:27:27 +020036 self.files = []
Thomas Gleixnera377ce72022-05-16 12:27:24 +020037
Thomas Gleixner67924b72022-05-16 12:27:27 +020038 def update(self, fname, basedir, miss):
Thomas Gleixnera377ce72022-05-16 12:27:24 +020039 self.total += 1
40 self.missing += miss
Thomas Gleixner67924b72022-05-16 12:27:27 +020041 if miss:
42 fname = './' + fname
43 bdir = os.path.dirname(fname)
44 if bdir == basedir.rstrip('/'):
45 self.files.append(fname)
Thomas Gleixnera377ce72022-05-16 12:27:24 +020046
Thomas Gleixner5385a292018-04-26 15:54:27 +020047# Read the spdx data from the LICENSES directory
48def read_spdxdata(repo):
49
50 # The subdirectories of LICENSES in the kernel source
Vincenzo Frascino8d7a7ab2019-05-31 22:30:45 -070051 # Note: exceptions needs to be parsed as last directory.
52 license_dirs = [ "preferred", "dual", "deprecated", "exceptions" ]
Joe Perchesfde5e902018-08-17 15:43:57 -070053 lictree = repo.head.commit.tree['LICENSES']
Thomas Gleixner5385a292018-04-26 15:54:27 +020054
55 spdx = SPDXdata()
56
57 for d in license_dirs:
58 for el in lictree[d].traverse():
59 if not os.path.isfile(el.path):
60 continue
61
62 exception = None
Nishanth Menon40751c6c2021-07-07 15:48:40 -050063 for l in open(el.path, encoding="utf-8").readlines():
Thomas Gleixner5385a292018-04-26 15:54:27 +020064 if l.startswith('Valid-License-Identifier:'):
65 lid = l.split(':')[1].strip().upper()
66 if lid in spdx.licenses:
67 raise SPDXException(el, 'Duplicate License Identifier: %s' %lid)
68 else:
69 spdx.licenses.append(lid)
70
71 elif l.startswith('SPDX-Exception-Identifier:'):
72 exception = l.split(':')[1].strip().upper()
73 spdx.exceptions[exception] = []
74
75 elif l.startswith('SPDX-Licenses:'):
76 for lic in l.split(':')[1].upper().strip().replace(' ', '').replace('\t', '').split(','):
77 if not lic in spdx.licenses:
Vincenzo Frascino8d7a7ab2019-05-31 22:30:45 -070078 raise SPDXException(None, 'Exception %s missing license %s' %(exception, lic))
Thomas Gleixner5385a292018-04-26 15:54:27 +020079 spdx.exceptions[exception].append(lic)
80
81 elif l.startswith("License-Text:"):
82 if exception:
83 if not len(spdx.exceptions[exception]):
Vincenzo Frascino8d7a7ab2019-05-31 22:30:45 -070084 raise SPDXException(el, 'Exception %s is missing SPDX-Licenses' %exception)
Thomas Gleixner5385a292018-04-26 15:54:27 +020085 spdx.exception_files += 1
86 else:
87 spdx.license_files += 1
88 break
89 return spdx
90
91class id_parser(object):
92
93 reserved = [ 'AND', 'OR', 'WITH' ]
94 tokens = [ 'LPAR', 'RPAR', 'ID', 'EXC' ] + reserved
95
96 precedence = ( ('nonassoc', 'AND', 'OR'), )
97
98 t_ignore = ' \t'
99
100 def __init__(self, spdx):
101 self.spdx = spdx
102 self.lasttok = None
103 self.lastid = None
104 self.lexer = lex.lex(module = self, reflags = re.UNICODE)
105 # Initialize the parser. No debug file and no parser rules stored on disk
106 # The rules are small enough to be generated on the fly
107 self.parser = yacc.yacc(module = self, write_tables = False, debug = False)
108 self.lines_checked = 0
109 self.checked = 0
Thomas Gleixner0509b272022-05-16 12:27:29 +0200110 self.excluded = 0
Thomas Gleixner5385a292018-04-26 15:54:27 +0200111 self.spdx_valid = 0
112 self.spdx_errors = 0
Thomas Gleixnera377ce72022-05-16 12:27:24 +0200113 self.spdx_dirs = {}
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200114 self.dirdepth = -1
115 self.basedir = '.'
Thomas Gleixner5385a292018-04-26 15:54:27 +0200116 self.curline = 0
117 self.deepest = 0
118
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200119 def set_dirinfo(self, basedir, dirdepth):
120 if dirdepth >= 0:
121 self.basedir = basedir
122 bdir = basedir.lstrip('./').rstrip('/')
123 if bdir != '':
124 parts = bdir.split('/')
125 else:
126 parts = []
127 self.dirdepth = dirdepth + len(parts)
128
Thomas Gleixner5385a292018-04-26 15:54:27 +0200129 # Validate License and Exception IDs
130 def validate(self, tok):
131 id = tok.value.upper()
132 if tok.type == 'ID':
133 if not id in self.spdx.licenses:
134 raise ParserException(tok, 'Invalid License ID')
135 self.lastid = id
136 elif tok.type == 'EXC':
Jeremy Clinebed95c42018-08-17 15:44:01 -0700137 if id not in self.spdx.exceptions:
Thomas Gleixner5385a292018-04-26 15:54:27 +0200138 raise ParserException(tok, 'Invalid Exception ID')
139 if self.lastid not in self.spdx.exceptions[id]:
140 raise ParserException(tok, 'Exception not valid for license %s' %self.lastid)
141 self.lastid = None
142 elif tok.type != 'WITH':
143 self.lastid = None
144
145 # Lexer functions
146 def t_RPAR(self, tok):
147 r'\)'
148 self.lasttok = tok.type
149 return tok
150
151 def t_LPAR(self, tok):
152 r'\('
153 self.lasttok = tok.type
154 return tok
155
156 def t_ID(self, tok):
157 r'[A-Za-z.0-9\-+]+'
158
159 if self.lasttok == 'EXC':
160 print(tok)
161 raise ParserException(tok, 'Missing parentheses')
162
163 tok.value = tok.value.strip()
164 val = tok.value.upper()
165
166 if val in self.reserved:
167 tok.type = val
168 elif self.lasttok == 'WITH':
169 tok.type = 'EXC'
170
171 self.lasttok = tok.type
172 self.validate(tok)
173 return tok
174
175 def t_error(self, tok):
176 raise ParserException(tok, 'Invalid token')
177
178 def p_expr(self, p):
179 '''expr : ID
180 | ID WITH EXC
181 | expr AND expr
182 | expr OR expr
183 | LPAR expr RPAR'''
184 pass
185
186 def p_error(self, p):
187 if not p:
188 raise ParserException(None, 'Unfinished license expression')
189 else:
190 raise ParserException(p, 'Syntax error')
191
192 def parse(self, expr):
193 self.lasttok = None
194 self.lastid = None
195 self.parser.parse(expr, lexer = self.lexer)
196
197 def parse_lines(self, fd, maxlines, fname):
198 self.checked += 1
199 self.curline = 0
Thomas Gleixnera377ce72022-05-16 12:27:24 +0200200 fail = 1
Thomas Gleixner5385a292018-04-26 15:54:27 +0200201 try:
202 for line in fd:
Thierry Reding3a6ab5c2018-12-14 14:17:24 -0800203 line = line.decode(locale.getpreferredencoding(False), errors='ignore')
Thomas Gleixner5385a292018-04-26 15:54:27 +0200204 self.curline += 1
205 if self.curline > maxlines:
206 break
207 self.lines_checked += 1
208 if line.find("SPDX-License-Identifier:") < 0:
209 continue
Thomas Gleixner959b4962019-01-16 11:26:53 +0100210 expr = line.split(':')[1].strip()
211 # Remove trailing comment closure
Aurélien Cedeyna5f4cb42019-02-20 22:18:34 +0100212 if line.strip().endswith('*/'):
Thomas Gleixner959b4962019-01-16 11:26:53 +0100213 expr = expr.rstrip('*/').strip()
Lukas Bulwahnc5c55382020-09-26 21:03:54 +0200214 # Remove trailing xml comment closure
215 if line.strip().endswith('-->'):
216 expr = expr.rstrip('-->').strip()
Thomas Gleixner959b4962019-01-16 11:26:53 +0100217 # Special case for SH magic boot code files
218 if line.startswith('LIST \"'):
219 expr = expr.rstrip('\"').strip()
Thomas Gleixner5385a292018-04-26 15:54:27 +0200220 self.parse(expr)
221 self.spdx_valid += 1
222 #
223 # Should we check for more SPDX ids in the same file and
224 # complain if there are any?
225 #
Thomas Gleixnera377ce72022-05-16 12:27:24 +0200226 fail = 0
Thomas Gleixner5385a292018-04-26 15:54:27 +0200227 break
228
229 except ParserException as pe:
230 if pe.tok:
231 col = line.find(expr) + pe.tok.lexpos
232 tok = pe.tok.value
233 sys.stdout.write('%s: %d:%d %s: %s\n' %(fname, self.curline, col, pe.txt, tok))
234 else:
Ding Xiang28c9f3f2022-01-14 10:40:58 +0800235 sys.stdout.write('%s: %d:0 %s\n' %(fname, self.curline, pe.txt))
Thomas Gleixner5385a292018-04-26 15:54:27 +0200236 self.spdx_errors += 1
237
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200238 if fname == '-':
239 return
240
Thomas Gleixnera377ce72022-05-16 12:27:24 +0200241 base = os.path.dirname(fname)
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200242 if self.dirdepth > 0:
243 parts = base.split('/')
244 i = 0
245 base = '.'
246 while i < self.dirdepth and i < len(parts) and len(parts[i]):
247 base += '/' + parts[i]
248 i += 1
249 elif self.dirdepth == 0:
250 base = self.basedir
251 else:
252 base = './' + base.rstrip('/')
253 base += '/'
254
Thomas Gleixnera377ce72022-05-16 12:27:24 +0200255 di = self.spdx_dirs.get(base, dirinfo())
Thomas Gleixner67924b72022-05-16 12:27:27 +0200256 di.update(fname, base, fail)
Thomas Gleixnera377ce72022-05-16 12:27:24 +0200257 self.spdx_dirs[base] = di
258
Thomas Gleixner0509b272022-05-16 12:27:29 +0200259class pattern(object):
260 def __init__(self, line):
261 self.pattern = line
262 self.match = self.match_file
263 if line == '.*':
264 self.match = self.match_dot
265 elif line.endswith('/'):
266 self.pattern = line[:-1]
267 self.match = self.match_dir
268 elif line.startswith('/'):
269 self.pattern = line[1:]
270 self.match = self.match_fn
271
272 def match_dot(self, fpath):
273 return os.path.basename(fpath).startswith('.')
274
275 def match_file(self, fpath):
276 return os.path.basename(fpath) == self.pattern
277
278 def match_fn(self, fpath):
279 return fnmatch.fnmatchcase(fpath, self.pattern)
280
281 def match_dir(self, fpath):
282 if self.match_fn(os.path.dirname(fpath)):
283 return True
284 return fpath.startswith(self.pattern)
285
286def exclude_file(fpath):
287 for rule in exclude_rules:
288 if rule.match(fpath):
289 return True
290 return False
291
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200292def scan_git_tree(tree, basedir, dirdepth):
293 parser.set_dirinfo(basedir, dirdepth)
Thomas Gleixner5385a292018-04-26 15:54:27 +0200294 for el in tree.traverse():
Thomas Gleixner5385a292018-04-26 15:54:27 +0200295 if not os.path.isfile(el.path):
296 continue
Thomas Gleixner0509b272022-05-16 12:27:29 +0200297 if exclude_file(el.path):
298 parser.excluded += 1
299 continue
Jeremy Clinebed95c42018-08-17 15:44:01 -0700300 with open(el.path, 'rb') as fd:
301 parser.parse_lines(fd, args.maxlines, el.path)
Thomas Gleixner5385a292018-04-26 15:54:27 +0200302
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200303def scan_git_subtree(tree, path, dirdepth):
Thomas Gleixner5385a292018-04-26 15:54:27 +0200304 for p in path.strip('/').split('/'):
305 tree = tree[p]
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200306 scan_git_tree(tree, path.strip('/'), dirdepth)
Thomas Gleixner5385a292018-04-26 15:54:27 +0200307
Thomas Gleixner0509b272022-05-16 12:27:29 +0200308def read_exclude_file(fname):
309 rules = []
310 if not fname:
311 return rules
312 with open(fname) as fd:
313 for line in fd:
314 line = line.strip()
315 if line.startswith('#'):
316 continue
317 if not len(line):
318 continue
319 rules.append(pattern(line))
320 return rules
321
Thomas Gleixner5385a292018-04-26 15:54:27 +0200322if __name__ == '__main__':
323
324 ap = ArgumentParser(description='SPDX expression checker')
325 ap.add_argument('path', nargs='*', help='Check path or file. If not given full git tree scan. For stdin use "-"')
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200326 ap.add_argument('-d', '--dirs', action='store_true',
327 help='Show [sub]directory statistics.')
328 ap.add_argument('-D', '--depth', type=int, default=-1,
329 help='Directory depth for -d statistics. Default: unlimited')
Thomas Gleixner0509b272022-05-16 12:27:29 +0200330 ap.add_argument('-e', '--exclude',
331 help='File containing file patterns to exclude. Default: scripts/spdxexclude')
Thomas Gleixner67924b72022-05-16 12:27:27 +0200332 ap.add_argument('-f', '--files', action='store_true',
333 help='Show files without SPDX.')
Thomas Gleixner5385a292018-04-26 15:54:27 +0200334 ap.add_argument('-m', '--maxlines', type=int, default=15,
335 help='Maximum number of lines to scan in a file. Default 15')
336 ap.add_argument('-v', '--verbose', action='store_true', help='Verbose statistics output')
337 args = ap.parse_args()
338
339 # Sanity check path arguments
340 if '-' in args.path and len(args.path) > 1:
341 sys.stderr.write('stdin input "-" must be the only path argument\n')
342 sys.exit(1)
343
344 try:
345 # Use git to get the valid license expressions
346 repo = git.Repo(os.getcwd())
347 assert not repo.bare
348
349 # Initialize SPDX data
350 spdx = read_spdxdata(repo)
351
Bhaskar Chowdhury40635122021-03-26 14:44:43 +0530352 # Initialize the parser
Thomas Gleixner5385a292018-04-26 15:54:27 +0200353 parser = id_parser(spdx)
354
355 except SPDXException as se:
356 if se.el:
357 sys.stderr.write('%s: %s\n' %(se.el.path, se.txt))
358 else:
359 sys.stderr.write('%s\n' %se.txt)
360 sys.exit(1)
361
362 except Exception as ex:
363 sys.stderr.write('FAIL: %s\n' %ex)
364 sys.stderr.write('%s\n' %traceback.format_exc())
365 sys.exit(1)
366
367 try:
Thomas Gleixner0509b272022-05-16 12:27:29 +0200368 fname = args.exclude
369 if not fname:
370 fname = os.path.join(os.path.dirname(__file__), 'spdxexclude')
371 exclude_rules = read_exclude_file(fname)
372 except Exception as ex:
373 sys.stderr.write('FAIL: Reading exclude file %s: %s\n' %(fname, ex))
374 sys.exit(1)
375
376 try:
Thomas Gleixner5385a292018-04-26 15:54:27 +0200377 if len(args.path) and args.path[0] == '-':
Thierry Reding3a6ab5c2018-12-14 14:17:24 -0800378 stdin = os.fdopen(sys.stdin.fileno(), 'rb')
379 parser.parse_lines(stdin, args.maxlines, '-')
Thomas Gleixner5385a292018-04-26 15:54:27 +0200380 else:
381 if args.path:
382 for p in args.path:
383 if os.path.isfile(p):
Thierry Reding3a6ab5c2018-12-14 14:17:24 -0800384 parser.parse_lines(open(p, 'rb'), args.maxlines, p)
Thomas Gleixner5385a292018-04-26 15:54:27 +0200385 elif os.path.isdir(p):
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200386 scan_git_subtree(repo.head.reference.commit.tree, p,
387 args.depth)
Thomas Gleixner5385a292018-04-26 15:54:27 +0200388 else:
389 sys.stderr.write('path %s does not exist\n' %p)
390 sys.exit(1)
391 else:
392 # Full git tree scan
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200393 scan_git_tree(repo.head.commit.tree, '.', args.depth)
394
395 ndirs = len(parser.spdx_dirs)
396 dirsok = 0
397 if ndirs:
398 for di in parser.spdx_dirs.values():
399 if not di.missing:
400 dirsok += 1
Thomas Gleixner5385a292018-04-26 15:54:27 +0200401
402 if args.verbose:
403 sys.stderr.write('\n')
404 sys.stderr.write('License files: %12d\n' %spdx.license_files)
405 sys.stderr.write('Exception files: %12d\n' %spdx.exception_files)
406 sys.stderr.write('License IDs %12d\n' %len(spdx.licenses))
407 sys.stderr.write('Exception IDs %12d\n' %len(spdx.exceptions))
408 sys.stderr.write('\n')
Thomas Gleixner0509b272022-05-16 12:27:29 +0200409 sys.stderr.write('Files excluded: %12d\n' %parser.excluded)
Thomas Gleixner5385a292018-04-26 15:54:27 +0200410 sys.stderr.write('Files checked: %12d\n' %parser.checked)
411 sys.stderr.write('Lines checked: %12d\n' %parser.lines_checked)
Thomas Gleixner149d6232022-05-16 12:27:22 +0200412 if parser.checked:
413 pc = int(100 * parser.spdx_valid / parser.checked)
414 sys.stderr.write('Files with SPDX: %12d %3d%%\n' %(parser.spdx_valid, pc))
Thomas Gleixner5385a292018-04-26 15:54:27 +0200415 sys.stderr.write('Files with errors: %12d\n' %parser.spdx_errors)
Thomas Gleixnera377ce72022-05-16 12:27:24 +0200416 if ndirs:
417 sys.stderr.write('\n')
418 sys.stderr.write('Directories accounted: %8d\n' %ndirs)
Thomas Gleixnera377ce72022-05-16 12:27:24 +0200419 pc = int(100 * dirsok / ndirs)
420 sys.stderr.write('Directories complete: %8d %3d%%\n' %(dirsok, pc))
Thomas Gleixner5385a292018-04-26 15:54:27 +0200421
Thomas Gleixner0e7f0302022-05-16 12:27:26 +0200422 if ndirs and ndirs != dirsok and args.dirs:
423 if args.verbose:
424 sys.stderr.write('\n')
425 sys.stderr.write('Incomplete directories: SPDX in Files\n')
426 for f in sorted(parser.spdx_dirs.keys()):
427 di = parser.spdx_dirs[f]
428 if di.missing:
429 valid = di.total - di.missing
430 pc = int(100 * valid / di.total)
431 sys.stderr.write(' %-80s: %5d of %5d %3d%%\n' %(f, valid, di.total, pc))
432
Thomas Gleixner67924b72022-05-16 12:27:27 +0200433 if ndirs and ndirs != dirsok and args.files:
434 if args.verbose or args.dirs:
435 sys.stderr.write('\n')
436 sys.stderr.write('Files without SPDX:\n')
437 for f in sorted(parser.spdx_dirs.keys()):
438 di = parser.spdx_dirs[f]
439 for f in sorted(di.files):
440 sys.stderr.write(' %s\n' %f)
441
Thomas Gleixner5385a292018-04-26 15:54:27 +0200442 sys.exit(0)
443
444 except Exception as ex:
445 sys.stderr.write('FAIL: %s\n' %ex)
446 sys.stderr.write('%s\n' %traceback.format_exc())
447 sys.exit(1)