Skip to content

Commit

Permalink
scripts: add Python 3 compatibility to spdxcheck.py
Browse files Browse the repository at this point in the history
"dict.has_key(key)" on dictionaries has been replaced with "key in
dict".  Additionally, when run under Python 3 some files don't decode
with the default encoding (tested with UTF-8).  To handle that, don't
open the file in text mode and decode text line-by-line, ignoring
encoding errors.

This remains compatible with Python 2 and should have no functional
change.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Jeremy Cline <[email protected]>
Acked-by: Thomas Gleixner <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
jeremycline authored and torvalds committed Aug 17, 2018
1 parent fde5e90 commit bed95c4
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions scripts/spdxcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from argparse import ArgumentParser
from ply import lex, yacc
import locale
import traceback
import sys
import git
Expand Down Expand Up @@ -102,7 +103,7 @@ def validate(self, tok):
raise ParserException(tok, 'Invalid License ID')
self.lastid = id
elif tok.type == 'EXC':
if not self.spdx.exceptions.has_key(id):
if id not in self.spdx.exceptions:
raise ParserException(tok, 'Invalid Exception ID')
if self.lastid not in self.spdx.exceptions[id]:
raise ParserException(tok, 'Exception not valid for license %s' %self.lastid)
Expand Down Expand Up @@ -167,6 +168,7 @@ def parse_lines(self, fd, maxlines, fname):
self.curline = 0
try:
for line in fd:
line = line.decode(locale.getpreferredencoding(False), errors='ignore')
self.curline += 1
if self.curline > maxlines:
break
Expand Down Expand Up @@ -201,7 +203,8 @@ def scan_git_tree(tree):
continue
if not os.path.isfile(el.path):
continue
parser.parse_lines(open(el.path), args.maxlines, el.path)
with open(el.path, 'rb') as fd:
parser.parse_lines(fd, args.maxlines, el.path)

def scan_git_subtree(tree, path):
for p in path.strip('/').split('/'):
Expand Down

0 comments on commit bed95c4

Please sign in to comment.