Skip to content

Commit

Permalink
Drop support for Python prior to 3.6 (#149)
Browse files Browse the repository at this point in the history
Most notably, switch to f-strings.
  • Loading branch information
cottsay authored Nov 22, 2024
1 parent 60aa79c commit 5ff4c14
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 59 deletions.
30 changes: 7 additions & 23 deletions colcon_cmake/package_identification/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
from pathlib import Path
import re
import sys

from colcon_core.dependency_descriptor import DependencyDescriptor
from colcon_core.logging import colcon_logger
Expand Down Expand Up @@ -40,9 +39,8 @@ def identify(self, metadata): # noqa: D102
lines = cmakelists_txt.read_text(errors='replace').splitlines()
if 'catkin_workspace()' in lines:
logger.warning(
"Ignoring '{cmakelists_txt}' since it seems to be a "
"toplevel CMake file generated by 'catkin_make'"
.format_map(locals()))
f"Ignoring '{cmakelists_txt}' since it seems to be a "
"toplevel CMake file generated by 'catkin_make'")
return

metadata.type = 'cmake'
Expand Down Expand Up @@ -153,7 +151,7 @@ def extract_project_name(content):
# extract project name
match = re.search(
# case insensitive function name
_get_case_insensitive_pattern('project') +
'(?i:project)'
# optional white space
r'\s*'
# open parenthesis
Expand Down Expand Up @@ -213,7 +211,7 @@ def extract_find_package_calls(content, *, function_name='find_package'):
"""
matches = re.findall(
# case insensitive function name
_get_case_insensitive_pattern(function_name) +
f'(?i:{function_name})'
# optional white space
r'\s*'
# open parenthesis
Expand All @@ -237,9 +235,9 @@ def extract_find_package_calls(content, *, function_name='find_package'):


def _extract_pkg_config_calls(content):
pattern1 = _get_case_insensitive_pattern('pkg_check_modules')
pattern2 = _get_case_insensitive_pattern('pkg_search_module')
function_names_pattern = '(?:{pattern1}|{pattern2})'.format_map(locals())
pattern1 = '(?i:pkg_check_modules)'
pattern2 = '(?i:pkg_search_module)'
function_names_pattern = f'(?:{pattern1}|{pattern2})'
matches = re.findall(
# case insensitive function names
function_names_pattern +
Expand Down Expand Up @@ -272,17 +270,3 @@ def _extract_pkg_config_calls(content):
module = module[:module.index(char)]
names.add(module)
return names


def _get_case_insensitive_pattern(value):
# non-capturing case insensitive pattern for a string literal
if sys.version_info[:2] < (3, 6):
# match each character separately with arbitrary case
pattern = ''
for char in value:
if char.lower() != char.upper():
pattern += '[' + char.lower() + char.upper() + ']'
else:
pattern += char
return pattern
return '(?i:{value})'.format_map(locals())
5 changes: 2 additions & 3 deletions colcon_cmake/task/cmake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ async def has_target(path, target):
install_project_file = get_project_file(path, 'INSTALL')
return install_project_file is not None
assert False, \
"'has_target' not implemented for CMake generator '{generator}'" \
.format_map(locals())
f"'has_target' not implemented for CMake generator '{generator}'"


async def get_makefile_targets(path):
Expand Down Expand Up @@ -247,7 +246,7 @@ def get_variable_from_cmake_cache(path, var, *, default=None):
lines = _get_cmake_cache_lines(path)
if lines is None:
return default
line_prefix = '{var}:'.format_map(locals())
line_prefix = f'{var}:'
for line in lines:
if line.startswith(line_prefix):
try:
Expand Down
27 changes: 10 additions & 17 deletions colcon_cmake/task/cmake/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ async def build( # noqa: D102
pkg = self.context.pkg
args = self.context.args

logger.info(
"Building CMake package in '{args.path}'".format_map(locals()))
logger.info(f"Building CMake package in '{args.path}'")

try:
env = await get_command_environment(
Expand All @@ -95,10 +94,8 @@ async def build( # noqa: D102
if project_name is None:
# if not the CMake code hasn't called project() and can't be built
logger.warning(
"Could not build CMake package '{pkg.name}' because the "
"CMake cache has no 'CMAKE_PROJECT_NAME' variable"
.format_map(locals())
)
f"Could not build CMake package '{pkg.name}' because the "
"CMake cache has no 'CMAKE_PROJECT_NAME' variable")
return

rc = await self._build(
Expand All @@ -114,8 +111,8 @@ async def build( # noqa: D102
return completed.returncode
else:
logger.warning(
"Could not run installation step for package '{pkg.name}' "
"because it has no 'install' target".format_map(locals()))
'Could not run installation step for package '
f"'{pkg.name}' because it has no 'install' target")

if not skip_hook_creation:
create_environment_scripts(
Expand Down Expand Up @@ -161,9 +158,7 @@ async def _reconfigure(self, args, env):
'14.0': 'Visual Studio 14 2015',
}
if vsv not in supported_vsv:
raise RuntimeError(
"Unknown / unsupported VS version '{vsv}'"
.format_map(locals()))
raise RuntimeError(f"Unknown / unsupported VS version '{vsv}'")
cmake_args += ['-G', supported_vsv[vsv]]
# choose 'x64' on VS 14 and 15 if not specified explicitly
# since otherwise 'Win32' is the default for those
Expand Down Expand Up @@ -196,9 +191,7 @@ def _get_last_cmake_args(self, build_base):
return ast.literal_eval(content)
except SyntaxError as e: # noqa: F841
logger.error(
"Failed to parse previous --cmake-args from '{path}': {e}"
.format_map(locals())
)
f"Failed to parse previous --cmake-args from '{path}': {e}")
return None

def _store_cmake_args(self, build_base, cmake_args):
Expand Down Expand Up @@ -237,7 +230,7 @@ async def _build(self, args, env, *, additional_targets=None):
if args.cmake_target_skip_unavailable:
if not await has_target(args.build_base, target):
continue
self.progress("build target '{target}'".format_map(locals()))
self.progress(f"build target '{target}'")
cmd += ['--target', target]
if i == 0 and args.cmake_clean_first:
cmd += ['--clean-first']
Expand Down Expand Up @@ -317,8 +310,8 @@ def _get_make_arguments(self, env):
# the number of cores can't be determined
return []
return [
'-j{jobs}'.format_map(locals()),
'-l{jobs}'.format_map(locals()),
f'-j{jobs}',
f'-l{jobs}',
]

async def _install(self, args, env):
Expand Down
10 changes: 4 additions & 6 deletions colcon_cmake/task/cmake/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ async def test(self, *, additional_hooks=None): # noqa: D102
pkg = self.context.pkg
args = self.context.args

logger.info(
"Testing CMake package in '{args.path}'".format_map(locals()))
logger.info(f"Testing CMake package in '{args.path}'")

assert os.path.exists(args.build_base), \
'Has this package been built before?'
Expand All @@ -64,8 +63,7 @@ async def test(self, *, additional_hooks=None): # noqa: D102
if line.startswith(' '):
break
else:
logger.log(
5, "No ctests found in '{args.path}'".format_map(locals()))
logger.log(5, f"No ctests found in '{args.path}'")
return

# CTest arguments
Expand Down Expand Up @@ -133,8 +131,8 @@ async def test(self, *, additional_hooks=None): # noqa: D102
latest_xml_path = tag_file.parent / latest_xml_dir / 'Test.xml'
if not latest_xml_path.exists():
logger.warning(
"Skipping '{tag_file}': could not find latest XML file "
"'{latest_xml_path}'".format_map(locals()))
f"Skipping '{tag_file}': could not find latest XML file "
f"'{latest_xml_path}'")
return

dst = Path(args.test_result_base) / 'Testing' / latest_xml_dir
Expand Down
20 changes: 10 additions & 10 deletions colcon_cmake/test_result/ctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def get_test_results( # noqa: D102
latest_xml_path = tag_file.parent / latest_xml_dir / 'Test.xml'
if not latest_xml_path.exists():
logger.warning(
"Skipping '{tag_file}': could not find latest XML file "
"'{latest_xml_path}'".format_map(locals()))
f"Skipping '{tag_file}': could not find latest XML file "
f"'{latest_xml_path}'")
continue

# parse the XML file
Expand All @@ -54,21 +54,21 @@ def get_test_results( # noqa: D102
# check if the root tag looks like a CTest file
if root.tag != 'Site':
logger.warning(
"Skipping '{latest_xml_path}': the root tag is not 'Site'"
.format_map(locals()))
f"Skipping '{latest_xml_path}': the root tag is not "
"'Site'")
continue

# look for a single 'Testing' child tag
children = list(root)
if len(children) != 1:
logger.warning(
"Skipping '{latest_xml_path}': 'Site' tag is expected to "
'"have exactly one child'.format_map(locals()))
f"Skipping '{latest_xml_path}': 'Site' tag is expected to "
'"have exactly one child')
continue
if children[0].tag != 'Testing':
logger.warning(
"Skipping '{latest_xml_path}': the child tag is not "
"'Testing'".format_map(locals()))
f"Skipping '{latest_xml_path}': the child tag is not "
"'Testing'")
continue

if files is not None:
Expand All @@ -86,8 +86,8 @@ def get_test_results( # noqa: D102
status = child.attrib['Status']
except KeyError:
logger.warning(
"Skipping '{latest_xml_path}': a 'test' tag lacks a "
"'Status' attribute".format_map(locals()))
f"Skipping '{latest_xml_path}': a 'test' tag lacks a "
"'Status' attribute")
break

if status == 'failed':
Expand Down

0 comments on commit 5ff4c14

Please sign in to comment.