#
# SPDX-FileCopyrightText: <text>Copyright 2026 Arm Limited and/or its
# affiliates <open-source-office@arm.com></text>
#
# SPDX-License-Identifier: MIT
import shlex
from typing import Union
from tests.utils.common_utils import CommonUtils
[docs]
logger = CommonUtils.get_logger(__name__)
[docs]
def write_file(
platform_base_obj,
path: str,
value: Union[str, int],
timeout: int = DEFAULT_TIMEOUT,
):
"""
Write a value to a file using shell redirection.
:param platform_base_obj: Platform base object providing console access.
:param path: Path to the file to write to.
:param value: Value to write to the file.
:param timeout: Command timeout in seconds.
:returns: output of the command execution
"""
# Use sh for redirection; assumes tests run as root
port = platform_base_obj.default_console
cmd = f"echo {shlex.quote(str(value))} > {shlex.quote(path)}"
return platform_base_obj.mgr.run_cmd(port, cmd, timeout)
[docs]
def read_file(
platform_base_obj, path: str, timeout: int = DEFAULT_TIMEOUT
) -> str:
"""
Read the contents of a file using the ``cat`` command.
:param platform_base_obj: Platform base object providing console access.
:param path: Path to the file to read from.
:param timeout: Command timeout in seconds.
:returns: Contents of the file
"""
port = platform_base_obj.default_console
return platform_base_obj.mgr.run_cmd(
port, f"cat {shlex.quote(path)}", timeout
)
[docs]
def write_to_port(
mgr,
port,
path: str,
value: Union[str, int],
):
"""
Write value to file over a specific port.
:param mgr: Manager object to run command
:param port: Console port to use
:param path: File path to write to
:param value: Value to write
:returns: output of the command execution
"""
timeout = DEFAULT_TIMEOUT
# Use sh for redirection; assumes tests run as root
cmd = f"echo {shlex.quote(str(value))} > {shlex.quote(path)}"
return mgr.run_cmd(port, cmd, timeout)
[docs]
def read_file_from_port(mgr, port, path: str, timeout: int = 120) -> str:
"""
Read file content using cat command over a specific port.
:param mgr: Manager object to run command
:param port: Console port to use
:param path: File path to read from
:param timeout: Command timeout in seconds
:returns: Contents of the file
"""
return mgr.run_cmd(port, f"cat {shlex.quote(path)}", timeout)
[docs]
def check_if_file_exists(mgr, port, path: str) -> bool:
"""
Check if a file exists on the target.
:param mgr: Manager object to run command
:param port: Console port to use
:param path: File path to read from
:returns: True if file exists, False otherwise
"""
check = (
f"if [ -e {shlex.quote(path)} ]; then echo present; "
"else echo missing; fi"
)
out = mgr.run_cmd(port, check)
return out.splitlines()[-1].strip() == "present"
[docs]
def read_int(mgr, port, path: str) -> int:
"""
Read integer value from file using cat command.
:param mgr: Manager object to run command
:param port: Console port to use
:param path: File path to read from
:returns: Integer value read from the file
"""
return int(read_file_from_port(mgr, port, path).strip())