Skip to content

Commit

Permalink
Better pyflakes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszb committed Feb 16, 2013
1 parent b775118 commit 01a0f15
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 43 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
86 changes: 86 additions & 0 deletions extras.py
Original file line number Diff line number Diff line change
@@ -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."


3 changes: 1 addition & 2 deletions guardian/compat.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion guardian/tests/admin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion guardian/tests/urls.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
42 changes: 3 additions & 39 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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(),
Expand Down Expand Up @@ -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.