Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: flake8-abp/setup.py

Issue 29340727: Noissue - Added flake8 extension accounting for our coding style and some other stuff (Closed)
Patch Set: Added more checks, tests, README and addressed comments Created May 7, 2016, 5:13 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # This file is part of Adblock Plus <https://adblockplus.org/>,
2 # Copyright (C) 2006-2016 Eyeo GmbH
3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 from __future__ import print_function
16
17 import os
18 import glob
19 import tokenize
20 import sys
21 import re
22
23 try:
24 from StringIO import StringIO
25 except ImportError:
26 from io import StringIO
27
28 from setuptools import setup, Command
29
30
31 class TestCommand(Command):
Vasily Kuznetsov 2016/05/09 13:39:51 Hm, a custom test runner. I would have probably ju
Sebastian Noack 2016/05/09 16:49:12 I'm not sure how that code would look like if usin
Vasily Kuznetsov 2016/05/09 17:37:32 Yeah, you could plug it into py.test and make it r
32 user_options = []
33
34 def _get_expected_errors(self, filename):
35 errors = set()
36
37 def tokeneater(kind, token, start, end, line):
38 if kind == tokenize.COMMENT:
39 match = re.search(r'^#+[*\s]*(A\d+)', token)
40 if match:
41 try:
42 offset = token.index('*')
43 except ValueError:
44 offset = 0
45 errors.add((start[0] + 1,
46 start[1] + 1 + offset,
47 match.group(1)))
48
49 with open(filename, 'rb') as file:
50 if sys.version_info[0] >= 3:
51 for token in tokenize.tokenize(file.readline):
52 tokeneater(*token)
53 else:
54 tokenize.tokenize(file.readline, tokeneater)
55
56 return errors
57
58 def _get_reported_errors(self, filename, style_guide):
59 orig_stdout = sys.stdout
60 sys.stdout = stdout = StringIO()
61
62 try:
63 style_guide.check_files([filename])
64 finally:
65 sys.stdout = orig_stdout
66
67 stdout.seek(0)
68 errors = set()
69
70 for line in stdout:
71 _, lineno, colno, error = line.split(':', 3)
72 errors.add((int(lineno), int(colno), error.split()[0]))
73
74 return errors
75
76 def initialize_options(self):
77 pass
78
79 def finalize_options(self):
80 pass
81
82 def run(self):
83 import flake8.engine
84
85 directory = os.path.dirname(__file__)
86 filenames = glob.glob(os.path.join(directory, 'tests', '*.py'))
87 style_guide = flake8.engine.get_style_guide()
88 failed = False
89
90 for filename in sorted(filenames):
91 expected = self._get_expected_errors(filename)
92 reported = self._get_reported_errors(filename, style_guide)
93 failures = expected ^ reported
94
95 if not failures:
96 print(filename + ': OK')
97 continue
98
99 for record in sorted(failures):
100 lineno, colno, error = record
101
102 print('{}:{}:{}: '.format(filename, lineno, colno), end='')
103 if record in expected:
104 print(error + ' expected')
105 else:
106 print('unexpected ' + error)
107
108 failed = True
109
110 if failed:
111 sys.exit(1)
112
113
114 setup(
115 name='flake8-abp',
116 version='0.1',
117 py_modules=['flake8_abp'],
118 entry_points={
119 'flake8.extension': [
120 'AXXX = flake8_abp:ASTChecker',
121 'A109-A110 = flake8_abp:check_quotes',
122 'A111 = flake8_abp:check_redundant_parenthesis',
123 'A303 = flake8_abp:check_non_default_encoding',
124 ],
125 },
126 cmdclass={'test': TestCommand}
127 )
OLDNEW

Powered by Google App Engine
This is Rietveld