From 1d5621bca2355b53b437234ba5fc786a1b784c9f Mon Sep 17 00:00:00 2001 From: Jakub Kocka Date: Wed, 5 Jun 2024 15:28:20 +0200 Subject: [PATCH] fix(tools): Avoid crashing when Git is used to acquire IDF version Closes https://github.com/espressif/esp-idf/issues/13345 --- tools/idf_py_actions/tools.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tools/idf_py_actions/tools.py b/tools/idf_py_actions/tools.py index 629442d2a7..15840ff984 100644 --- a/tools/idf_py_actions/tools.py +++ b/tools/idf_py_actions/tools.py @@ -80,6 +80,7 @@ def executable_exists(args: List) -> bool: def _idf_version_from_cmake() -> Optional[str]: + """Acquires version of ESP-IDF from version.cmake""" version_path = os.path.join(os.environ['IDF_PATH'], 'tools/cmake/version.cmake') regex = re.compile(r'^\s*set\s*\(\s*IDF_VERSION_([A-Z]{5})\s+(\d+)') ver = {} @@ -113,7 +114,7 @@ def idf_version() -> Optional[str]: '--work-tree=%s' % os.environ['IDF_PATH'], 'describe', '--tags', '--dirty', '--match', 'v*.*', ]).decode('utf-8', 'ignore').strip() - except (subprocess.CalledProcessError, UnicodeError): + except Exception: # if failed, then try to parse cmake.version file sys.stderr.write('WARNING: Git version unavailable, reading from source\n') version = _idf_version_from_cmake() @@ -148,7 +149,7 @@ def get_default_serial_port() -> Any: # 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: +def print_warning(message: str, stream: Optional[TextIO]=None) -> None: if not SHELL_COMPLETE_RUN: print(message, file=stream or sys.stderr) @@ -277,8 +278,9 @@ def fit_text_in_terminal(out: str) -> str: class RunTool: - def __init__(self, tool_name: str, args: List, cwd: str, env: Dict=None, custom_error_handler: FunctionType=None, build_dir: str=None, - hints: bool=True, force_progression: bool=False, interactive: bool=False, convert_output: bool=False) -> None: + def __init__(self, tool_name: str, args: List, cwd: str, env: Optional[Dict]=None, custom_error_handler: Optional[FunctionType]=None, + build_dir: Optional[str]=None, hints: bool=True, force_progression: bool=False, interactive: bool=False, convert_output: bool=False + ) -> None: self.tool_name = tool_name self.args = args self.cwd = cwd @@ -471,7 +473,7 @@ def run_tool(*args: Any, **kwargs: Any) -> None: def run_target(target_name: str, args: 'PropertyDict', env: Optional[Dict]=None, - custom_error_handler: FunctionType=None, force_progression: bool=False, interactive: bool=False) -> None: + custom_error_handler: Optional[FunctionType]=None, force_progression: bool=False, interactive: bool=False) -> None: """Run target in build directory.""" if env is None: env = {} @@ -554,7 +556,7 @@ def _detect_cmake_generator(prog_name: str) -> Any: def ensure_build_directory(args: 'PropertyDict', prog_name: str, always_run_cmake: bool=False, - env: Dict=None) -> None: + env: Optional[Dict]=None) -> None: """Check the build directory exists and that cmake has been run there. If this isn't the case, create the build directory (if necessary) and @@ -671,7 +673,7 @@ def merge_action_lists(*action_lists: Dict) -> Dict: return merged_actions -def get_sdkconfig_filename(args: 'PropertyDict', cache_cmdl: Dict=None) -> str: +def get_sdkconfig_filename(args: 'PropertyDict', cache_cmdl: Optional[Dict]=None) -> str: """ Get project's sdkconfig file name. """ @@ -720,7 +722,7 @@ def is_target_supported(project_path: str, supported_targets: List) -> bool: def _check_idf_target(args: 'PropertyDict', prog_name: str, cache: Dict, - cache_cmdl: Dict, env: Dict=None) -> None: + cache_cmdl: Dict, env: Optional[Dict]=None) -> None: """ Cross-check the three settings (sdkconfig, CMakeCache, environment) and if there is mismatch, fail with instructions on how to fix this.