fix(idf_tools): Opt for the recommended tool in tools.json rather than the supported one (v5.0)

This commit is contained in:
Aleksei Apaseev
2023-12-08 11:10:06 +08:00
parent 9fe3bf2a3c
commit d17433623d
3 changed files with 151 additions and 56 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python
# coding=utf-8
#
# SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -1568,6 +1568,94 @@ def action_check(args): # type: ignore
raise SystemExit(1)
# The following functions are used in process_tool which is a part of the action_export.
def handle_recommended_version_to_use(
tool,
tool_name,
version_to_use,
prefer_system_hint,
): # type: (IDFTool, str, str, str) -> Tuple[list, dict]
tool_export_paths = tool.get_export_paths(version_to_use)
tool_export_vars = tool.get_export_vars(version_to_use)
if tool.version_in_path and tool.version_in_path not in tool.versions:
info('Not using an unsupported version of tool {} found in PATH: {}.'.format(
tool.name, tool.version_in_path) + prefer_system_hint, f=sys.stderr)
return tool_export_paths, tool_export_vars
def handle_supported_or_deprecated_version(tool, tool_name): # type: (IDFTool, str) -> None
version_obj: IDFToolVersion = tool.versions[tool.version_in_path] # type: ignore
if version_obj.status == IDFToolVersion.STATUS_SUPPORTED:
info('Using a supported version of tool {} found in PATH: {}.'.format(tool_name, tool.version_in_path),
f=sys.stderr)
info('However the recommended version is {}.'.format(tool.get_recommended_version()),
f=sys.stderr)
elif version_obj.status == IDFToolVersion.STATUS_DEPRECATED:
warn('using a deprecated version of tool {} found in PATH: {}'.format(tool_name, tool.version_in_path))
def handle_missing_versions(
tool,
tool_name,
install_cmd,
prefer_system_hint
): # type: (IDFTool, str, str, str) -> None
fatal('tool {} has no installed versions. Please run \'{}\' to install it.'.format(
tool.name, install_cmd))
if tool.version_in_path and tool.version_in_path not in tool.versions:
info('An unsupported version of tool {} was found in PATH: {}. '.format(tool_name, tool.version_in_path) +
prefer_system_hint, f=sys.stderr)
def process_tool(
tool,
tool_name,
args,
install_cmd,
prefer_system_hint
): # type: (IDFTool, str, argparse.Namespace, str, str) -> Tuple[list, dict, bool]
tool_found: bool = True
tool_export_paths: List[str] = []
tool_export_vars: Dict[str, str] = {}
tool.find_installed_versions()
recommended_version_to_use = tool.get_preferred_installed_version()
if not tool.is_executable and recommended_version_to_use:
tool_export_vars = tool.get_export_vars(recommended_version_to_use)
return tool_export_paths, tool_export_vars, tool_found
if recommended_version_to_use and not args.prefer_system:
tool_export_paths, tool_export_vars = handle_recommended_version_to_use(
tool, tool_name, recommended_version_to_use, prefer_system_hint
)
return tool_export_paths, tool_export_vars, tool_found
if tool.version_in_path:
if tool.version_in_path not in tool.versions:
# unsupported version
if args.prefer_system: # type: ignore
warn('using an unsupported version of tool {} found in PATH: {}'.format(
tool.name, tool.version_in_path))
return tool_export_paths, tool_export_vars, tool_found
else:
# unsupported version in path
pass
else:
# supported/deprecated version in PATH, use it
handle_supported_or_deprecated_version(tool, tool_name)
return tool_export_paths, tool_export_vars, tool_found
if not tool.versions_installed:
if tool.get_install_type() == IDFTool.INSTALL_ALWAYS:
handle_missing_versions(tool, tool_name, install_cmd, prefer_system_hint)
tool_found = False
# If a tool found, but it is optional and does not have versions installed, use whatever is in PATH.
return tool_export_paths, tool_export_vars, tool_found
return tool_export_paths, tool_export_vars, tool_found
def action_export(args): # type: ignore
if args.deactivate and different_idf_detected():
deactivate_statement(args)
@@ -1587,58 +1675,10 @@ def action_export(args): # type: ignore
for name, tool in tools_info.items():
if tool.get_install_type() == IDFTool.INSTALL_NEVER:
continue
tool.find_installed_versions()
version_to_use = tool.get_preferred_installed_version()
if not tool.is_executable and version_to_use:
tool_export_vars = tool.get_export_vars(version_to_use)
export_vars = {**export_vars, **tool_export_vars}
continue
if tool.version_in_path:
if tool.version_in_path not in tool.versions:
# unsupported version
if args.prefer_system: # type: ignore
warn('using an unsupported version of tool {} found in PATH: {}'.format(
tool.name, tool.version_in_path))
continue
else:
# unsupported version in path
pass
else:
# supported/deprecated version in PATH, use it
version_obj = tool.versions[tool.version_in_path]
if version_obj.status == IDFToolVersion.STATUS_SUPPORTED:
info('Using a supported version of tool {} found in PATH: {}.'.format(name, tool.version_in_path),
f=sys.stderr)
info('However the recommended version is {}.'.format(tool.get_recommended_version()),
f=sys.stderr)
elif version_obj.status == IDFToolVersion.STATUS_DEPRECATED:
warn('using a deprecated version of tool {} found in PATH: {}'.format(name, tool.version_in_path))
continue
if not tool.versions_installed:
if tool.get_install_type() == IDFTool.INSTALL_ALWAYS:
all_tools_found = False
fatal('tool {} has no installed versions. Please run \'{}\' to install it.'.format(
tool.name, install_cmd))
if tool.version_in_path and tool.version_in_path not in tool.versions:
info('An unsupported version of tool {} was found in PATH: {}. '.format(name, tool.version_in_path) +
prefer_system_hint, f=sys.stderr)
continue
else:
# tool is optional, and does not have versions installed
# use whatever is available in PATH
continue
if tool.version_in_path and tool.version_in_path not in tool.versions:
info('Not using an unsupported version of tool {} found in PATH: {}.'.format(
tool.name, tool.version_in_path) + prefer_system_hint, f=sys.stderr)
export_paths = tool.get_export_paths(version_to_use)
if export_paths:
paths_to_export += export_paths
tool_export_vars = tool.get_export_vars(version_to_use)
tool_export_paths, tool_export_vars, tool_found = process_tool(tool, name, args, install_cmd, prefer_system_hint)
if not tool_found:
all_tools_found = False
paths_to_export += tool_export_paths
export_vars = {**export_vars, **tool_export_vars}
current_path = os.getenv('PATH')