From e33a3cf35a7c1f8341a6fdb74467c21a568ae005 Mon Sep 17 00:00:00 2001 From: Graham Hayes Date: Tue, 2 Jun 2015 16:36:23 +0100 Subject: [PATCH] Test - add nicer formating to flake8 output Change-Id: Ie1ee4ae5416277f5012646c8b47477e61ec55be0 --- .gitignore | 1 + tools/pretty_flake8.py | 88 ++++++++++++++++++++++++++++++++++++++++++ tools/pretty_flake8.sh | 6 +++ tox.ini | 6 +-- 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100755 tools/pretty_flake8.py create mode 100755 tools/pretty_flake8.sh diff --git a/.gitignore b/.gitignore index 0e3d1284..f26d93aa 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ functionaltests/tempest.log functionaltests/.testrepository/ *.ipynb /.ipynb_checkpoints/* +flake8_results.html diff --git a/tools/pretty_flake8.py b/tools/pretty_flake8.py new file mode 100755 index 00000000..f08c1684 --- /dev/null +++ b/tools/pretty_flake8.py @@ -0,0 +1,88 @@ +# Copyright 2015 Hewlett-Packard Development Company, L.P. +# +# 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 re +import sys +import linecache + +from prettytable import PrettyTable + +PEP8_LINE = r'^((?P.*):(?P\d*):(?P\d*):) ' \ + '(?P(?P\w\d{1,3})(?P.*$))' + +HTML = True + + +def main(): + + raw_errors = [] + + max_filename_len = 0 + for line in sys.stdin: + m = re.match(PEP8_LINE, line) + if m: + m = m.groupdict() + raw_errors.append(m) + if len(m['file']) > max_filename_len: + max_filename_len = len(m['file']) + else: + print line + + print 'Flake8 Results' + + ct = PrettyTable([ + "File", + "Line", + "Column", + "Error Code", + "Error Message", + "Code" + ]) + + ct.align["File"] = "l" + ct.align["Error Message"] = "l" + ct.align["Code"] = "l" + + for line in raw_errors: + ct.add_row(format_dict(line)) + + print ct + + with open('flake8_results.html', 'w') as f: + f.write('%s' % ct.get_html_string(attributes = {"cellspacing": 0})) # noqa + + +def format_dict(raw): + output = [] + if raw['file'].startswith('./'): + output.append(raw['file'][2:]) + else: + output.append(raw['file']) + + output.append(raw['line']) + output.append(raw['col']) + output.append(raw['error_code']) + + output.append(raw['error_desc'].lstrip()) + + code_string = linecache.getline( + output[0], + int(raw['line'])).lstrip().rstrip() + + output.append(code_string) + + return output + +if __name__ == '__main__': + main() diff --git a/tools/pretty_flake8.sh b/tools/pretty_flake8.sh new file mode 100755 index 00000000..b552383a --- /dev/null +++ b/tools/pretty_flake8.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +TESTARGS=$1 + +exec 3>&1 +status=$(exec 4>&1 >&3; ( flake8 ; echo $? >&4 ) | python tools/pretty_flake8.py) && exit $status diff --git a/tox.ini b/tox.ini index 54c74c8b..d19d173a 100644 --- a/tox.ini +++ b/tox.ini @@ -29,14 +29,14 @@ commands = python setup.py testr --coverage --testr-args='{posargs}' commands = oslo_debug_helper {posargs} [testenv:flake8] -commands = flake8 +commands = sh tools/pretty_flake8.sh [testenv:pep8] basepython = python2.7 -commands = flake8 +commands = sh tools/pretty_flake8.sh [testenv:pyflakes] -commands = flake8 +commands = sh tools/pretty_flake8.sh [testenv:bashate] deps = bashate