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 <simon.chupin@espressif.com>
	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 <frantisek.hrbata@espressif.com>
This commit is contained in:
Frantisek Hrbata 2023-05-17 12:15:00 +02:00
parent b08267f3b1
commit 8eda836de6
2 changed files with 12 additions and 11 deletions

View File

@ -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),

View File

@ -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))