Skip to content

Commit d2e96e1

Browse files
[ament_cmake_xmllint] Add file exclude support (#458)
The `ament_cmake_xmllit` respects the `AMENT_LINT_AUTO_FILE_EXCLUDE` variable.
1 parent 0e5bf76 commit d2e96e1

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

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_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)