mirror of
https://github.com/espressif/esp-idf.git
synced 2024-09-19 14:26:01 -04:00
test(build_system): extract file helpers, add 'bin_files_differ'
- Move file-related functions bin_file_contains and file_contains from idf_utils.py and existing functions from editing.py into a new file file_utils.py - Add a function 'bin_files_differ' to compare binary files
This commit is contained in:
parent
7eaa185e07
commit
d6edcba3b6
@ -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
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
from .build_constants import ALL_ARTIFACTS, APP_BINS, BOOTLOADER_BINS, JSON_METADATA, PARTITION_BIN
|
from .build_constants import ALL_ARTIFACTS
|
||||||
from .editing import append_to_file, replace_in_file
|
from .build_constants import APP_BINS
|
||||||
from .idf_utils import (EXT_IDF_PATH, EnvDict, IdfPyFunc, bin_file_contains, file_contains, find_python,
|
from .build_constants import BOOTLOADER_BINS
|
||||||
get_idf_build_env, run_cmake, run_cmake_and_build, run_idf_py)
|
from .build_constants import JSON_METADATA
|
||||||
from .snapshot import Snapshot, get_snapshot
|
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__ = [
|
__all__ = [
|
||||||
'append_to_file', 'replace_in_file',
|
'append_to_file', 'replace_in_file',
|
||||||
'get_idf_build_env', 'run_idf_py', 'EXT_IDF_PATH', 'EnvDict', 'IdfPyFunc',
|
'get_idf_build_env', 'run_idf_py', 'EXT_IDF_PATH', 'EnvDict', 'IdfPyFunc',
|
||||||
'Snapshot', 'get_snapshot', 'run_cmake', 'APP_BINS', 'BOOTLOADER_BINS',
|
'Snapshot', 'get_snapshot', 'run_cmake', 'APP_BINS', 'BOOTLOADER_BINS',
|
||||||
'PARTITION_BIN', 'JSON_METADATA', 'ALL_ARTIFACTS',
|
'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'
|
||||||
]
|
]
|
||||||
|
@ -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)
|
|
@ -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
|
@ -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
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import typing
|
import typing
|
||||||
from pathlib import Path, WindowsPath
|
from pathlib import Path
|
||||||
from typing import Pattern, Union
|
from typing import Union
|
||||||
|
|
||||||
try:
|
try:
|
||||||
EXT_IDF_PATH = os.environ['IDF_PATH'] # type: str
|
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(*cmake_args, env=env)
|
||||||
run_cmake('--build', '.')
|
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
|
|
||||||
|
Loading…
Reference in New Issue
Block a user