diff --git a/tools/test_build_system/test_build_system_helpers/__init__.py b/tools/test_build_system/test_build_system_helpers/__init__.py index 9f7616cdc0..57c6493e74 100644 --- a/tools/test_build_system/test_build_system_helpers/__init__.py +++ b/tools/test_build_system/test_build_system_helpers/__init__.py @@ -1,15 +1,30 @@ -# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 -from .build_constants import ALL_ARTIFACTS, APP_BINS, BOOTLOADER_BINS, JSON_METADATA, PARTITION_BIN -from .editing import append_to_file, replace_in_file -from .idf_utils import (EXT_IDF_PATH, EnvDict, IdfPyFunc, bin_file_contains, file_contains, find_python, - get_idf_build_env, run_cmake, run_cmake_and_build, run_idf_py) -from .snapshot import Snapshot, get_snapshot +from .build_constants import ALL_ARTIFACTS +from .build_constants import APP_BINS +from .build_constants import BOOTLOADER_BINS +from .build_constants import JSON_METADATA +from .build_constants import PARTITION_BIN +from .file_utils import append_to_file +from .file_utils import bin_file_contains +from .file_utils import bin_files_differ +from .file_utils import file_contains +from .file_utils import replace_in_file +from .idf_utils import EnvDict +from .idf_utils import EXT_IDF_PATH +from .idf_utils import find_python +from .idf_utils import get_idf_build_env +from .idf_utils import IdfPyFunc +from .idf_utils import run_cmake +from .idf_utils import run_cmake_and_build +from .idf_utils import run_idf_py +from .snapshot import get_snapshot +from .snapshot import Snapshot __all__ = [ 'append_to_file', 'replace_in_file', 'get_idf_build_env', 'run_idf_py', 'EXT_IDF_PATH', 'EnvDict', 'IdfPyFunc', 'Snapshot', 'get_snapshot', 'run_cmake', 'APP_BINS', 'BOOTLOADER_BINS', 'PARTITION_BIN', 'JSON_METADATA', 'ALL_ARTIFACTS', - 'run_cmake_and_build', 'find_python', 'file_contains', 'bin_file_contains' + 'run_cmake_and_build', 'find_python', 'file_contains', 'bin_file_contains', 'bin_files_differ' ] diff --git a/tools/test_build_system/test_build_system_helpers/editing.py b/tools/test_build_system/test_build_system_helpers/editing.py deleted file mode 100644 index 7f21d52822..0000000000 --- a/tools/test_build_system/test_build_system_helpers/editing.py +++ /dev/null @@ -1,17 +0,0 @@ -# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD -# SPDX-License-Identifier: Apache-2.0 -import typing -from pathlib import Path - - -def append_to_file(filename: typing.Union[str, Path], what: str) -> None: - with open(filename, 'a', encoding='utf-8') as f: - f.write(what) - - -def replace_in_file(filename: typing.Union[str, Path], search: str, replace: str) -> None: - with open(filename, 'r', encoding='utf-8') as f: - data = f.read() - result = data.replace(search, replace) - with open(filename, 'w', encoding='utf-8') as f: - f.write(result) diff --git a/tools/test_build_system/test_build_system_helpers/file_utils.py b/tools/test_build_system/test_build_system_helpers/file_utils.py new file mode 100644 index 0000000000..e79a8b62f4 --- /dev/null +++ b/tools/test_build_system/test_build_system_helpers/file_utils.py @@ -0,0 +1,64 @@ +# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 +import re +import typing as t +from pathlib import Path +from pathlib import WindowsPath + + +def append_to_file(filename: t.Union[str, Path], what: str) -> None: + with open(filename, 'a', encoding='utf-8') as f: + f.write(what) + + +def replace_in_file(filename: t.Union[str, Path], search: str, replace: str) -> None: + with open(filename, 'r', encoding='utf-8') as f: + data = f.read() + result = data.replace(search, replace) + with open(filename, 'w', encoding='utf-8') as f: + f.write(result) + + +def file_contains(filename: t.Union[str, Path], what: t.Union[t.Union[str, Path], t.Pattern]) -> bool: + """ + Returns true if file contains required object + :param filename: path to file where lookup is executed + :param what: searched substring or regex object + """ + with open(filename, 'r', encoding='utf-8') as f: + data = f.read() + if isinstance(what, t.Pattern): + return re.search(what, data) is not None + else: + what_str = str(what) + # In case of windows path, try both single-slash `\` and double-slash '\\' paths + if isinstance(what, WindowsPath): + what_double_slash = what_str.replace('\\', '\\\\') + return what_str in data or what_double_slash in data + + return what_str in data + + +def bin_file_contains(filename: t.Union[str, Path], what: bytearray) -> bool: + """ + Returns true if the binary file contains the given string + :param filename: path to file where lookup is executed + :param what: searched bytes + """ + with open(filename, 'rb') as f: + data = f.read() + return data.find(what) != -1 + + +def bin_files_differ(filename1: t.Union[str, Path], filename2: t.Union[str, Path]) -> bool: + """ + Checks if two binary files are different + :param filename1: path to first file + :param filename2: path to second file + :return: True if files have different content, False if the content is the same + """ + with open(filename1, 'rb') as f1: + data1 = f1.read() + with open(filename2, 'rb') as f2: + data2 = f2.read() + return data1 != data2 diff --git a/tools/test_build_system/test_build_system_helpers/idf_utils.py b/tools/test_build_system/test_build_system_helpers/idf_utils.py index bec2df14ea..638789535e 100644 --- a/tools/test_build_system/test_build_system_helpers/idf_utils.py +++ b/tools/test_build_system/test_build_system_helpers/idf_utils.py @@ -1,14 +1,13 @@ -# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 import logging import os -import re import shutil import subprocess import sys import typing -from pathlib import Path, WindowsPath -from typing import Pattern, Union +from pathlib import Path +from typing import Union try: EXT_IDF_PATH = os.environ['IDF_PATH'] # type: str @@ -135,34 +134,3 @@ def run_cmake_and_build(*cmake_args: str, env: typing.Optional[EnvDict] = None) """ run_cmake(*cmake_args, env=env) run_cmake('--build', '.') - - -def file_contains(filename: Union[str, Path], what: Union[Union[str, Path], Pattern]) -> bool: - """ - Returns true if file contains required object - :param filename: path to file where lookup is executed - :param what: searched substring or regex object - """ - with open(filename, 'r', encoding='utf-8') as f: - data = f.read() - if isinstance(what, Pattern): - return re.search(what, data) is not None - else: - what_str = str(what) - # In case of windows path, try both single-slash `\` and double-slash '\\' paths - if isinstance(what, WindowsPath): - what_double_slash = what_str.replace('\\', '\\\\') - return what_str in data or what_double_slash in data - - return what_str in data - - -def bin_file_contains(filename: Union[str, Path], what: bytearray) -> bool: - """ - Returns true if the binary file contains the given string - :param filename: path to file where lookup is executed - :param what: searched bytes - """ - with open(filename, 'rb') as f: - data = f.read() - return data.find(what) != -1