Skip to content

Commit d8f6cc3

Browse files
committed
Fixed flakes and readded missing Thread import
1 parent 3e92cb4 commit d8f6cc3

File tree

5 files changed

+96
-39
lines changed

5 files changed

+96
-39
lines changed

extras.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import _ast
2+
import os
3+
import sys
4+
from setuptools import Command
5+
6+
7+
def check(filename):
8+
from pyflakes import reporter as mod_reporter
9+
from pyflakes.checker import Checker
10+
codeString = open(filename).read()
11+
reporter = mod_reporter._makeDefaultReporter()
12+
# First, compile into an AST and handle syntax errors.
13+
try:
14+
tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
15+
except SyntaxError:
16+
value = sys.exc_info()[1]
17+
msg = value.args[0]
18+
19+
(lineno, offset, text) = value.lineno, value.offset, value.text
20+
21+
# If there's an encoding problem with the file, the text is None.
22+
if text is None:
23+
# Avoid using msg, since for the only known case, it contains a
24+
# bogus message that claims the encoding the file declared was
25+
# unknown.
26+
reporter.unexpectedError(filename, 'problem decoding source')
27+
else:
28+
reporter.syntaxError(filename, msg, lineno, offset, text)
29+
return 1
30+
except Exception:
31+
reporter.unexpectedError(filename, 'problem decoding source')
32+
return 1
33+
else:
34+
# Okay, it's syntactically valid. Now check it.
35+
lines = codeString.splitlines()
36+
warnings = Checker(tree, filename)
37+
warnings.messages.sort(key=lambda m: m.lineno)
38+
real_messages = []
39+
for m in warnings.messages:
40+
line = lines[m.lineno - 1]
41+
if 'pyflakes:ignore' in line.rsplit('#', 1)[-1]:
42+
# ignore lines with pyflakes:ignore
43+
pass
44+
else:
45+
real_messages.append(m)
46+
reporter.flake(m)
47+
return len(real_messages)
48+
49+
50+
class RunFlakesCommand(Command):
51+
"""
52+
Runs pyflakes against guardian codebase.
53+
"""
54+
description = "Check sources with pyflakes"
55+
user_options = []
56+
ignore = ['__init__.py', '__main__.py', 'hgcompat.py']
57+
58+
def initialize_options(self):
59+
pass
60+
61+
def finalize_options(self):
62+
pass
63+
64+
def run(self):
65+
try:
66+
import pyflakes # pyflakes:ignore
67+
except ImportError:
68+
sys.stderr.write("No pyflakes installed!\n")
69+
sys.exit(-1)
70+
thisdir = os.path.dirname(__file__)
71+
vcsdir = os.path.join(thisdir, 'vcs')
72+
warns = 0
73+
# Define top-level directories
74+
for topdir, dirnames, filenames in os.walk(vcsdir):
75+
filenames = (f for f in filenames if f not in self.ignore)
76+
paths = (os.path.join(topdir, f) for f in filenames if f.endswith('.py'))
77+
for path in paths:
78+
if path.endswith('tests/__init__.py'):
79+
# ignore that module (it should only gather test cases with *)
80+
continue
81+
warns += check(path)
82+
if warns > 0:
83+
sys.stderr.write("ERROR: Finished with total %d warnings.\n" % warns)
84+
sys.exit(1)
85+
else:
86+
print("No problems found in source codes.")
87+
88+

setup.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
3-
from setuptools import setup, find_packages, Command
3+
from setuptools import setup, find_packages
4+
from extras import RunFlakesCommand
45

56
vcs = __import__('vcs')
67
readme_file = os.path.abspath(os.path.join(os.path.dirname(__file__),
@@ -18,38 +19,6 @@
1819
install_requires.append('unittest2')
1920
tests_require = install_requires + ['dulwich', 'mercurial']
2021

21-
class run_flakes(Command):
22-
description = 'Runs code against pyflakes'
23-
user_options = []
24-
25-
def initialize_options(self):
26-
pass
27-
28-
def finalize_options(self):
29-
pass
30-
31-
def run(self):
32-
try:
33-
import pyflakes.scripts.pyflakes as flakes
34-
except ImportError:
35-
print "Audit requires PyFlakes installed in your system."
36-
sys.exit(-1)
37-
38-
warns = 0
39-
# Define top-level directories
40-
dirs = ['vcs']
41-
ignore = ['__init__.py', 'hgcompat.py']
42-
for dir in dirs:
43-
for root, _, files in os.walk(dir):
44-
for file in files:
45-
if file not in ignore and file.endswith('.py') :
46-
warns += flakes.checkPath(os.path.join(root, file))
47-
if warns > 0:
48-
sys.stderr.write("ERROR: Finished with total %d warnings.\n" % warns)
49-
sys.exit(1)
50-
else:
51-
print "No problems found in sourcecode."
52-
5322

5423
setup(
5524
name='vcs',
@@ -78,5 +47,5 @@ def run(self):
7847
'vcs = vcs:main',
7948
],
8049
},
81-
cmdclass={'flakes': run_flakes},
50+
cmdclass={'flakes': RunFlakesCommand},
8251
)

vcs/commands/summary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def post_process(self, repo, **options):
5858
output = ['']
5959
output.extend([
6060
'%s: %s' % (label.rjust(max_label_size+3), value)
61-
for label, value in stats
61+
for label, value in stats # pyflakes:ignore
6262
])
6363
output.append('')
6464
output.append('')

vcs/tests/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from __init__ import main
1+
from __init__ import * # allows to run tests with: python vcs/tests
22

33
if __name__ == '__main__':
44
main()

vcs/utils/compat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ def __deepcopy__(self, memo={}):
153153
#==============================================================================
154154

155155
if sys.version_info >= (2, 6):
156-
from threading import Event
156+
from threading import Event, Thread
157157
else:
158-
from threading import _Verbose, Lock, _time, \
159-
_allocate_lock, RLock, _sleep
158+
from threading import (Thread, _Verbose, Lock, _time, # pyflakes:ignore
159+
_allocate_lock, RLock, _sleep)
160160

161161
def Condition(*args, **kwargs):
162162
return _Condition(*args, **kwargs)

0 commit comments

Comments
 (0)