With verbose output enabled, twister logs the cmake arguments as a python array whenever it invokes cmake to generate a build system. This is clunky output for manual reproduction, because it forces the developer to manually join the arguments together for consumption by the shell. As an enhancement for this use case, use the standard library shlex.join() function to produce a string that can be copy/pasted directly into the shell on POSIX platforms. We should use this function instead of str.join, because it correctly handles things like shell quoting for options with spaces in them. This function is not available on Windows, so we fall back on behavior that is similar to the old one there. The main difference on Windows is that the output now includes the path to cmake as well, and not just its arguments. (This is the same on POSIX, and is intended to help debug if multiple cmakes are installed on the same machine.) We are running cmake from a couple of different places, so doing this cleanly requires adding a shared module with the platform abstraction. Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
27 lines
818 B
Python
27 lines
818 B
Python
# Copyright (c) 2022 Nordic Semiconductor ASA
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
'''
|
|
Common code used when logging that is needed by multiple modules.
|
|
'''
|
|
|
|
import platform
|
|
import shlex
|
|
|
|
_WINDOWS = (platform.system() == 'Windows')
|
|
|
|
def log_command(logger, msg, args):
|
|
'''Platform-independent helper for logging subprocess invocations.
|
|
Will log a command string that can be copy/pasted into a POSIX
|
|
shell on POSIX platforms. This is not available on Windows, so
|
|
the entire args array is logged instead.
|
|
|
|
:param logger: logging.Logger to use
|
|
:param msg: message to associate with the command
|
|
:param args: argument list as passed to subprocess module
|
|
'''
|
|
msg = f'{msg}: %s'
|
|
if _WINDOWS:
|
|
logger.debug(msg, str(args))
|
|
else:
|
|
logger.debug(msg, shlex.join(args))
|