Skip to content

Commit 9edcfc0

Browse files
[ament_cmake_lint_cmake] Add file exclude support (#458)
In the `ament_lint_cmake` CMake function, the optional list argument `EXCLUDE` can now be used as an exclusion specifier. The `ament_cmake_lint_cmake` respects the `AMENT_LINT_AUTO_FILE_EXCLUDE` variable. Signed-off-by: Guillaume Autran <[email protected]>
1 parent 1e737ea commit 9edcfc0

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

ament_cmake_lint_cmake/cmake/ament_cmake_lint_cmake_lint_hook.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ file(GLOB_RECURSE _cmake_files FOLLOW_SYMLINKS
1818
)
1919
if(_cmake_files)
2020
message(STATUS "Added test 'lint_cmake' to check CMake code style")
21-
ament_lint_cmake()
21+
if(DEFINED AMENT_LINT_AUTO_FILE_EXCLUDE)
22+
ament_lint_cmake(EXCLUDE ${AMENT_LINT_AUTO_FILE_EXCLUDE})
23+
else()
24+
ament_lint_cmake()
25+
endif()
2226
endif()

ament_cmake_lint_cmake/cmake/ament_lint_cmake.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
# :param MAX_LINE_LENGTH: override the maximum line length,
2121
# the default is defined in ament_lint_cmake
2222
# :type MAX_LINE_LENGTH: integer
23+
# :param EXCLUDE: an optional list of exclude files or directories for cmake lint check
24+
# :type EXCLUDE: list
2325
# :param ARGN: the files or directories to check
2426
# :type ARGN: list of strings
2527
#
2628
# @public
2729
#
2830
function(ament_lint_cmake)
29-
cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;TESTNAME" "" ${ARGN})
31+
cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;TESTNAME" "EXCLUDE" ${ARGN})
3032
if(NOT ARG_TESTNAME)
3133
set(ARG_TESTNAME "lint_cmake")
3234
endif()
@@ -41,6 +43,9 @@ function(ament_lint_cmake)
4143
if(DEFINED ARG_MAX_LINE_LENGTH)
4244
list(APPEND cmd "--linelength" "${ARG_MAX_LINE_LENGTH}")
4345
endif()
46+
if(ARG_EXCLUDE)
47+
list(APPEND cmd "--exclude" "${ARG_EXCLUDE}")
48+
endif()
4449
list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS})
4550

4651
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ament_lint_cmake")

ament_lint_cmake/ament_lint_cmake/main.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616

1717
import argparse
18+
import glob
1819
import os
1920
import sys
2021
import time
@@ -51,6 +52,13 @@ def main(argv=sys.argv[1:]):
5152
parser.add_argument(
5253
'--linelength', metavar='N', type=int, default=140,
5354
help='The maximum line length')
55+
parser.add_argument(
56+
'--exclude',
57+
metavar='filename',
58+
nargs='*',
59+
default=[],
60+
dest='excludes',
61+
help='The filenames to exclude.')
5462
# not using a file handle directly
5563
# in order to prevent leaving an empty file when something fails early
5664
parser.add_argument(
@@ -61,7 +69,7 @@ def main(argv=sys.argv[1:]):
6169
if args.xunit_file:
6270
start_time = time.time()
6371

64-
files = get_files(args.paths)
72+
files = get_files(args.paths, args.excludes)
6573
if not files:
6674
print('No files found', file=sys.stderr)
6775
return 1
@@ -125,7 +133,12 @@ def custom_error(filename, linenumber, category, message):
125133
return rc
126134

127135

128-
def get_files(paths):
136+
def get_files(paths, exclude_patterns):
137+
excludes = []
138+
for exclude_pattern in exclude_patterns:
139+
excludes.extend(glob.glob(exclude_pattern))
140+
excludes = {os.path.realpath(x) for x in excludes}
141+
129142
files = []
130143
for path in paths:
131144
if os.path.isdir(path):
@@ -145,9 +158,12 @@ def get_files(paths):
145158
fname_low.endswith('.cmake') or
146159
fname_low.endswith('.cmake.in')
147160
):
148-
files.append(os.path.join(dirpath, filename))
161+
fname = os.path.join(dirpath, filename)
162+
if os.path.realpath(fname) not in excludes:
163+
files.append(fname)
149164
if os.path.isfile(path):
150-
files.append(path)
165+
if os.path.realpath(path) not in excludes:
166+
files.append(path)
151167
return [os.path.normpath(f) for f in files]
152168

153169

0 commit comments

Comments
 (0)