From 8eda836de6f0cd6f2d7e5b01a0a91897ee1a6db6 Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Wed, 17 May 2023 12:15:00 +0200 Subject: [PATCH] tools: don't print hints directly in print_hints This is partial backport of following commit, which changes print_hints to generate_hints. commit 92ef2a4c835ac5d6043ee14678ce0c5c941d3c63 Author: simon.chupin Date: Tue Aug 9 15:39:23 2022 +0200 Tools: Add unit tests for idf.py hints Only hunks for core_ext.py and tools.py are picked. It would be possible to use the original print_hints approach, where the hints are directly printed out and not returned, but it seems to make sense to keep it closer to most recent version. It should make further backports easier and it allows to cherry pick the iterative hints approach. Signed-off-by: Frantisek Hrbata --- tools/idf_py_actions/core_ext.py | 7 ++++--- tools/idf_py_actions/tools.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) 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))