From 47fb1178287ca3b619cae74a6962d8c45f521093 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 9 Apr 2013 12:30:24 +0530 Subject: [PATCH] Switch to using flake8 to check for errors --- session.vim | 3 --- setup.cfg | 4 +++ setup/check.py | 49 ++++++------------------------------ src/calibre/db/tests/main.py | 5 ++++ src/calibre/linux.py | 2 +- 5 files changed, 18 insertions(+), 45 deletions(-) create mode 100644 setup.cfg diff --git a/session.vim b/session.vim index 5e127428cf..a67c5ed8e6 100644 --- a/session.vim +++ b/session.vim @@ -1,6 +1,3 @@ -" Project wide builtins -let $PYFLAKES_BUILTINS = "_,dynamic_property,__,P,I,lopen,icu_lower,icu_upper,icu_title,ngettext" - " Include directories for C++ modules let g:syntastic_cpp_include_dirs = [ \'/usr/include/python2.7', diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000000..ba2629d20f --- /dev/null +++ b/setup.cfg @@ -0,0 +1,4 @@ +[flake8] +max-line-length = 160 +builtins = _,dynamic_property,__,P,I,lopen,icu_lower,icu_upper,icu_title,ngettext +ignore = E12,E221,E301,E302,E304,E401,W391 diff --git a/setup/check.py b/setup/check.py index 0baec38a50..281527e51a 100644 --- a/setup/check.py +++ b/setup/check.py @@ -22,40 +22,12 @@ class Message: self.filename, self.lineno, self.msg = filename, lineno, msg def __str__(self): - return '%s:%s: %s'%(self.filename, self.lineno, self.msg) - -def check_for_python_errors(code_string, filename): - import _ast - # First, compile into an AST and handle syntax errors. - try: - tree = compile(code_string, filename, "exec", _ast.PyCF_ONLY_AST) - except (SyntaxError, IndentationError) as value: - msg = value.args[0] - - (lineno, offset, text) = value.lineno, value.offset, value.text - - # If there's an encoding problem with the file, the text is None. - if text is None: - # Avoid using msg, since for the only known case, it contains a - # bogus message that claims the encoding the file declared was - # unknown. - msg = "%s: problem decoding source" % filename - - return [Message(filename, lineno, msg)] - else: - checker = __import__('pyflakes.checker').checker - # Okay, it's syntactically valid. Now check it. - w = checker.Checker(tree, filename) - w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno)) - return [Message(x.filename, x.lineno, x.message%x.message_args) for x in - w.messages] + return '%s:%s: %s' % (self.filename, self.lineno, self.msg) class Check(Command): description = 'Check for errors in the calibre source code' - BUILTINS = ['_', '__', 'dynamic_property', 'I', 'P', 'lopen', 'icu_lower', - 'icu_upper', 'icu_title', 'ngettext'] CACHE = '.check-cache.pickle' def get_files(self, cache): @@ -65,10 +37,10 @@ class Check(Command): mtime = os.stat(y).st_mtime if cache.get(y, 0) == mtime: continue - if (f.endswith('.py') and f not in ('feedparser.py', - 'pyparsing.py', 'markdown.py') and - 'prs500/driver.py' not in y): - yield y, mtime + if (f.endswith('.py') and f not in ( + 'feedparser.py', 'pyparsing.py', 'markdown.py') and + 'prs500/driver.py' not in y): + yield y, mtime if f.endswith('.coffee'): yield y, mtime @@ -79,25 +51,22 @@ class Check(Command): if f.endswith('.recipe') and cache.get(f, 0) != mtime: yield f, mtime - def run(self, opts): cache = {} if os.path.exists(self.CACHE): cache = cPickle.load(open(self.CACHE, 'rb')) - builtins = list(set_builtins(self.BUILTINS)) for f, mtime in self.get_files(cache): self.info('\tChecking', f) errors = False ext = os.path.splitext(f)[1] if ext in {'.py', '.recipe'}: - w = check_for_python_errors(open(f, 'rb').read(), f) - if w: + p = subprocess.Popen(['flake8', '--ignore=E,W', f]) + if p.wait() != 0: errors = True - self.report_errors(w) else: from calibre.utils.serve_coffee import check_coffeescript try: - check_coffeescript(f) + check_coffeescript(f) except: errors = True if errors: @@ -106,8 +75,6 @@ class Check(Command): self.j(self.SRC, '../session.vim'), '-f', f]) raise SystemExit(1) cache[f] = mtime - for x in builtins: - delattr(__builtin__, x) cPickle.dump(cache, open(self.CACHE, 'wb'), -1) wn_path = os.path.expanduser('~/work/servers/src/calibre_servers/main') if os.path.exists(wn_path): diff --git a/src/calibre/db/tests/main.py b/src/calibre/db/tests/main.py index 7268db3e99..7b9c63af22 100644 --- a/src/calibre/db/tests/main.py +++ b/src/calibre/db/tests/main.py @@ -9,6 +9,11 @@ __docformat__ = 'restructuredtext en' import unittest, os, argparse +try: + import init_calibre # noqa +except ImportError: + pass + def find_tests(): return unittest.defaultTestLoader.discover(os.path.dirname(os.path.abspath(__file__)), pattern='*.py') diff --git a/src/calibre/linux.py b/src/calibre/linux.py index 395831fa8f..d2b0c941a6 100644 --- a/src/calibre/linux.py +++ b/src/calibre/linux.py @@ -33,7 +33,7 @@ entry_points = { 'fetch-ebook-metadata = calibre.ebooks.metadata.sources.cli:main', 'calibre-smtp = calibre.utils.smtp:main', ], - 'gui_scripts' : [ + 'gui_scripts' : [ __appname__+' = calibre.gui2.main:main', 'lrfviewer = calibre.gui2.lrf_renderer.main:main', 'ebook-viewer = calibre.gui2.viewer.main:main',