Skip to content

Commit 0141ebf

Browse files
Merge pull request #1 from clearpathrobotics/ga_jazzy_cmake_lint
[ament_cmake_lint_cmake] Add file exclude support
2 parents 7241e40 + 213fae0 commit 0141ebf

File tree

8 files changed

+65
-14
lines changed

8 files changed

+65
-14
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_cmake_pep257/cmake/ament_cmake_pep257_lint_hook.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@
1515
file(GLOB_RECURSE _python_files FOLLOW_SYMLINKS "*.py")
1616
if(_python_files)
1717
message(STATUS "Added test 'pep257' to check Python code against some of the docstring style conventions in PEP 257")
18-
ament_pep257()
18+
if(DEFINED AMENT_LINT_AUTO_FILE_EXCLUDE)
19+
ament_pep257(EXCLUDE ${AMENT_LINT_AUTO_FILE_EXCLUDE})
20+
else()
21+
ament_pep257()
22+
endif()
1923
endif()

ament_cmake_pep257/cmake/ament_pep257.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
#
1818
# :param TESTNAME: the name of the test, default: "pep257"
1919
# :type TESTNAME: string
20+
# :param EXCLUDE: an optional list of exclude files or directories for cmake pep257 check
21+
# :type EXCLUDE: list
2022
# :param ARGN: the files or directories to check
2123
# :type ARGN: list of strings
2224
#
2325
# @public
2426
#
2527
function(ament_pep257)
26-
cmake_parse_arguments(ARG "" "TESTNAME" "" ${ARGN})
28+
cmake_parse_arguments(ARG "" "TESTNAME" "EXCLUDE" ${ARGN})
2729
if(NOT ARG_TESTNAME)
2830
set(ARG_TESTNAME "pep257")
2931
endif()
@@ -35,6 +37,9 @@ function(ament_pep257)
3537

3638
set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${ARG_TESTNAME}.xunit.xml")
3739
set(cmd "${ament_pep257_BIN}" "--xunit-file" "${result_file}")
40+
if(ARG_EXCLUDE)
41+
list(APPEND cmd "--exclude" "${ARG_EXCLUDE}")
42+
endif()
3843
list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS})
3944

4045
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ament_pep257")

ament_cmake_xmllint/cmake/ament_cmake_xmllint_lint_hook.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
1515
file(GLOB_RECURSE _source_files FOLLOW_SYMLINKS "*.xml")
1616
if(_source_files)
1717
message(STATUS "Added test 'xmllint' to check XML markup files")
18-
ament_xmllint()
18+
19+
if(DEFINED AMENT_LINT_AUTO_FILE_EXCLUDE)
20+
ament_xmllint(EXCLUDE ${AMENT_LINT_AUTO_FILE_EXCLUDE})
21+
else()
22+
ament_xmllint()
23+
endif()
1924
endif()

ament_cmake_xmllint/cmake/ament_xmllint.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
#
1818
# :param TESTNAME: the name of the test, default: "xmllint"
1919
# :type TESTNAME: string
20+
# :param EXCLUDE: an optional list of exclude files or directories for xmllint check
21+
# :type EXCLUDE: list
2022
# :param ARGN: the files or directories to check
2123
# :type ARGN: list of strings
2224
#
2325
# @public
2426
#
2527
function(ament_xmllint)
26-
cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;TESTNAME" "" ${ARGN})
28+
cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;TESTNAME" "EXCLUDE" ${ARGN})
2729
if(NOT ARG_TESTNAME)
2830
set(ARG_TESTNAME "xmllint")
2931
endif()
@@ -35,6 +37,9 @@ function(ament_xmllint)
3537

3638
set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${ARG_TESTNAME}.xunit.xml")
3739
set(cmd "${ament_xmllint_BIN}" "--xunit-file" "${result_file}")
40+
if(ARG_EXCLUDE)
41+
list(APPEND cmd "--exclude" "${ARG_EXCLUDE}")
42+
endif()
3843
list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS})
3944

4045
find_program(xmllint_BIN NAMES "xmllint")

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

ament_xmllint/ament_xmllint/main.py

Lines changed: 11 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 shutil
2021
import subprocess
@@ -46,6 +47,7 @@ def main(argv=sys.argv[1:]):
4647
'--exclude',
4748
nargs='*',
4849
default=[],
50+
dest='excludes',
4951
help='Exclude specific file names and directory names from the check')
5052
# not using a file handle directly
5153
# in order to prevent leaving an empty file when something fails early
@@ -62,7 +64,7 @@ def main(argv=sys.argv[1:]):
6264
if args.xunit_file:
6365
start_time = time.time()
6466

65-
files = get_files(args.paths, args.extensions, args.exclude)
67+
files = get_files(args.paths, args.extensions, args.excludes)
6668
if not files:
6769
print('No files found', file=sys.stderr)
6870
return 1
@@ -158,7 +160,12 @@ def main(argv=sys.argv[1:]):
158160
return rc
159161

160162

161-
def get_files(paths, extensions, excludes=[]):
163+
def get_files(paths, extensions, exclude_patterns):
164+
excludes = []
165+
for exclude_pattern in exclude_patterns:
166+
excludes.extend(glob.glob(exclude_pattern))
167+
excludes = {os.path.realpath(x) for x in excludes}
168+
162169
files = []
163170
for path in paths:
164171
if os.path.isdir(path):
@@ -169,12 +176,12 @@ def get_files(paths, extensions, excludes=[]):
169176
# ignore folder starting with . or _
170177
dirnames[:] = [d for d in dirnames if d[0] not in ['.', '_']]
171178
# ignore excluded folders
172-
dirnames[:] = [d for d in dirnames if d not in excludes]
179+
dirnames[:] = [d for d in dirnames if os.path.realpath(d) not in excludes]
173180
dirnames.sort()
174181

175182
# select files by extension
176183
for filename in sorted(filenames):
177-
if filename in excludes:
184+
if os.path.realpath(os.path.join(dirpath, filename)) in excludes:
178185
continue
179186
_, ext = os.path.splitext(filename)
180187
if ext not in ['.%s' % e for e in extensions]:

0 commit comments

Comments
 (0)