-
Notifications
You must be signed in to change notification settings - Fork 106
/
main.py
executable file
·340 lines (283 loc) · 10.8 KB
/
main.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
#!/usr/bin/env python3
# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import sys
import time
from xml.sax.saxutils import escape
from xml.sax.saxutils import quoteattr
import flake8
from flake8.api.legacy import StyleGuide
from flake8.main import application as flake8_app
from flake8.main import options as flake8_options
def main(argv=sys.argv[1:]):
rc, _ = main_with_errors(argv=argv)
return rc
def main_with_errors(argv=sys.argv[1:]):
config_file = os.path.join(
os.path.dirname(__file__), 'configuration', 'ament_flake8.ini')
parser = argparse.ArgumentParser(
description='Check code using flake8.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--config',
metavar='path',
default=config_file,
dest='config_file',
help='The config file')
parser.add_argument(
'--linelength', metavar='N', type=int,
help='The maximum line length (default: specified in the config file)')
parser.add_argument(
'paths',
nargs='*',
default=[os.curdir],
help='The files or directories to check. For directories files ending '
"in '.py' will be considered.")
parser.add_argument(
'--exclude',
metavar='filename',
nargs='*',
dest='excludes',
help='The filenames to exclude.')
# not using a file handle directly
# in order to prevent leaving an empty file when something fails early
parser.add_argument(
'--xunit-file',
help='Generate a xunit compliant XML file')
args = parser.parse_args(argv)
if args.xunit_file:
start_time = time.time()
if not os.path.exists(args.config_file):
print("Could not find config file '%s'" % args.config_file, file=sys.stderr)
return 1
# Filter out folders having AMENT_IGNORE
if args.excludes is None:
args.excludes = []
for dirpath, dirnames, filenames in os.walk(os.getcwd()):
if 'AMENT_IGNORE' in dirnames + filenames:
dirnames[:] = []
args.excludes.append(dirpath)
else:
# ignore folder starting with . or _
args.excludes.extend(d for d in dirnames if d[0] in ['.', '_'])
report = generate_flake8_report(
args.config_file, args.paths, args.excludes,
max_line_length=args.linelength)
# print statistics about errors
if report.total_errors:
print('')
report.print_statistics()
# print summary
print('')
print('%d files checked' % len(report.files))
if not report.total_errors:
print('No problems found')
rc = 0
else:
print('%d errors' % (report.total_errors))
print('')
error_type_counts = get_error_type_counts(report.get_error_codes())
for k, v in error_type_counts.items():
print("'%s'-type errors: %d" % (k, v))
rc = 1
print('')
print('Checked files:')
print(''.join(['\n* %s' % f for f in report.files]))
# generate xunit file
if args.xunit_file:
folder_name = os.path.basename(os.path.dirname(args.xunit_file))
file_name = os.path.basename(args.xunit_file)
suffix = '.xml'
if file_name.endswith(suffix):
file_name = file_name[0:-len(suffix)]
suffix = '.xunit'
if file_name.endswith(suffix):
file_name = file_name[0:-len(suffix)]
testname = '%s.%s' % (folder_name, file_name)
xml = get_xunit_content(report, testname, time.time() - start_time)
path = os.path.dirname(os.path.abspath(args.xunit_file))
if not os.path.exists(path):
os.makedirs(path)
with open(args.xunit_file, 'w') as f:
f.write(xml)
return rc, [format_error(e) for e in report.errors]
def get_flake8_style_guide(argv):
# This is a modified version of flake8.legacy.get_style_guide() in which we pass argv through
# to parse_configuration_and_cli(), as opposed to a dict of flake8 options.
# Since we are using config files and a mix plugins, it is not trivial to determine the
# appropriate options to pass into the standard flake8.legacy.get_style_guide();
# passing argv gets it to determine the options for us.
application = flake8_app.Application()
if hasattr(application, 'parse_preliminary_options'):
prelim_opts, remaining_args = application.parse_preliminary_options(
argv)
flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file)
from flake8.options import config
config_finder = config.ConfigFileFinder(
application.program, prelim_opts.append_config,
config_file=prelim_opts.config,
ignore_config_files=prelim_opts.isolated)
application.find_plugins(config_finder)
application.register_plugin_options()
application.parse_configuration_and_cli(config_finder, remaining_args)
else:
application.parse_preliminary_options_and_args([])
flake8.configure_logging(
application.prelim_opts.verbose, application.prelim_opts.output_file)
application.make_config_finder()
application.find_plugins()
application.register_plugin_options()
application.parse_configuration_and_cli(argv)
application.make_formatter()
try:
# needed in older flake8 versions to populate the listener
application.make_notifier()
except AttributeError:
pass
application.make_guide()
application.make_file_checker_manager()
return StyleGuide(application)
def parse_config_file(config_file):
from flake8.options import config, manager, aggregator
try:
# Support 4.0.0
opts_manager = manager.OptionManager(prog='flake8', version='4.0.0')
flake8_options.register_default_options(opts_manager)
return aggregator.aggregate_options(
opts_manager,
config.ConfigFileFinder('flake8', [], config_file),
[]
)
except TypeError:
opts_manager = manager.OptionManager(prog='flake8', version='3.0.0')
flake8_options.register_default_options(opts_manager)
return aggregator.aggregate_options(
opts_manager,
config.ConfigFileFinder('flake8', [], [config_file]),
[]
)
def generate_flake8_report(config_file, paths, excludes, max_line_length=None):
opts, _ = parse_config_file(config_file)
# Ignore flake8 defaults here
if opts.exclude != list(flake8_options.defaults.EXCLUDE):
# Explicitly append to exclude args to prevent config excludes
# from being overwritten
excludes.extend(opts.exclude)
flake8_argv = []
if config_file is not None:
flake8_argv.append('--config={0}'.format(config_file))
if len(excludes) > 0:
flake8_argv.append('--exclude={0}'.format(','.join(excludes)))
if max_line_length is not None:
flake8_argv.append('--max-line-length={0}'.format(max_line_length))
style = get_flake8_style_guide(flake8_argv)
# Monkey patch formatter to collect all errors
format_func = style._application.formatter.format
report = CustomReport()
def custom_format(error):
format_func(error)
report.add_error(error)
print('')
print(format_error(error))
style._application.formatter.format = custom_format
# Get the names of files checked
report.report = style.check_files(paths)
file_checkers = style._application.file_checker_manager.checkers
report.files = [file_checker.filename for file_checker in file_checkers]
assert report.report.total_errors == len(report.errors)
return report
def format_error(error):
return '%s:%d:%d: %s %s' % (
error.filename, error.line_number, error.column_number, error.code,
error.text)
def get_xunit_content(report, testname, elapsed):
data = {
'testname': testname,
'test_count': max(report.total_errors, 1),
'error_count': report.total_errors,
'time': '%.3f' % round(elapsed, 3),
}
xml = """<?xml version="1.0" encoding="UTF-8"?>
<testsuite
name="%(testname)s"
tests="%(test_count)d"
errors="0"
failures="%(error_count)d"
time="%(time)s"
>
""" % data
if report.errors:
# report each flake8 error/warning as a failing testcase
for error in report.errors:
data = {
'quoted_name': quoteattr(
'%s (%s:%d:%d)' % (
error.code, error.filename, error.line_number,
error.column_number)),
'testname': testname,
'quoted_message': quoteattr(
'%s:\n%s' % (error.text, error.physical_line)),
}
xml += """ <testcase
name=%(quoted_name)s
classname="%(testname)s"
>
<failure message=%(quoted_message)s/>
</testcase>
""" % data
else:
# if there are no flake8 problems report a single successful test
data = {
'testname': testname,
}
xml += """ <testcase
name="flake8"
classname="%(testname)s"/>
""" % data
# output list of checked files
data = {
'escaped_files': escape(''.join(['\n* %s' % f for f in report.files])),
}
xml += """ <system-out>Checked files:%(escaped_files)s</system-out>
""" % data
xml += '</testsuite>\n'
return xml
def get_error_type_counts(error_codes):
# Determine the type of error by the first character in its error code
# e.g. 'E261' is type 'E'
error_types = sorted({e[0] for e in error_codes})
# Create dictionary of error code types and their counts
error_type_counts = {}
for error_type in error_types:
error_type_counts[error_type] = len([
e for e in error_codes if e.startswith(error_type)])
return error_type_counts
class CustomReport:
def __init__(self):
self.files = []
self.errors = []
self.report = None
@property
def total_errors(self):
return len(self.errors)
def add_error(self, error):
self.errors.append(error)
def get_error_codes(self):
return [e.code for e in self.errors]
def print_statistics(self):
self.report._application.report_statistics()
if __name__ == '__main__':
sys.exit(main())