summaryrefslogtreecommitdiff
path: root/pyflakes
diff options
context:
space:
mode:
authorexarkun <exarkun>2008-08-28 14:33:07 +0000
committerexarkun <exarkun>2008-08-28 14:33:07 +0000
commit0ccef85c950c91712e6126339ff7b0535f1d5708 (patch)
tree2180db36e01ba2c2e95765e4e36b549076e193ee /pyflakes
parent1cd653d38b521d1e8ea10410830cf29683a8c09f (diff)
downloadpyflakes-0ccef85c950c91712e6126339ff7b0535f1d5708.tar.gz
Merge pyflakes-trailing-whitespace-2663
Author: exarkun Reviewer: pjd Fixes: #2663 Work-around a strangeness in the Python compiler which caused failures on source with trailing whitespace but no trailing newline (by adding a trailing newline). Also, re-organize for the command line `pyflakes` tool into a real module so that it can be properly unit tested.
Diffstat (limited to 'pyflakes')
-rw-r--r--pyflakes/scripts/__init__.py0
-rw-r--r--pyflakes/scripts/pyflakes.py55
-rw-r--r--pyflakes/test/test_script.py23
3 files changed, 78 insertions, 0 deletions
diff --git a/pyflakes/scripts/__init__.py b/pyflakes/scripts/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/pyflakes/scripts/__init__.py
diff --git a/pyflakes/scripts/pyflakes.py b/pyflakes/scripts/pyflakes.py
new file mode 100644
index 0000000..4d5c46c
--- /dev/null
+++ b/pyflakes/scripts/pyflakes.py
@@ -0,0 +1,55 @@
+
+"""
+Implementation of the command-line I{pyflakes} tool.
+"""
+
+import compiler, sys
+import os
+
+checker = __import__('pyflakes.checker').checker
+
+def check(codeString, filename):
+ try:
+ tree = compiler.parse(codeString)
+ except (SyntaxError, IndentationError):
+ value = sys.exc_info()[1]
+ try:
+ (lineno, offset, line) = value[1][1:]
+ except IndexError:
+ print >> sys.stderr, 'could not compile %r' % (filename,)
+ return 1
+ if line.endswith("\n"):
+ line = line[:-1]
+ print >> sys.stderr, '%s:%d: could not compile' % (filename, lineno)
+ print >> sys.stderr, line
+ print >> sys.stderr, " " * (offset-2), "^"
+ return 1
+ else:
+ 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):
+ if os.path.exists(filename):
+ return check(file(filename, 'U').read() + '\n', filename)
+
+
+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'):
+ warnings += checkPath(os.path.join(dirpath, filename))
+ else:
+ warnings += checkPath(arg)
+ else:
+ warnings += check(sys.stdin.read(), '<stdin>')
+
+ raise SystemExit(warnings > 0)
diff --git a/pyflakes/test/test_script.py b/pyflakes/test/test_script.py
new file mode 100644
index 0000000..2c2a0aa
--- /dev/null
+++ b/pyflakes/test/test_script.py
@@ -0,0 +1,23 @@
+
+"""
+Tests for L{pyflakes.scripts.pyflakes}.
+"""
+
+from twisted.python.filepath import FilePath
+from twisted.trial.unittest import TestCase
+
+from pyflakes.scripts.pyflakes import checkPath
+
+class CheckTests(TestCase):
+ """
+ Tests for L{check} and L{checkPath} which check a file for flakes.
+ """
+ def test_missingTrailingNewline(self):
+ """
+ Source which doesn't end with a newline shouldn't cause any
+ exception to be raised nor an error indicator to be returned by
+ L{check}.
+ """
+ fName = self.mktemp()
+ FilePath(fName).setContent("def foo():\n\tpass\n\t")
+ self.assertFalse(checkPath(fName))