/
run_flake8.py
72 lines (58 loc) · 2.25 KB
/
run_flake8.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
# coding: utf-8
import os
import sys
from optparse import make_option
import django
import pep8
from flake8.engine import get_style_guide
from ..utils import get_app_locations
from .run_pep8 import Pep8Task
class Flake8Task(Pep8Task):
if django.VERSION < (1, 8):
option_list = Pep8Task.option_list + (
make_option(
'--max-complexity',
dest='max_complexity',
help='McCabe complexity threshold',
),
)
def __init__(self, **options):
super(Flake8Task, self).__init__(**options)
if options.get('flake8_file_output', True):
output_dir = options['output_dir']
if not os.path.exists(output_dir):
os.makedirs(output_dir)
self.output = open(os.path.join(output_dir, 'flake8.report'), 'w')
else:
self.output = sys.stdout
if options['max_complexity']:
self.pep8_options['max_complexity'] = int(options['max_complexity'])
@classmethod
def add_arguments(cls, parser):
Pep8Task.add_arguments(parser)
parser.add_argument('--max-complexity',
dest='max_complexity',
help='McCabe complexity threshold')
def teardown_test_environment(self, **kwargs):
class JenkinsReport(pep8.BaseReport):
def error(instance, line_number, offset, text, check):
code = super(JenkinsReport, instance).error(
line_number, offset, text, check,
)
if not code:
return
sourceline = instance.line_offset + line_number
self.output.write(
'%s:%s:%s: %s\n' %
(instance.filename, sourceline, offset + 1, text),
)
flake8style = get_style_guide(
parse_argv=False,
config_file=self.pep8_rcfile,
reporter=JenkinsReport,
**self.pep8_options)
# Jenkins pep8 validator requires relative paths
project_root = os.path.abspath(os.path.dirname(__name__))
for location in map(lambda x: os.path.relpath(x, project_root), get_app_locations()):
flake8style.input_dir(location)
self.output.close()