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

use Python 3 for cpplint and split into Python and CMake package #39

Merged
merged 3 commits into from
Nov 7, 2015
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
23 changes: 23 additions & 0 deletions ament_cmake_cpplint/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 2.8.3)

project(ament_cmake_cpplint NONE)

find_package(ament_cmake_core REQUIRED)
find_package(ament_cmake_test REQUIRED)

ament_package(
CONFIG_EXTRAS "ament_cmake_cpplint-extras.cmake"
)

install(
DIRECTORY cmake
DESTINATION share/${PROJECT_NAME}
)

if(AMENT_ENABLE_TESTING)
find_package(ament_cmake_copyright REQUIRED)
ament_copyright()

find_package(ament_cmake_lint_cmake REQUIRED)
ament_lint_cmake()
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# copied from ament_cpplint/ament_cpplint-extras.cmake
# copied from ament_cmake_cpplint/ament_cmake_cpplint-extras.cmake

find_package(ament_cmake_test QUIET REQUIRED)

set(ament_cpplint_BIN "${ament_cpplint_DIR}/../../../bin/ament_cpplint")
include("${ament_cmake_cpplint_DIR}/ament_cpplint.cmake")

include("${ament_cpplint_DIR}/ament_cpplint.cmake")

ament_register_extension("ament_lint_auto" "ament_cpplint"
"ament_cpplint_lint_hook.cmake")
ament_register_extension("ament_lint_auto" "ament_cmake_cpplint"
"ament_cmake_cpplint_lint_hook.cmake")
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ function(ament_cpplint)
set(ARG_TESTNAME "cpplint")
endif()

find_program(ament_cpplint_BIN NAMES "ament_cpplint")
if(NOT ament_cpplint_BIN)
message(FATAL_ERROR "ament_cpplint() variable 'ament_cpplint_BIN' must not be empty")
message(FATAL_ERROR "ament_cpplint() could not find program 'ament_cpplint'")
endif()

# cpplint only works with Python 2
set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${ARG_TESTNAME}.xunit.xml")
set(cmd "python2" "${ament_cpplint_BIN}" "--xunit-file" "${result_file}")
set(cmd "${ament_cpplint_BIN}" "--xunit-file" "${result_file}")
list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS})

file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ament_cpplint")
Expand Down
43 changes: 43 additions & 0 deletions ament_cmake_cpplint/doc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ament_cmake_cpplint
===================

Checks the code style of C / C++ source files using `cpplint
<https://github.com/google/styleguide>`_.
Files with the following extensions are being considered:
``.c``, ``.cc``, ``.cpp``, ``.cxx``, ``.h``, ``.hh``, ``.hpp``, ``.hxx``.


How to run the check from the command line?
-------------------------------------------

The command line tool is provided by the package `ament_cpplint
<https://github.com/ament/ament_lint>`_.


How to run the check from within a CMake ament package as part of the tests?
----------------------------------------------------------------------------

``package.xml``:

.. code:: xml

<buildtool_depend>ament_cmake</buildtool_depend>
<test_depend>ament_cmake_cpplint</test_depend>

``CMakeLists.txt``:

.. code:: cmake

find_package(ament_cmake REQUIRED)
if(AMENT_ENABLE_TESTING)
find_package(ament_cmake_cpplint REQUIRED)
ament_cpplint()
endif()

When running multiple linters as part of the CMake tests the documentation of
the package `ament_lint_auto <https://github.com/ament/ament_lint>`_ might
contain some useful information.

The documentation of the package `ament_cmake_test
<https://github.com/ament/ament_cmake>`_ provides more information on testing
in CMake ament packages.
23 changes: 23 additions & 0 deletions ament_cmake_cpplint/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<package format="2">
<name>ament_cmake_cpplint</name>
<version>0.0.0</version>
<description>
The CMake API for ament_cpplint to lint C / C++ code using cpplint.
</description>
<maintainer email="[email protected]">Dirk Thomas</maintainer>
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake_core</buildtool_depend>
<buildtool_depend>ament_cmake_test</buildtool_depend>

<buildtool_export_depend>ament_cmake_test</buildtool_export_depend>
<buildtool_export_depend>ament_cpplint</buildtool_export_depend>

<test_depend>ament_cmake_copyright</test_depend>
<test_depend>ament_cmake_lint_cmake</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
35 changes: 0 additions & 35 deletions ament_cpplint/CMakeLists.txt

This file was deleted.

78 changes: 0 additions & 78 deletions ament_cpplint/ament_cpplint/__init__.py
Original file line number Diff line number Diff line change
@@ -1,78 +0,0 @@
# Copyright 2014-2015 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.

from xml.sax.saxutils import escape
from xml.sax.saxutils import quoteattr


def get_xunit_content(report, testname, elapsed):
test_count = sum([max(len(r[1]), 1) for r in report])
error_count = sum([len(r[1]) for r in report])
data = {
'testname': testname,
'test_count': test_count,
'error_count': error_count,
'time': '%.3f' % round(elapsed, 3),
}
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<testsuite
name="%(testname)s"
tests="%(test_count)d"
failures="%(error_count)d"
time="%(time)s"
>
''' % data

for (filename, errors) in report:

if errors:
# report each cpplint error as a failing testcase
for error in errors:
data = {
'quoted_name': quoteattr(
'%s [%s] (%s:%d)' % (
error['category'], error['confidence'],
filename, error['linenum'])),
'testname': testname,
'quoted_message': quoteattr(error['message']),
}
xml += ''' <testcase
name=%(quoted_name)s
classname="%(testname)s"
>
<failure message=%(quoted_message)s/>
</testcase>
''' % data

else:
# if there are no cpplint errors report a single successful test
data = {
'quoted_location': quoteattr(filename),
'testname': testname,
}
xml += ''' <testcase
name=%(quoted_location)s
classname="%(testname)s"
status="No errors"/>
''' % data

# output list of checked files
data = {
'escaped_files': escape(''.join(['\n* %s' % r[0] for r in report])),
}
xml += ''' <system-out>Checked files:%(escaped_files)s</system-out>
''' % data

xml += '</testsuite>\n'
return xml
41 changes: 32 additions & 9 deletions ament_cpplint/ament_cpplint/cpplint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (c) 2009 Google Inc. All rights reserved.
#
Expand Down Expand Up @@ -54,6 +54,21 @@
import sys
import unicodedata

Py3k = (sys.version_info[0] == 3)
"""A boolean to check if we are running Python3000"""

try:
xrange(0, 1)
except NameError:
xrange = range
try:
unicode
except NameError:
basestring = unicode = str
try:
long
except NameError:
long = int

_USAGE = """
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
Expand Down Expand Up @@ -842,7 +857,11 @@ def IncrementErrorCount(self, category):

def PrintErrorCounts(self):
"""Print a summary of errors by category, and the total."""
for category, count in self.errors_by_category.iteritems():
try:
items = self.errors_by_category.iteritems()
except AttributeError:
items = self.errors_by_category.items()
for category, count in items:
sys.stderr.write('Category \'%s\' errors found: %d\n' %
(category, count))
sys.stderr.write('Total errors found: %d\n' % self.error_count)
Expand Down Expand Up @@ -1817,7 +1836,7 @@ def CheckForBadCharacters(filename, lines, error):
error: The function to call with any errors found.
"""
for linenum, line in enumerate(lines):
if u'\ufffd' in line:
if unicode(b'\xef\xbf\xbd', 'utf-8') in line:
error(filename, linenum, 'readability/utf8', 5,
'Line contains invalid UTF-8 (or Unicode replacement character).')
if '\0' in line:
Expand Down Expand Up @@ -4703,7 +4722,10 @@ def _GetTextInside(text, start_pattern):

# Give opening punctuations to get the matching close-punctuations.
matching_punctuation = {'(': ')', '{': '}', '[': ']'}
closing_punctuation = set(matching_punctuation.itervalues())
try:
closing_punctuation = set(matching_punctuation.values())
except AttributeError:
closing_punctuation = set(matching_punctuation.itervalues())

# Find the position to start extracting text.
match = re.search(start_pattern, text, re.M)
Expand Down Expand Up @@ -5677,7 +5699,7 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,

# include_dict is modified during iteration, so we iterate over a copy of
# the keys.
header_keys = include_dict.keys()
header_keys = list(include_dict.keys())
for header in header_keys:
(same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
fullpath = common_path + header
Expand Down Expand Up @@ -6312,10 +6334,11 @@ def main():

# Change stderr to write with replacement characters so we don't die
# if we try to print something containing non-ASCII characters.
sys.stderr = codecs.StreamReaderWriter(sys.stderr,
codecs.getreader('utf8'),
codecs.getwriter('utf8'),
'replace')
if not Py3k:
sys.stderr = codecs.StreamReaderWriter(sys.stderr,
codecs.getreader('utf8'),
codecs.getwriter('utf8'),
'replace')

_cpplint_state.ResetErrorCounts()
for filename in filenames:
Expand Down
Loading