Skip to content

Commit

Permalink
fix(tools): Avoid crashing when Git is used to acquire IDF version
Browse files Browse the repository at this point in the history
Closes #13345
  • Loading branch information
jakub-kocka committed Jun 11, 2024
1 parent 2b1a0cc commit 7113031
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions tools/idf_py_actions/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -460,7 +462,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 = {}
Expand Down Expand Up @@ -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,
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
Expand Down Expand Up @@ -660,7 +662,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.
"""
Expand Down Expand Up @@ -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,
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.
Expand Down

0 comments on commit 7113031

Please sign in to comment.