mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-02 11:10:54 +03:00
refactor(tools): Addressed pre-commit findings
This commit is contained in:
@@ -144,8 +144,8 @@ repos:
|
|||||||
files: 'tools/.+|examples/.+|components/.+'
|
files: 'tools/.+|examples/.+|components/.+'
|
||||||
require_serial: true
|
require_serial: true
|
||||||
additional_dependencies:
|
additional_dependencies:
|
||||||
- PyYAML == 5.3.1
|
- PyYAML>=5.3.1
|
||||||
- idf-build-apps~=2.13
|
- idf-build-apps>=2.14
|
||||||
- id: sort-yaml-files
|
- id: sort-yaml-files
|
||||||
name: sort yaml files
|
name: sort yaml files
|
||||||
entry: tools/ci/sort_yaml.py
|
entry: tools/ci/sort_yaml.py
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
|
# SPDX-FileCopyrightText: 2022-2026 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
|
||||||
@@ -7,7 +7,6 @@ import subprocess
|
|||||||
import sys
|
import sys
|
||||||
import typing
|
import typing
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
EXT_IDF_PATH = os.environ['IDF_PATH'] # type: str
|
EXT_IDF_PATH = os.environ['IDF_PATH'] # type: str
|
||||||
@@ -16,7 +15,7 @@ except KeyError:
|
|||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
EnvDict = typing.Dict[str, str]
|
EnvDict = dict[str, str]
|
||||||
IdfPyFunc = typing.Callable[..., subprocess.CompletedProcess]
|
IdfPyFunc = typing.Callable[..., subprocess.CompletedProcess]
|
||||||
|
|
||||||
|
|
||||||
@@ -99,11 +98,12 @@ def get_idf_build_env(idf_path: str) -> EnvDict:
|
|||||||
sys.executable,
|
sys.executable,
|
||||||
os.path.join(idf_path, 'tools', 'idf_tools.py'),
|
os.path.join(idf_path, 'tools', 'idf_tools.py'),
|
||||||
'export',
|
'export',
|
||||||
'--format=key-value'
|
'--format=key-value',
|
||||||
]
|
]
|
||||||
keys_values = subprocess.check_output(cmd, stderr=subprocess.PIPE).decode()
|
keys_values = subprocess.check_output(cmd, stderr=subprocess.PIPE).decode()
|
||||||
idf_tool_py_env = {key: os.path.expandvars(value) for key, value in
|
idf_tool_py_env = {
|
||||||
[line.split('=') for line in keys_values.splitlines()]}
|
key: os.path.expandvars(value) for key, value in [line.split('=') for line in keys_values.splitlines()]
|
||||||
|
}
|
||||||
env_vars = {} # type: EnvDict
|
env_vars = {} # type: EnvDict
|
||||||
env_vars.update(os.environ)
|
env_vars.update(os.environ)
|
||||||
env_vars.update(idf_tool_py_env)
|
env_vars.update(idf_tool_py_env)
|
||||||
@@ -113,13 +113,15 @@ def get_idf_build_env(idf_path: str) -> EnvDict:
|
|||||||
return env_vars
|
return env_vars
|
||||||
|
|
||||||
|
|
||||||
def run_idf_py(*args: str,
|
def run_idf_py(
|
||||||
env: typing.Optional[EnvDict] = None,
|
*args: str,
|
||||||
idf_path: typing.Optional[typing.Union[str,Path]] = None,
|
env: EnvDict | None = None,
|
||||||
workdir: typing.Optional[str] = None,
|
idf_path: str | Path | None = None,
|
||||||
|
workdir: str | None = None,
|
||||||
check: bool = True,
|
check: bool = True,
|
||||||
python: typing.Optional[str] = None,
|
python: str | None = None,
|
||||||
input_str: typing.Optional[str] = None) -> subprocess.CompletedProcess:
|
input_str: str | None = None,
|
||||||
|
) -> subprocess.CompletedProcess:
|
||||||
"""
|
"""
|
||||||
Run idf.py command with given arguments, raise an exception on failure
|
Run idf.py command with given arguments, raise an exception on failure
|
||||||
:param args: arguments to pass to idf.py
|
:param args: arguments to pass to idf.py
|
||||||
@@ -144,27 +146,36 @@ def run_idf_py(*args: str,
|
|||||||
|
|
||||||
cmd = [
|
cmd = [
|
||||||
python,
|
python,
|
||||||
os.path.join(idf_path, 'tools', 'idf.py')
|
os.path.join(idf_path, 'tools', 'idf.py'),
|
||||||
]
|
]
|
||||||
cmd += args # type: ignore
|
cmd += args # type: ignore
|
||||||
logging.debug('running {} in {}'.format(' '.join(cmd), workdir))
|
logging.debug(f'running {" ".join(cmd)} in {workdir}')
|
||||||
try:
|
try:
|
||||||
return subprocess.run(
|
return subprocess.run(
|
||||||
cmd, env=env, cwd=workdir,
|
cmd,
|
||||||
check=check, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
env=env,
|
||||||
text=True, encoding='utf-8', errors='backslashreplace', input=input_str)
|
cwd=workdir,
|
||||||
|
check=check,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
encoding='utf-8',
|
||||||
|
errors='backslashreplace',
|
||||||
|
input=input_str,
|
||||||
|
)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
logging.error('The following idf.py command has failed: {}'.format(' '.join(cmd)))
|
logging.error(f'The following idf.py command has failed: {" ".join(cmd)}')
|
||||||
logging.error('Working directory: {}'.format(workdir))
|
logging.error(f'Working directory: {workdir}')
|
||||||
logging.error('Stdout: {}'.format(_clip_log_output(e.stdout)))
|
logging.error(f'Stdout: {_clip_log_output(e.stdout)}')
|
||||||
logging.error('Stderr: {}'.format(_clip_log_output(e.stderr)))
|
logging.error(f'Stderr: {_clip_log_output(e.stderr)}')
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def run_cmake(*cmake_args: str,
|
def run_cmake(
|
||||||
env: typing.Optional[EnvDict] = None,
|
*cmake_args: str,
|
||||||
|
env: EnvDict | None = None,
|
||||||
check: bool = True,
|
check: bool = True,
|
||||||
workdir: typing.Optional[Union[Path,str]] = None) -> subprocess.CompletedProcess:
|
workdir: Path | str | None = None,
|
||||||
|
) -> subprocess.CompletedProcess:
|
||||||
"""
|
"""
|
||||||
Run cmake command with given arguments, raise an exception on failure
|
Run cmake command with given arguments, raise an exception on failure
|
||||||
:param cmake_args: arguments to pass cmake
|
:param cmake_args: arguments to pass cmake
|
||||||
@@ -178,27 +189,33 @@ def run_cmake(*cmake_args: str,
|
|||||||
if workdir:
|
if workdir:
|
||||||
build_dir = Path(workdir, 'build')
|
build_dir = Path(workdir, 'build')
|
||||||
else:
|
else:
|
||||||
build_dir = (Path(os.getcwd()) / 'build')
|
build_dir = Path(os.getcwd()) / 'build'
|
||||||
|
|
||||||
build_dir.mkdir(parents=True, exist_ok=True)
|
build_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
cmd = ['cmake'] + list(cmake_args)
|
cmd = ['cmake'] + list(cmake_args)
|
||||||
|
|
||||||
logging.debug('running {} in {}'.format(' '.join(cmd), build_dir))
|
logging.debug(f'running {" ".join(cmd)} in {build_dir}')
|
||||||
try:
|
try:
|
||||||
return subprocess.run(
|
return subprocess.run(
|
||||||
cmd, env=env, cwd=build_dir,
|
cmd,
|
||||||
check=check, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
env=env,
|
||||||
text=True, encoding='utf-8', errors='backslashreplace')
|
cwd=build_dir,
|
||||||
|
check=check,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
encoding='utf-8',
|
||||||
|
errors='backslashreplace',
|
||||||
|
)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
logging.error('The following cmake command has failed: {}'.format(' '.join(cmd)))
|
logging.error(f'The following cmake command has failed: {" ".join(cmd)}')
|
||||||
logging.error('Working directory: {}'.format(workdir))
|
logging.error(f'Working directory: {workdir}')
|
||||||
logging.error('Stdout: {}'.format(_clip_log_output(e.stdout)))
|
logging.error(f'Stdout: {_clip_log_output(e.stdout)}')
|
||||||
logging.error('Stderr: {}'.format(_clip_log_output(e.stderr)))
|
logging.error(f'Stderr: {_clip_log_output(e.stderr)}')
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def run_cmake_and_build(*cmake_args: str, env: typing.Optional[EnvDict] = None) -> None:
|
def run_cmake_and_build(*cmake_args: str, env: EnvDict | None = None) -> None:
|
||||||
"""
|
"""
|
||||||
Run cmake command with given arguments and build afterwards, raise an exception on failure
|
Run cmake command with given arguments and build afterwards, raise an exception on failure
|
||||||
:param cmake_args: arguments to pass cmake
|
:param cmake_args: arguments to pass cmake
|
||||||
|
|||||||
Reference in New Issue
Block a user