Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend linter API to allow overriding the max line length #51

Merged
merged 3 commits into from
May 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ament_cmake_cpplint/cmake/ament_cpplint.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
#
# :param TESTNAME: the name of the test, default: "cpplint"
# :type TESTNAME: string
# :param MAX_LINE_LENGTH: override the maximum line length,
# the default is defined in ament_cpplint
# :type MAX_LINE_LENGTH: integer
# :param ROOT: override the --root option of cpplint
# :type ROOT: string
# :param ARGN: the files or directories to check
# :type ARGN: list of strings
#
# @public
#
function(ament_cpplint)
cmake_parse_arguments(ARG "" "TESTNAME" "" ${ARGN})
cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;ROOT;TESTNAME" "" ${ARGN})
if(NOT ARG_TESTNAME)
set(ARG_TESTNAME "cpplint")
endif()
Expand All @@ -35,6 +40,12 @@ function(ament_cpplint)

set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${ARG_TESTNAME}.xunit.xml")
set(cmd "${ament_cpplint_BIN}" "--xunit-file" "${result_file}")
if(ARG_MAX_LINE_LENGTH)
list(APPEND cmd "--linelength" "${ARG_MAX_LINE_LENGTH}")
endif()
if(ARG_ROOT)
list(APPEND cmd "--root" "${ARG_ROOT}")
endif()
list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS})

file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ament_cpplint")
Expand Down
8 changes: 7 additions & 1 deletion ament_cmake_pep8/cmake/ament_pep8.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
#
# :param TESTNAME: the name of the test, default: "pep8"
# :type TESTNAME: string
# :param MAX_LINE_LENGTH: override the maximum line length,
# the default is defined in ament_cpplint
# :type MAX_LINE_LENGTH: integer
# :param ARGN: the files or directories to check
# :type ARGN: list of strings
#
# @public
#
function(ament_pep8)
cmake_parse_arguments(ARG "" "TESTNAME" "" ${ARGN})
cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;TESTNAME" "" ${ARGN})
if(NOT ARG_TESTNAME)
set(ARG_TESTNAME "pep8")
endif()
Expand All @@ -35,6 +38,9 @@ function(ament_pep8)

set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${ARG_TESTNAME}.xunit.xml")
set(cmd "${ament_pep8_BIN}" "--xunit-file" "${result_file}")
if(ARG_MAX_LINE_LENGTH)
list(APPEND cmd "--linelength" "${ARG_MAX_LINE_LENGTH}")
endif()
list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS})

file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ament_pep8")
Expand Down
8 changes: 7 additions & 1 deletion ament_cmake_uncrustify/cmake/ament_uncrustify.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
#
# :param TESTNAME: the name of the test, default: "uncrustify"
# :type TESTNAME: string
# :param MAX_LINE_LENGTH: override the maximum line length,
# the default is defined in ament_uncrustify
# :type MAX_LINE_LENGTH: integer
# :param ARGN: the files or directories to check
# :type ARGN: list of strings
#
# @public
#
function(ament_uncrustify)
cmake_parse_arguments(ARG "" "TESTNAME" "" ${ARGN})
cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;TESTNAME" "" ${ARGN})
if(NOT ARG_TESTNAME)
set(ARG_TESTNAME "uncrustify")
endif()
Expand All @@ -35,6 +38,9 @@ function(ament_uncrustify)

set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${ARG_TESTNAME}.xunit.xml")
set(cmd "${ament_uncrustify_BIN}" "--xunit-file" "${result_file}")
if(ARG_MAX_LINE_LENGTH)
list(APPEND cmd "--linelength" "${ARG_MAX_LINE_LENGTH}")
endif()
list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS})

file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ament_uncrustify")
Expand Down
28 changes: 18 additions & 10 deletions ament_pep8/ament_pep8/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def main(argv=sys.argv[1:]):
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='*',
Expand All @@ -60,7 +63,9 @@ def main(argv=sys.argv[1:]):
print("Could not config file '%s'" % args.config_file, file=sys.stderr)
return 1

report = generate_pep8_report(args.config_file, args.paths, args.excludes)
report = generate_pep8_report(
args.config_file, args.paths, args.excludes,
max_line_length=args.linelength)

# print statistics about errors
if report.total_errors:
Expand Down Expand Up @@ -100,14 +105,17 @@ def main(argv=sys.argv[1:]):
return rc


def generate_pep8_report(config_file, paths, excludes):
pep8style = CustomStyleGuide(
repeat=True,
show_source=True,
verbose=True,
reporter=CustomReport,
config_file=config_file,
)
def generate_pep8_report(config_file, paths, excludes, max_line_length=None):
kwargs = {
'repeat': True,
'show_source': True,
'verbose': True,
'reporter': CustomReport,
'config_file': config_file,
}
if max_line_length is not None:
kwargs['max_line_length'] = max_line_length
pep8style = CustomStyleGuide(**kwargs)
if excludes:
pep8style.options.exclude += excludes
return pep8style.check_files(paths)
Expand Down Expand Up @@ -193,7 +201,7 @@ def error(self, line_number, offset, text, check):
'column': offset + 1,
'error_code': code,
'error_message': text,
'source_line': line.splitlines()[0],
'source_line': line.splitlines()[0] if line else '',
})
return code

Expand Down
14 changes: 11 additions & 3 deletions ament_pyflakes/ament_pyflakes/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ def main(argv=sys.argv[1:]):
print(filename)
checkPath(filename, reporter=reporter)
for error in reporter.errors:
print(error, file=sys.stderr)
try:
print(error, file=sys.stderr)
except TypeError:
# this can happen if the line contains percent characters
print(error.__dict__, file=sys.stderr)
report.append((filename, reporter.errors))
print('')

Expand Down Expand Up @@ -145,13 +149,17 @@ def get_xunit_content(report, testname, elapsed):
if errors:
# report each pyflakes error as a failing testcase
for error in errors:
try:
msg = error.message % error.message_args
except TypeError:
# this can happen if the line contains percent characters
msg = error.message + ' ' + str(error.message_args)
data = {
'quoted_name': quoteattr(
'%s (%s:%d)' % (
type(error).__name__, filename, error.lineno)),
'testname': testname,
'quoted_message': quoteattr(
error.message % error.message_args),
'quoted_message': quoteattr(msg),
}
xml += ''' <testcase
name=%(quoted_name)s
Expand Down
19 changes: 19 additions & 0 deletions ament_uncrustify/ament_uncrustify/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
from __future__ import print_function

import argparse
from configparser import ConfigParser
import difflib
import filecmp
import os
import re
import shutil
import subprocess
import sys
Expand All @@ -45,6 +47,9 @@ def main(argv=sys.argv[1:]):
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='*',
Expand Down Expand Up @@ -72,6 +77,20 @@ def main(argv=sys.argv[1:]):
print("Could not config file '%s'" % args.config_file, file=sys.stderr)
return 1

if args.linelength is not None:
# check if different from config file
config = ConfigParser()
with open(args.config_file, 'r') as h:
config_str = h.read()
config.read_string('[DEFAULT]\n' + config_str)
code_width = config['DEFAULT']['code_width']
code_width = int(re.split('[ \t#]', code_width, maxsplit=1)[0])
if args.linelength != code_width:
# generate temporary config file with custom line length
temp_config = tempfile.NamedTemporaryFile('w')
Copy link
Contributor Author

@dirk-thomas dirk-thomas May 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

Fixed in #52.

temp_config.write(config_str + '\ncode_width=%d' % args.linelength)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appends the config parameter, but does not remove the original if it's already set. Will that work correctly in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in an ini-like file the last value is the one being used.

args.config_file = temp_config.name

if args.xunit_file:
start_time = time.time()

Expand Down