Permalink
Browse files

Better pyflakes

  • Loading branch information...
1 parent b775118 commit 01a0f15da532100a0414c99fdb466e2155e3f5cf @lukaszb lukaszb committed Feb 16, 2013
Showing with 93 additions and 43 deletions.
  1. +1 −0 MANIFEST.in
  2. +86 −0 extras.py
  3. +1 −2 guardian/compat.py
  4. +1 −1 guardian/tests/admin_test.py
  5. +1 −1 guardian/tests/urls.py
  6. +3 −39 setup.py
View
1 MANIFEST.in
@@ -2,6 +2,7 @@ include CHANGES
include LICENSE
include README.rst
include MANIFEST.in
+include extras.py
include tests.py
include run_test_and_report.sh
recursive-include guardian *.py
View
86 extras.py
@@ -0,0 +1,86 @@
+import _ast
+import os
+import sys
+from setuptools import Command
+#from pyflakes.scripts import pyflakes as flakes
+
+
+def check(filename):
+ from pyflakes import reporter as mod_reporter
+ from pyflakes.checker import Checker
+ codeString = open(filename).read()
+ reporter = mod_reporter._makeDefaultReporter()
+ # First, compile into an AST and handle syntax errors.
+ try:
+ tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
+ except SyntaxError:
+ value = sys.exc_info()[1]
+ 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.
+ reporter.unexpectedError(filename, 'problem decoding source')
+ else:
+ reporter.syntaxError(filename, msg, lineno, offset, text)
+ return 1
+ except Exception:
+ reporter.unexpectedError(filename, 'problem decoding source')
+ return 1
+ else:
+ # Okay, it's syntactically valid. Now check it.
+ lines = codeString.splitlines()
+ warnings = Checker(tree, filename)
+ warnings.messages.sort(key=lambda m: m.lineno)
+ real_messages = []
+ for m in warnings.messages:
+ line = lines[m.lineno - 1]
+ if 'pyflakes:ignore' in line.rsplit('#', 1)[-1]:
+ # ignore lines with pyflakes:ignore
+ pass
+ else:
+ real_messages.append(m)
+ reporter.flake(m)
+ return len(real_messages)
+
+class RunFlakesCommand(Command):
+ """
+ Runs pyflakes against guardian codebase.
+ """
+ description = "Check sources with pyflakes"
+ user_options = []
+
+ def initialize_options(self):
+ pass
+
+ def finalize_options(self):
+ pass
+
+ def run(self):
+ try:
+ import pyflakes # pyflakes:ignore
+ except ImportError:
+ sys.stderr.write("No pyflakes installed!\n")
+ sys.exit(-1)
+ thisdir = os.path.dirname(__file__)
+ guardiandir = os.path.join(thisdir, 'guardian')
+ warns = 0
+ # Define top-level directories
+ for topdir, dirnames, filenames in os.walk(guardiandir):
+ paths = (os.path.join(topdir, f) for f in filenames if f .endswith('.py'))
+ for path in paths:
+ if path.endswith('tests/__init__.py'):
+ # ignore that module (it should only gather test cases with *)
+ continue
+ warns += check(path)
+ if warns > 0:
+ sys.stderr.write("ERROR: Finished with total %d warnings.\n" % warns)
+ sys.exit(1)
+ else:
+ print "No problems found in source codes."
+
+
View
3 guardian/compat.py
@@ -1,4 +1,3 @@
-import django
from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
@@ -34,4 +33,4 @@
from django.contrib.auth import get_user_model
except ImportError:
from django.contrib.auth.models import User
- get_user_model = lambda: User
+ get_user_model = lambda: User
View
2 guardian/tests/admin_test.py
@@ -282,7 +282,7 @@ def test_view_manage_group(self):
# we simpy clean up AdminTests class ...
# TODO: use @unittest.skipUnless('django.contrib.admin' in settings.INSTALLED_APPS)
# if possible (requires Python 2.7, though)
- AdminTests = type('AdminTests', (TestCase,), {})
+ AdminTests = type('AdminTests', (TestCase,), {}) # pyflakes:ignore
class GuardedModelAdminTests(TestCase):
View
2 guardian/tests/urls.py
@@ -1,5 +1,5 @@
# handler404 and handler500 are needed for admin tests
-from guardian.compat import include, patterns, handler404, handler500
+from guardian.compat import include, patterns, handler404, handler500 # pyflakes:ignore
from django.contrib import admin
admin.autodiscover()
View
42 setup.py
@@ -1,6 +1,7 @@
import os
import sys
-from setuptools import setup, find_packages, Command
+from setuptools import setup, find_packages
+from extras import RunFlakesCommand
guardian = __import__('guardian')
readme_file = os.path.join(os.path.dirname(__file__), 'README.rst')
@@ -11,43 +12,6 @@
"``long_description`` (%s)\n" % readme_file)
sys.exit(1)
-class run_flakes(Command):
- """
- Runs pyflakes against guardian codebase.
- """
- description = "Check sources with pyflakes"
- user_options = []
-
- def initialize_options(self):
- pass
-
- def finalize_options(self):
- pass
-
- def run(self):
- try:
- import pyflakes.scripts.pyflakes as flakes
- except ImportError:
- sys.stderr.write("No pyflakes installed!\n")
- sys.exit(-1)
-
- warns = 0
- # Define top-level directories
- for topdir, dirnames, filenames in os.walk(guardian.__path__[0]):
- paths = (os.path.join(topdir, f) for f in filenames if f .endswith('.py'))
- for path in paths:
- if path.endswith('tests/__init__.py'):
- # ignore that module (it should only gather test cases with *)
- continue
- warns += flakes.checkPath(path)
- if warns > 0:
- sys.stderr.write("ERROR: Finished with total %d warnings.\n" % warns)
- sys.exit(1)
- else:
- print "No problems found in source codes."
-
-
-
setup(
name = 'django-guardian',
version = guardian.get_version(),
@@ -79,6 +43,6 @@ def run(self):
'Topic :: Security',
],
test_suite='tests.main',
- cmdclass={'flakes': run_flakes},
+ cmdclass={'flakes': RunFlakesCommand},
)

0 comments on commit 01a0f15

Please sign in to comment.