diff --git a/tools/idf_py_actions/core_ext.py b/tools/idf_py_actions/core_ext.py index 9e29f86bf0..ee7ca00bf5 100644 --- a/tools/idf_py_actions/core_ext.py +++ b/tools/idf_py_actions/core_ext.py @@ -18,8 +18,8 @@ from click.core import Context from idf_py_actions.constants import GENERATORS, PREVIEW_TARGETS, SUPPORTED_TARGETS, URL_TO_DOC from idf_py_actions.errors import FatalError from idf_py_actions.global_options import global_options -from idf_py_actions.tools import (PropertyDict, TargetChoice, ensure_build_directory, get_target, idf_version, - merge_action_lists, print_hints, run_target) +from idf_py_actions.tools import (PropertyDict, TargetChoice, ensure_build_directory, generate_hints, get_target, + idf_version, merge_action_lists, run_target, yellow_print) def action_extensions(base_actions: Dict, project_path: str) -> Any: @@ -42,7 +42,8 @@ def action_extensions(base_actions: Dict, project_path: str) -> Any: """ def tool_error_handler(e: int, stdout: str, stderr: str) -> None: - print_hints(stdout, stderr) + for hint in generate_hints(stdout, stderr): + yellow_print(hint) ensure_build_directory(args, ctx.info_name) run_target('all', args, force_progression=GENERATORS[args.generator].get('force_progression', False), diff --git a/tools/idf_py_actions/tools.py b/tools/idf_py_actions/tools.py index 5ffba967f0..4e0eb2581e 100644 --- a/tools/idf_py_actions/tools.py +++ b/tools/idf_py_actions/tools.py @@ -8,7 +8,7 @@ import sys from asyncio.subprocess import Process from io import open from types import FunctionType -from typing import Any, Dict, List, Match, Optional, TextIO, Tuple, Union +from typing import Any, Dict, Generator, List, Match, Optional, TextIO, Tuple, Union import click import yaml @@ -107,7 +107,7 @@ def debug_print_idf_version() -> None: print_warning(f'ESP-IDF {idf_version() or "version unknown"}') -def print_hints(*filenames: str) -> None: +def generate_hints(*filenames: str) -> Generator: """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: hints = yaml.safe_load(file) @@ -140,14 +140,13 @@ def print_hints(*filenames: str) -> None: sys.exit(1) if hint_list: for message in hint_list: - yellow_print('HINT:', message) + yield ' '.join(['HINT:', message]) elif match: extra_info = ', '.join(match.groups()) if hint.get('match_to_output', '') else '' try: - yellow_print(' '.join(['HINT:', hint['hint'].format(extra_info)])) - except KeyError as e: - red_print('Argument {} missing in {}. Check hints.yml file.'.format(e, hint)) - sys.exit(1) + yield ' '.join(['HINT:', hint['hint'].format(extra_info)]) + except KeyError: + raise KeyError("Argument 'hint' missing in {}. Check hints.yml file.".format(hint)) def fit_text_in_terminal(out: str) -> str: @@ -210,7 +209,8 @@ class RunTool: return if stderr_output_file and stdout_output_file: - print_hints(stderr_output_file, stdout_output_file) + for hint in generate_hints(stderr_output_file, stdout_output_file): + yellow_print(hint) raise FatalError('{} failed with exit code {}, output of the command is in the {} and {}'.format(self.tool_name, process.returncode, stderr_output_file, stdout_output_file))