diff options
author | Jeremy Cline <jcline@redhat.com> | 2018-08-17 18:44:01 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2018-08-17 19:20:27 -0400 |
commit | bed95c43c15eb6b1ccc5b09e5ae08cac726c456d (patch) | |
tree | 7e65fdd4f065437bbe1b534efb89fae47dc87ceb | |
parent | fde5e903fb5893415fa49d280e998be226d0898f (diff) |
scripts: add Python 3 compatibility to spdxcheck.py
"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/20180717190635.29467-1-jcline@redhat.com
Signed-off-by: Jeremy Cline <jcline@redhat.com>
Acked-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rwxr-xr-x | scripts/spdxcheck.py | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/scripts/spdxcheck.py b/scripts/spdxcheck.py index a6041f29b18e..839e190bbd7a 100755 --- a/scripts/spdxcheck.py +++ b/scripts/spdxcheck.py | |||
@@ -4,6 +4,7 @@ | |||
4 | 4 | ||
5 | from argparse import ArgumentParser | 5 | from argparse import ArgumentParser |
6 | from ply import lex, yacc | 6 | from ply import lex, yacc |
7 | import locale | ||
7 | import traceback | 8 | import traceback |
8 | import sys | 9 | import sys |
9 | import git | 10 | import git |
@@ -102,7 +103,7 @@ class id_parser(object): | |||
102 | raise ParserException(tok, 'Invalid License ID') | 103 | raise ParserException(tok, 'Invalid License ID') |
103 | self.lastid = id | 104 | self.lastid = id |
104 | elif tok.type == 'EXC': | 105 | elif tok.type == 'EXC': |
105 | if not self.spdx.exceptions.has_key(id): | 106 | if id not in self.spdx.exceptions: |
106 | raise ParserException(tok, 'Invalid Exception ID') | 107 | raise ParserException(tok, 'Invalid Exception ID') |
107 | if self.lastid not in self.spdx.exceptions[id]: | 108 | if self.lastid not in self.spdx.exceptions[id]: |
108 | raise ParserException(tok, 'Exception not valid for license %s' %self.lastid) | 109 | raise ParserException(tok, 'Exception not valid for license %s' %self.lastid) |
@@ -167,6 +168,7 @@ class id_parser(object): | |||
167 | self.curline = 0 | 168 | self.curline = 0 |
168 | try: | 169 | try: |
169 | for line in fd: | 170 | for line in fd: |
171 | line = line.decode(locale.getpreferredencoding(False), errors='ignore') | ||
170 | self.curline += 1 | 172 | self.curline += 1 |
171 | if self.curline > maxlines: | 173 | if self.curline > maxlines: |
172 | break | 174 | break |
@@ -201,7 +203,8 @@ def scan_git_tree(tree): | |||
201 | continue | 203 | continue |
202 | if not os.path.isfile(el.path): | 204 | if not os.path.isfile(el.path): |
203 | continue | 205 | continue |
204 | parser.parse_lines(open(el.path), args.maxlines, el.path) | 206 | with open(el.path, 'rb') as fd: |
207 | parser.parse_lines(fd, args.maxlines, el.path) | ||
205 | 208 | ||
206 | def scan_git_subtree(tree, path): | 209 | def scan_git_subtree(tree, path): |
207 | for p in path.strip('/').split('/'): | 210 | for p in path.strip('/').split('/'): |