From 2d61c9ca792862b9623584de7abc8a02c1f16ade Mon Sep 17 00:00:00 2001 From: Alexey Lapshin Date: Sat, 10 Sep 2022 00:59:45 +0400 Subject: [PATCH] tools: move cmake executable check into function uses it Before this change idf.py could exit with reason cmake does not exist in PATH even cmake will not be executed by idf.py (e.g., 'idf.py gdb'). --- tools/idf.py | 35 +++-------------------------------- tools/idf_py_actions/tools.py | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/tools/idf.py b/tools/idf.py index 0b91e13ff0..1d05593aed 100755 --- a/tools/idf.py +++ b/tools/idf.py @@ -27,7 +27,7 @@ from collections import Counter, OrderedDict, _OrderedDictKeysView from importlib import import_module from pkgutil import iter_modules from types import FrameType -from typing import Any, Callable, Dict, List, Optional, TextIO, Union +from typing import Any, Callable, Dict, List, Optional, Union # pyc files remain in the filesystem when switching between branches which might raise errors for incompatible # idf.py extensions. Therefore, pyc file generation is turned off: @@ -37,8 +37,8 @@ import python_version_checker # noqa: E402 try: from idf_py_actions.errors import FatalError # noqa: E402 - from idf_py_actions.tools import (PropertyDict, executable_exists, get_target, idf_version, # noqa: E402 - merge_action_lists, realpath) + from idf_py_actions.tools import (PROG, SHELL_COMPLETE_RUN, SHELL_COMPLETE_VAR, PropertyDict, # noqa: E402 + debug_print_idf_version, get_target, merge_action_lists, print_warning, realpath) if os.getenv('IDF_COMPONENT_MANAGER') != '0': from idf_component_manager import idf_extensions except ImportError: @@ -53,23 +53,6 @@ PYTHON = sys.executable # you have to pass env=os.environ explicitly anywhere that we create a process os.environ['PYTHON'] = sys.executable -# Name of the program, normally 'idf.py'. -# Can be overridden from idf.bat using IDF_PY_PROGRAM_NAME -PROG = os.getenv('IDF_PY_PROGRAM_NAME', 'idf.py') - -# environment variable used during click shell completion run -SHELL_COMPLETE_VAR = '_IDF.PY_COMPLETE' - -# was shell completion invoked? -SHELL_COMPLETE_RUN = SHELL_COMPLETE_VAR in os.environ - - -# function prints warning when autocompletion is not being performed -# set argument stream to sys.stderr for errors and exceptions -def print_warning(message: str, stream: TextIO=None) -> None: - if not SHELL_COMPLETE_RUN: - print(message, file=stream or sys.stderr) - def check_environment() -> List: """ @@ -79,10 +62,6 @@ def check_environment() -> List: """ checks_output = [] - if not executable_exists(['cmake', '--version']): - debug_print_idf_version() - raise FatalError("'cmake' must be available on the PATH to use %s" % PROG) - # verify that IDF_PATH env variable is set # find the directory idf.py is in, then the parent directory of this, and assume this is IDF_PATH detected_idf_path = realpath(os.path.join(os.path.dirname(__file__), '..')) @@ -137,14 +116,6 @@ def _safe_relpath(path: str, start: Optional[str]=None) -> str: return os.path.abspath(path) -def debug_print_idf_version() -> None: - version = idf_version() - if version: - print_warning('ESP-IDF %s' % version) - else: - print_warning('ESP-IDF version unknown') - - def init_cli(verbose_output: List=None) -> Any: # Click is imported here to run it after check_environment() import click diff --git a/tools/idf_py_actions/tools.py b/tools/idf_py_actions/tools.py index cfacc7239c..4e8f7ec919 100644 --- a/tools/idf_py_actions/tools.py +++ b/tools/idf_py_actions/tools.py @@ -16,6 +16,16 @@ import yaml from .constants import GENERATORS from .errors import FatalError +# Name of the program, normally 'idf.py'. +# Can be overridden from idf.bat using IDF_PY_PROGRAM_NAME +PROG = os.getenv('IDF_PY_PROGRAM_NAME', 'idf.py') + +# environment variable used during click shell completion run +SHELL_COMPLETE_VAR = '_IDF.PY_COMPLETE' + +# was shell completion invoked? +SHELL_COMPLETE_RUN = SHELL_COMPLETE_VAR in os.environ + def executable_exists(args: List) -> bool: try: @@ -78,6 +88,13 @@ def idf_version() -> Optional[str]: return version +# function prints warning when autocompletion is not being performed +# set argument stream to sys.stderr for errors and exceptions +def print_warning(message: str, stream: TextIO=None) -> None: + if not SHELL_COMPLETE_RUN: + print(message, file=stream or sys.stderr) + + def color_print(message: str, color: str, newline: Optional[str]='\n') -> None: """ Print a message to stderr with colored highlighting """ ansi_normal = '\033[0m' @@ -95,6 +112,10 @@ def red_print(message: str, newline: Optional[str]='\n') -> None: color_print(message, ansi_red, newline) +def debug_print_idf_version() -> None: + print_warning(f'ESP-IDF {idf_version() or "version unknown"}') + + def print_hints(*filenames: str) -> None: """Getting output files and printing hints on how to resolve errors based on the output.""" with open(os.path.join(os.path.dirname(__file__), 'hints.yml'), 'r') as file: @@ -383,6 +404,11 @@ def ensure_build_directory(args: 'PropertyDict', prog_name: str, always_run_cmak the build directory, an error is raised. If the parameter is None, this function will set it to an auto-detected default generator or to the value already configured in the build directory. """ + + if not executable_exists(['cmake', '--version']): + debug_print_idf_version() + raise FatalError(f'"cmake" must be available on the PATH to use {PROG}') + project_dir = args.project_dir # Verify the project directory if not os.path.isdir(project_dir):