diff --git a/README b/README index 0a5eadd..27b900d 100644 --- a/README +++ b/README @@ -5,10 +5,13 @@ Flake8 Flake8 is just a merge of the PyFlakes and the pep8 projects into one single script. -It runs both tests by launching the single 'flake8' script, and also adds a -few features: +It runs both tests by launching the single 'flake8' script, but ignores pep8 +options and just uses its defaults. + +It also adds a few features: - lines that contains a "# NOQA" comment at the end will not issue a warning +- merging pep8 and pyflakes options - more things to come.. Original projects: diff --git a/bin/flake8 b/bin/flake8 index 0732e9a..7150262 100755 --- a/bin/flake8 +++ b/bin/flake8 @@ -1,5 +1,5 @@ #!/usr/bin/python -from flake8.scripts.flake8 import main +from flake8 import main if __name__ == '__main__': main() diff --git a/flake8/__init__.py b/flake8/__init__.py index 792d600..baf501f 100644 --- a/flake8/__init__.py +++ b/flake8/__init__.py @@ -1 +1,99 @@ -# + +""" +Implementation of the command-line I{flake8} tool. +""" + +import sys +import os +import _ast +import pep8 + +checker = __import__('flake8.checker').checker + +def check(codeString, filename): + """ + Check the Python source given by C{codeString} for flakes. + + @param codeString: The Python source to check. + @type codeString: C{str} + + @param filename: The name of the file the source came from, used to report + errors. + @type filename: C{str} + + @return: The number of warnings emitted. + @rtype: C{int} + """ + # First, compile into an AST and handle syntax errors. + try: + tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST) + except SyntaxError, 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. + print >> sys.stderr, "%s: problem decoding source" % (filename, ) + else: + line = text.splitlines()[-1] + + if offset is not None: + offset = offset - (len(text) - len(line)) + + print >> sys.stderr, '%s:%d: %s' % (filename, lineno, msg) + print >> sys.stderr, line + + if offset is not None: + print >> sys.stderr, " " * offset, "^" + + return 1 + else: + # Okay, it's syntactically valid. Now check it. + w = checker.Checker(tree, filename) + w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno)) + for warning in w.messages: + print warning + return len(w.messages) + + +def checkPath(filename): + """ + Check the given path, printing out any warnings detected. + + @return: the number of warnings printed + """ + try: + return check(file(filename, 'U').read() + '\n', filename) + except IOError, msg: + print >> sys.stderr, "%s: %s" % (filename, msg.args[1]) + return 1 + + +def main(): + pep8.process_options() + + warnings = 0 + args = sys.argv[1:] + if args: + for arg in args: + if os.path.isdir(arg): + for dirpath, dirnames, filenames in os.walk(arg): + for filename in filenames: + if filename.endswith('.py'): + fullpath = os.path.join(dirpath, filename) + warnings += checkPath(fullpath) + warnings += pep8.input_file(fullpath) + else: + warnings += checkPath(arg) + warnings += pep8.input_file(arg) + + else: + stdin = sys.stdin.read() + warnings += check(stdin, '') + + + raise SystemExit(warnings > 0) diff --git a/flake8/checker.py b/flake8/checker.py index 6d711f1..bef72bd 100644 --- a/flake8/checker.py +++ b/flake8/checker.py @@ -6,8 +6,7 @@ import __builtin__ import os.path import _ast -from pyflakes import messages - +from flake8 import messages # utility function to iterate over an AST node's children, adapted # from Python 2.6's standard ast module diff --git a/flake8/pep8.py b/flake8/pep8.py index f40c524..1f572f5 100644 --- a/flake8/pep8.py +++ b/flake8/pep8.py @@ -981,7 +981,7 @@ def input_file(filename): count = options.counters.get(code, 0) if count == 0 and 'not' not in basename: message("%s: error %s not found" % (filename, code)) - return len(errors) + return errors def input_dir(dirname): """ diff --git a/flake8/scripts/__init__.py b/flake8/scripts/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/flake8/scripts/flake8.py b/flake8/scripts/flake8.py deleted file mode 100644 index 2566502..0000000 --- a/flake8/scripts/flake8.py +++ /dev/null @@ -1,99 +0,0 @@ - -""" -Implementation of the command-line I{flake8} tool. -""" - -import sys -import os -import _ast - -from flake8.pep8 import input_file -from flake8 import pep8 - -checker = __import__('flake8.checker').checker - -def check(codeString, filename): - """ - Check the Python source given by C{codeString} for flakes. - - @param codeString: The Python source to check. - @type codeString: C{str} - - @param filename: The name of the file the source came from, used to report - errors. - @type filename: C{str} - - @return: The number of warnings emitted. - @rtype: C{int} - """ - # First, compile into an AST and handle syntax errors. - try: - tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST) - except SyntaxError, 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. - print >> sys.stderr, "%s: problem decoding source" % (filename, ) - else: - line = text.splitlines()[-1] - - if offset is not None: - offset = offset - (len(text) - len(line)) - - print >> sys.stderr, '%s:%d: %s' % (filename, lineno, msg) - print >> sys.stderr, line - - if offset is not None: - print >> sys.stderr, " " * offset, "^" - - return 1 - else: - # Okay, it's syntactically valid. Now check it. - w = checker.Checker(tree, filename) - w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno)) - for warning in w.messages: - print warning - return len(w.messages) - - -def checkPath(filename): - """ - Check the given path, printing out any warnings detected. - - @return: the number of warnings printed - """ - try: - return check(file(filename, 'U').read() + '\n', filename) - except IOError, msg: - print >> sys.stderr, "%s: %s" % (filename, msg.args[1]) - return 1 - - -def main(): - warnings = 0 - args = sys.argv[1:] - if args: - for arg in args: - if os.path.isdir(arg): - for dirpath, dirnames, filenames in os.walk(arg): - for filename in filenames: - if filename.endswith('.py'): - fullpath = os.path.join(dirpath, filename) - warnings += checkPath(fullpath)) - warnings += input_file(fullpath) - else: - warnings += checkPath(arg) - warnings += input_file(arg) - - else: - stdin = sys.stdin.read() - warnings += check(stdin, '') - - - raise SystemExit(warnings > 0) diff --git a/setup.py b/setup.py index ccdef52..969c599 100755 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( description="code checking", author="Tarek Ziade", url="http://bitbucket.org/tarek/flake8", - packages=["flake8", "flake8.scripts", "flake8.test"], + packages=["flake8", "flake8.test"], scripts=["bin/flake8"], long_description=README, classifiers=[