fix(tools): Avoid crashing when Git is used to acquire IDF version

Closes https://github.com/espressif/esp-idf/issues/13345
This commit is contained in:
Jakub Kocka 2024-06-05 15:28:20 +02:00
parent 2b1a0cc523
commit 71130318df

View File

@ -80,6 +80,7 @@ def executable_exists(args: List) -> bool:
def _idf_version_from_cmake() -> Optional[str]: 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') 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+)') regex = re.compile(r'^\s*set\s*\(\s*IDF_VERSION_([A-Z]{5})\s+(\d+)')
ver = {} ver = {}
@ -113,7 +114,7 @@ def idf_version() -> Optional[str]:
'--work-tree=%s' % os.environ['IDF_PATH'], '--work-tree=%s' % os.environ['IDF_PATH'],
'describe', '--tags', '--dirty', '--match', 'v*.*', 'describe', '--tags', '--dirty', '--match', 'v*.*',
]).decode('utf-8', 'ignore').strip() ]).decode('utf-8', 'ignore').strip()
except (subprocess.CalledProcessError, UnicodeError): except Exception:
# if failed, then try to parse cmake.version file # if failed, then try to parse cmake.version file
sys.stderr.write('WARNING: Git version unavailable, reading from source\n') sys.stderr.write('WARNING: Git version unavailable, reading from source\n')
version = _idf_version_from_cmake() version = _idf_version_from_cmake()
@ -148,7 +149,7 @@ def get_default_serial_port() -> Any:
# function prints warning when autocompletion is not being performed # function prints warning when autocompletion is not being performed
# set argument stream to sys.stderr for errors and exceptions # 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: if not SHELL_COMPLETE_RUN:
print(message, file=stream or sys.stderr) print(message, file=stream or sys.stderr)
@ -277,8 +278,9 @@ def fit_text_in_terminal(out: str) -> str:
class RunTool: class RunTool:
def __init__(self, tool_name: str, args: List, cwd: str, env: Dict=None, custom_error_handler: FunctionType=None, build_dir: str=None, def __init__(self, tool_name: str, args: List, cwd: str, env: Optional[Dict]=None, custom_error_handler: Optional[FunctionType]=None,
hints: bool=True, force_progression: bool=False, interactive: bool=False, convert_output: bool=False) -> 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.tool_name = tool_name
self.args = args self.args = args
self.cwd = cwd self.cwd = cwd
@ -460,7 +462,7 @@ def run_tool(*args: Any, **kwargs: Any) -> None:
def run_target(target_name: str, args: 'PropertyDict', env: Optional[Dict]=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.""" """Run target in build directory."""
if env is None: if env is None:
env = {} env = {}
@ -543,7 +545,7 @@ def _detect_cmake_generator(prog_name: str) -> Any:
def ensure_build_directory(args: 'PropertyDict', prog_name: str, always_run_cmake: bool=False, 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. """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 If this isn't the case, create the build directory (if necessary) and
@ -660,7 +662,7 @@ def merge_action_lists(*action_lists: Dict) -> Dict:
return merged_actions 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. Get project's sdkconfig file name.
""" """
@ -709,7 +711,7 @@ def is_target_supported(project_path: str, supported_targets: List) -> bool:
def _check_idf_target(args: 'PropertyDict', prog_name: str, cache: Dict, 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 Cross-check the three settings (sdkconfig, CMakeCache, environment) and if there is
mismatch, fail with instructions on how to fix this. mismatch, fail with instructions on how to fix this.