1# Copyright (c) 2023 Nordic Semiconductor ASA
2#
3# SPDX-License-Identifier: Apache-2.0
4
5from __future__ import annotations
6
7import logging
8import os.path
9import platform
10import shlex
11
12_WINDOWS = platform.system() == 'Windows'
13
14logger = logging.getLogger(__name__)
15
16
17def log_command(logger: logging.Logger, msg: str, args: list, level: int = logging.DEBUG):
18    """
19    Platform-independent helper for logging subprocess invocations.
20
21    Will log a command string that can be copy/pasted into a POSIX
22    shell on POSIX platforms. This is not available on Windows, so
23    the entire args array is logged instead.
24
25    :param logger: logging.Logger to use
26    :param msg: message to associate with the command
27    :param args: argument list as passed to subprocess module
28    :param level: log level
29    """
30    msg = f'{msg}: %s'
31    if _WINDOWS:
32        logger.log(level, msg, str(args))
33    else:
34        logger.log(level, msg, shlex.join(args))
35
36
37def normalize_filename(filename: str) -> str:
38    filename = os.path.expanduser(os.path.expandvars(filename))
39    filename = os.path.normpath(os.path.abspath(filename))
40    return filename
41