-
Notifications
You must be signed in to change notification settings - Fork 58
/
lint.py
410 lines (341 loc) · 13.3 KB
/
lint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
"""Flake8 lint worker."""
from __future__ import print_function
import ast
import os
import sys
try:
from configparser import RawConfigParser
except ImportError:
from ConfigParser import RawConfigParser
try:
from io import TextIOWrapper
except ImportError:
pass
# Add 'contrib' to sys.path to simulate installation of package 'flake8'
# and it's dependencies: 'pyflake', 'pep8', 'mccabe' and 'pep8-naming'
CONTRIB_PATH = os.path.join(os.path.dirname(__file__), 'contrib')
if CONTRIB_PATH not in sys.path:
sys.path.insert(0, CONTRIB_PATH)
from flake8 import __version__ as flake8_version
from flake8._pyflakes import patch_pyflakes
import flake8_debugger
from flake8_import_order import (
__version__ as flake8_import_order_version,
ImportOrderChecker
)
import mccabe
import pep8
import pep8ext_naming
from pydocstyle import (
__version__ as pydocstyle_version,
PEP257Checker
)
from pyflakes import (
__version__ as pyflakes_version,
checker as pyflakes_checker
)
import pyflakes.api
patch_pyflakes()
if sys.platform.startswith('win'):
DEFAULT_CONFIG_FILE = os.path.expanduser(r'~\.flake8')
else:
DEFAULT_CONFIG_FILE = os.path.join(
os.getenv('XDG_CONFIG_HOME') or os.path.expanduser('~/.config'),
'flake8'
)
CONFIG_FILES = ('setup.cfg', 'tox.ini', '.pep8')
def tools_versions():
"""Return all lint tools versions."""
return (
('pep8', pep8.__version__),
('flake8', flake8_version),
('pyflakes', pyflakes_version),
('mccabe', mccabe.__version__),
('pydocstyle', pydocstyle_version),
('naming', pep8ext_naming.__version__),
('debugger', flake8_debugger.__version__),
('import-order', flake8_import_order_version),
)
class Pep8Report(pep8.BaseReport):
"""Collect all check results."""
def __init__(self, options):
"""Initialize reporter."""
super(Pep8Report, self).__init__(options)
# errors "collection"
self.errors = []
def error(self, line_number, offset, text, check):
"""Get error and save it into errors collection."""
code = super(Pep8Report, self).error(line_number, offset, text, check)
if code:
self.errors.append((self.line_offset + line_number, offset, text))
return code
class FlakesReporter(object):
"""Formats the results of pyflakes to the linter.
Example at 'pyflakes.reporter.Reporter' class.
"""
def __init__(self):
"""Construct a Reporter."""
# errors "collection"
self.errors = []
def unexpectedError(self, filename, msg): # noqa
"""An unexpected error occurred trying to process filename."""
self.errors.append((0, 0, msg))
def syntaxError(self, filename, msg, lineno, offset, text): # noqa
"""
There was a syntax errror in filename.
"""
line = text.splitlines()[-1]
if offset is not None:
offset = offset - (len(text) - len(line))
self.errors.append((lineno, offset, msg))
else:
self.errors.append((lineno, 0, msg))
def flake(self, msg):
"""Add pyflakes error to the list of errors."""
# unused import has no col attr, seems buggy... this fixes it
col = getattr(msg, 'col', 0)
self.errors.append(
(msg.lineno, col, msg.flake8_msg % msg.message_args)
)
class ImportOrderLinter(ImportOrderChecker):
"""Import order linter."""
def __init__(self, tree, filename, lines, order_style='cryptography'):
"""Initialize linter."""
super(ImportOrderLinter, self).__init__(filename, tree)
self.lines = lines
self.options = {
'import_order_style': order_style,
}
def load_file(self):
"""Load file."""
pass
def error(self, node, code, message):
"""Format lint error."""
lineno, col_offset = node.lineno, node.col_offset
return (lineno, col_offset, '{0} {1}'.format(code, message))
def run(self):
"""Run lint."""
for error in self.check_order():
yield error
def load_flake8_config(filename, global_config=False, project_config=False):
"""Return flake8 settings from config file.
More info: http://flake8.readthedocs.org/en/latest/config.html
"""
parser = RawConfigParser()
# check global config
if global_config and os.path.isfile(DEFAULT_CONFIG_FILE):
parser.read(DEFAULT_CONFIG_FILE)
# search config in filename dir and all parent dirs
if project_config:
parent = tail = os.path.abspath(filename)
while tail:
if parser.read([os.path.join(parent, fn) for fn in CONFIG_FILES]):
break
parent, tail = os.path.split(parent)
result = {}
if parser.has_section('flake8'):
options = (
('ignore', 'ignore', 'list'),
('select', 'select', 'list'),
('exclude', 'ignore_files', 'list'),
('max_line_length', 'pep8_max_line_length', 'int')
)
for config, plugin, option_type in options:
if not parser.has_option('flake8', config):
config = config.replace('_', '-')
if parser.has_option('flake8', config):
if option_type == 'list':
option_value = parser.get('flake8', config).strip()
if option_value:
result[plugin] = option_value.split(',')
elif option_type == 'int':
option_value = parser.get('flake8', config).strip()
if option_value:
result[plugin] = parser.getint('flake8', config)
return result
def lint(lines, settings):
"""Run flake8 lint with internal interpreter."""
warnings = []
# lint with pep8
if settings.get('pep8', True):
pep8style = pep8.StyleGuide(
reporter=Pep8Report,
ignore=['DIRTY-HACK'], # PEP8 error will never starts like this
max_line_length=settings.get('pep8_max_line_length')
)
pep8style.input_file(filename=None, lines=lines.splitlines(True))
warnings.extend(pep8style.options.report.errors)
if settings.get('pydocstyle', False):
for error in PEP257Checker().check_source(lines, ''):
warnings.append((
getattr(error, 'line', 0),
0,
getattr(error, 'message', '')
))
try:
tree = compile(lines, '', 'exec', ast.PyCF_ONLY_AST, True)
except (SyntaxError, TypeError):
(exc_type, exc) = sys.exc_info()[:2]
if len(exc.args) > 1:
offset = exc.args[1]
if len(offset) > 2:
offset = offset[1:3]
else:
offset = (1, 0)
warnings.append((
offset[0],
offset[1] or 0,
'E901 %s: %s' % (exc_type.__name__, exc.args[0])
))
else:
# lint with pyflakes
if settings.get('pyflakes', True):
builtins = settings.get('builtins')
if builtins: # builtins is extended
# some magic (ok, ok, monkey-patching) goes here
pyflakes.checker.Checker.builtIns = (
pyflakes.checker.Checker.builtIns.union(builtins)
)
w = pyflakes_checker.Checker(tree)
w.messages.sort(key=lambda m: m.lineno)
reporter = FlakesReporter()
for warning in w.messages:
reporter.flake(warning)
warnings.extend(reporter.errors)
# lint with naming
if settings.get('naming', True):
checker = pep8ext_naming.NamingChecker(tree, None)
for error in checker.run():
warnings.append(error[0:3])
# lint with flake8-debugger
if settings.get('debugger', True):
debug_warns = flake8_debugger.check_tree_for_debugger_statements(
tree, []
)
for warn in debug_warns:
warnings.append(
(warn.get("line"), warn.get("col"), warn.get("message"))
)
# lint with import order
if settings.get('import_order', False):
order_style = settings.get('import_order_style')
import_linter = ImportOrderLinter(tree, None, lines, order_style)
for error in import_linter.run():
warnings.append(error[0:3])
# check complexity
try:
complexity = int(settings.get('complexity', -1))
except (TypeError, ValueError):
complexity = -1
if complexity > -1:
mccabe.McCabeChecker.max_complexity = complexity
checker = mccabe.McCabeChecker(tree, None)
for error in checker.run():
warnings.append(error[0:3])
return sorted(warnings, key=lambda e: '{0:09d}{1:09d}'.format(e[0], e[1]))
def lint_external(lines, settings, interpreter, linter):
"""Run flake8 lint with external interpreter."""
import subprocess
# first argument is interpreter
arguments = [interpreter, linter]
# do we need to run pyflake lint
if settings.get('pyflakes', True):
arguments.append('--pyflakes')
builtins = settings.get('builtins')
if builtins:
arguments.append('--builtins')
arguments.append(','.join(builtins))
# do we need to run pep8 lint
if settings.get('pep8', True):
arguments.append('--pep8')
max_line_length = settings.get('pep8_max_line_length', 79)
arguments.append('--pep8-max-line-length')
arguments.append(str(max_line_length))
# do we need to run pydocstyle lint
if settings.get('pydocstyle', False):
arguments.append('--pydocstyle')
# do we need to run naming lint
if settings.get('naming', True):
arguments.append('--naming')
# do we need to run debugger lint
if settings.get('debugger', True):
arguments.append('--debugger')
# do we need to run import order lint
if settings.get('import_order', False):
arguments.append('--import-order')
# get import order style
import_order_style = settings.get('import_order_style')
if import_order_style in ('cryptography', 'google'):
arguments.extend(('--import-order-style', import_order_style))
else:
arguments.extend(('--import-order-style', 'cryptography'))
# do we need to run complexity check
complexity = settings.get('complexity', -1)
arguments.extend(('--complexity', str(complexity)))
# place for warnings =)
warnings = []
startupinfo = None
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# run subprocess
proc = subprocess.Popen(
arguments,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
startupinfo=startupinfo
)
result = proc.communicate(input=lines.encode('utf-8'))[0]
# parse STDOUT for warnings and errors
for line in result.splitlines():
line = line.decode('utf-8').strip()
warning = line.split(':', 2)
if len(warning) == 3:
try:
warnings.append((int(warning[0]), int(warning[1]), warning[2]))
except (TypeError, ValueError):
print("Flake8Lint ERROR: {0}".format(line))
else:
print("Flake8Lint ERROR: {0}".format(line))
# and return them =)
return warnings
if __name__ == "__main__":
import argparse
# parse arguments
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--pyflakes', action='store_true',
help="run pyflakes lint")
arg_parser.add_argument('--builtins',
help="python builtins extend")
arg_parser.add_argument('--pep8', action='store_true',
help="run pep8 lint")
arg_parser.add_argument('--pydocstyle', action='store_true',
help="run pydocstyle lint")
arg_parser.add_argument('--naming', action='store_true',
help="run naming lint")
arg_parser.add_argument('--debugger', action='store_true',
help="run debugger lint")
arg_parser.add_argument('--import-order', action='store_true',
help="run import order lint")
arg_parser.add_argument('--import-order-style',
help="import order style: cryptography or google")
arg_parser.add_argument('--complexity', type=int,
help="check complexity")
arg_parser.add_argument('--pep8-max-line-length', type=int, default=79,
help="pep8 max line length")
lint_settings = arg_parser.parse_args().__dict__
if lint_settings.get('builtins'):
lint_settings['builtins'] = lint_settings['builtins'].split(',')
if '' == ''.encode(): # Python 2: implicit encoding
stdin_lines = sys.stdin.read()
else: # Python 3
stdin_lines = TextIOWrapper(sys.stdin.buffer, errors='ignore').read()
# run lint and print errors
for lint_warning in lint(stdin_lines, lint_settings):
try:
print("%d:%d:%s" % lint_warning)
except Exception:
print(lint_warning)
sys.stdout.flush()