diff options
author | Georg Brandl <georg@python.org> | 2014-10-08 09:21:15 +0200 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2014-10-08 09:21:15 +0200 |
commit | 444fb6fd9b3492040a36fcca672fee8175f8d603 (patch) | |
tree | 2bd411ca78decf276b95dc6b1788e594b2e35287 /scripts/check_sources.py | |
parent | 491fec23ef01687906f5d71ee718522cd2917926 (diff) | |
parent | c1bfe4eed3805d3556bffa3c6b9cc2d3f6976205 (diff) | |
download | pygments-444fb6fd9b3492040a36fcca672fee8175f8d603.tar.gz |
Merged in leodemoura/pygments-main (pull request #399)
Diffstat (limited to 'scripts/check_sources.py')
-rwxr-xr-x | scripts/check_sources.py | 59 |
1 files changed, 22 insertions, 37 deletions
diff --git a/scripts/check_sources.py b/scripts/check_sources.py index 94c2c15d..5f233887 100755 --- a/scripts/check_sources.py +++ b/scripts/check_sources.py @@ -23,8 +23,10 @@ from os.path import join, splitext, abspath checkers = {} + def checker(*suffixes, **kwds): only_pkg = kwds.pop('only_pkg', False) + def deco(func): for suffix in suffixes: checkers.setdefault(suffix, []).append(func) @@ -38,8 +40,6 @@ copyright_re = re.compile(r'^ :copyright: Copyright 2006-2014 by ' r'the Pygments team, see AUTHORS\.$', re.UNICODE) copyright_2_re = re.compile(r'^ %s(, %s)*[,.]$' % (name_mail_re, name_mail_re), re.UNICODE) -coding_re = re.compile(r'coding[:=]\s*([-\w.]+)') -not_ix_re = re.compile(r'\bnot\s+\S+?\s+i[sn]\s\S+') is_const_re = re.compile(r'if.*?==\s+(None|False|True)\b') misspellings = ["developement", "adress", "verificate", # ALLOW-MISSPELLING @@ -48,44 +48,30 @@ misspellings = ["developement", "adress", "verificate", # ALLOW-MISSPELLING @checker('.py') def check_syntax(fn, lines): + if '#!/' in lines[0]: + lines = lines[1:] + if 'coding:' in lines[0]: + lines = lines[1:] try: - compile(''.join(lines), fn, "exec") + compile('\n'.join(lines), fn, "exec") except SyntaxError as err: yield 0, "not compilable: %s" % err @checker('.py') def check_style_and_encoding(fn, lines): - encoding = 'ascii' for lno, line in enumerate(lines): - if len(line) > 90: + if len(line) > 110: yield lno+1, "line too long" - m = not_ix_re.search(line) - if m: - yield lno+1, '"' + m.group() + '"' if is_const_re.search(line): yield lno+1, 'using == None/True/False' - if lno < 2: - co = coding_re.search(line) - if co: - encoding = co.group(1) - try: - line.decode(encoding) - except AttributeError: - # Python 3 - encoding was already checked - pass - except UnicodeDecodeError as err: - yield lno+1, "not decodable: %s\n Line: %r" % (err, line) - except LookupError as err: - yield 0, "unknown encoding: %s" % encoding - encoding = 'latin1' @checker('.py', only_pkg=True) def check_fileheader(fn, lines): # line number correction c = 1 - if lines[0:1] == ['#!/usr/bin/env python\n']: + if lines[0:1] == ['#!/usr/bin/env python']: lines = lines[1:] c = 2 @@ -94,31 +80,28 @@ def check_fileheader(fn, lines): for lno, l in enumerate(lines): llist.append(l) if lno == 0: - if l == '# -*- coding: rot13 -*-\n': - # special-case pony package - return - elif l != '# -*- coding: utf-8 -*-\n': + if l != '# -*- coding: utf-8 -*-': yield 1, "missing coding declaration" elif lno == 1: - if l != '"""\n' and l != 'r"""\n': + if l != '"""' and l != 'r"""': yield 2, 'missing docstring begin (""")' else: docopen = True elif docopen: - if l == '"""\n': + if l == '"""': # end of docstring if lno <= 4: yield lno+c, "missing module name in docstring" break - if l != "\n" and l[:4] != ' ' and docopen: + if l != "" and l[:4] != ' ' and docopen: yield lno+c, "missing correct docstring indentation" if lno == 2: # if not in package, don't check the module name modname = fn[:-3].replace('/', '.').replace('.__init__', '') while modname: - if l.lower()[4:-1] == modname: + if l.lower()[4:] == modname: break modname = '.'.join(modname.split('.')[1:]) else: @@ -133,7 +116,7 @@ def check_fileheader(fn, lines): # check for copyright and license fields license = llist[-2:-1] - if license != [" :license: BSD, see LICENSE for details.\n"]: + if license != [" :license: BSD, see LICENSE for details."]: yield 0, "no correct license info" ci = -3 @@ -176,16 +159,19 @@ def main(argv): for root, dirs, files in os.walk(path): if '.hg' in dirs: dirs.remove('.hg') + if 'examplefiles' in dirs: + dirs.remove('examplefiles') if '-i' in opts and abspath(root) in opts['-i']: del dirs[:] continue # XXX: awkward: for the Makefile call: don't check non-package # files for file headers - in_pocoo_pkg = root.startswith('./pygments') + in_pygments_pkg = root.startswith('./pygments') for fn in files: fn = join(root, fn) - if fn[:2] == './': fn = fn[2:] + if fn[:2] == './': + fn = fn[2:] if '-i' in opts and abspath(fn) in opts['-i']: continue @@ -199,15 +185,14 @@ def main(argv): print("Checking %s..." % fn) try: - f = open(fn, 'r') - lines = list(f) + lines = open(fn, 'rb').read().decode('utf-8').splitlines() except (IOError, OSError) as err: print("%s: cannot open: %s" % (fn, err)) num += 1 continue for checker in checkerlist: - if not in_pocoo_pkg and checker.only_pkg: + if not in_pygments_pkg and checker.only_pkg: continue for lno, msg in checker(fn, lines): print(u"%s:%d: %s" % (fn, lno, msg), file=out) |