From 278565308a859376d645f0716ecb8a0171977d22 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Sun, 6 Jul 2025 13:13:53 +0200 Subject: [PATCH 1/5] remove similar folder names when (re)install tool(chains) --- platform.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/platform.py b/platform.py index 97ac53e03..42dd9cf51 100644 --- a/platform.py +++ b/platform.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import fnmatch import os import contextlib import json @@ -117,6 +118,25 @@ def safe_remove_directory(path: str) -> bool: return True +@safe_file_operation +def safe_remove_directory_pattern(base_path: str, pattern: str) -> bool: + """Safely remove directories matching a pattern with error handling.""" + if not os.path.exists(base_path): + return True + + try: + # Find all directories matching the pattern in the base directory + for item in os.listdir(base_path): + item_path = os.path.join(base_path, item) + if os.path.isdir(item_path) and fnmatch.fnmatch(item, pattern): + shutil.rmtree(item_path) + logger.debug(f"Directory removed: {item_path}") + except OSError as e: + logger.error(f"Error removing directories with pattern {pattern}: {e}") + return False + return True + + @safe_file_operation def safe_copy_file(src: str, dst: str) -> bool: """Safely copy files with error handling.""" @@ -297,9 +317,19 @@ def _handle_existing_tool( logger.debug(f"Tool {tool_name} found with correct version") return True - # Wrong version, reinstall + # Wrong version, reinstall - remove similar paths too logger.info(f"Reinstalling {tool_name} due to version mismatch") + + # Remove the main tool directory safe_remove_directory(paths['tool_path']) + + # Also remove similar directories with version suffixes (e.g., xtensa.12232) + tool_base_name = os.path.basename(paths['tool_path']) + packages_dir = os.path.dirname(paths['tool_path']) + + # Remove directories matching pattern like "toolname.*" + safe_remove_directory_pattern(packages_dir, f"{tool_base_name}.*") + return self.install_tool(tool_name, retry_count + 1) def _configure_arduino_framework(self, frameworks: List[str]) -> None: From a28328761467140e8489553184612f0413dbb674 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Sun, 6 Jul 2025 14:01:24 +0200 Subject: [PATCH 2/5] Fix order of deleting folders --- platform.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/platform.py b/platform.py index 42dd9cf51..dbf31f09c 100644 --- a/platform.py +++ b/platform.py @@ -320,16 +320,15 @@ def _handle_existing_tool( # Wrong version, reinstall - remove similar paths too logger.info(f"Reinstalling {tool_name} due to version mismatch") - # Remove the main tool directory - safe_remove_directory(paths['tool_path']) - - # Also remove similar directories with version suffixes (e.g., xtensa.12232) tool_base_name = os.path.basename(paths['tool_path']) packages_dir = os.path.dirname(paths['tool_path']) - # Remove directories matching pattern like "toolname.*" + # Remove similar directories with version suffixes FIRST (e.g., xtensa.12232) safe_remove_directory_pattern(packages_dir, f"{tool_base_name}.*") + # Then remove the main tool directory (if it still exists) + safe_remove_directory(paths['tool_path']) + return self.install_tool(tool_name, retry_count + 1) def _configure_arduino_framework(self, frameworks: List[str]) -> None: From be561a6354616f16e276c56bbd49cf6328a61580 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Sun, 6 Jul 2025 14:41:38 +0200 Subject: [PATCH 3/5] remove left overs folders with "@" in name --- platform.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/platform.py b/platform.py index dbf31f09c..d222c25a4 100644 --- a/platform.py +++ b/platform.py @@ -323,7 +323,8 @@ def _handle_existing_tool( tool_base_name = os.path.basename(paths['tool_path']) packages_dir = os.path.dirname(paths['tool_path']) - # Remove similar directories with version suffixes FIRST (e.g., xtensa.12232) + # Remove similar directories with version suffixes FIRST (e.g., xtensa@src, xtensa.12232) + safe_remove_directory_pattern(packages_dir, f"{tool_base_name}@*") safe_remove_directory_pattern(packages_dir, f"{tool_base_name}.*") # Then remove the main tool directory (if it still exists) From c594d419e5edad0416ffbd0b3cde59d66cb68fe1 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Sun, 6 Jul 2025 15:03:11 +0200 Subject: [PATCH 4/5] Delete regarding tool folders which contain `@`before installing tool --- platform.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/platform.py b/platform.py index d222c25a4..1b4023165 100644 --- a/platform.py +++ b/platform.py @@ -168,6 +168,17 @@ def _get_tool_paths(self, tool_name: str) -> Dict[str, str]: """Get centralized path calculation for tools with caching.""" if tool_name not in self._tools_cache: tool_path = os.path.join(self.packages_dir, tool_name) + # Remove all directories containing '@' in their name + try: + for item in os.listdir(self.packages_dir): + if '@' in item and item.startswith(tool_name): + item_path = os.path.join(self.packages_dir, item) + if os.path.isdir(item_path): + safe_remove_directory(item_path) + logger.debug(f"Removed directory with '@' in name: {item_path}") + except OSError as e: + logger.error(f"Error scanning packages directory for '@' directories: {e}") + self._tools_cache[tool_name] = { 'tool_path': tool_path, 'package_path': os.path.join(tool_path, "package.json"), From cb92315bfeeba5ca272e8df5895dc3594de50f6e Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Sun, 6 Jul 2025 15:59:14 +0200 Subject: [PATCH 5/5] remove redundant logging --- platform.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/platform.py b/platform.py index 1b4023165..73509af3c 100644 --- a/platform.py +++ b/platform.py @@ -123,17 +123,12 @@ def safe_remove_directory_pattern(base_path: str, pattern: str) -> bool: """Safely remove directories matching a pattern with error handling.""" if not os.path.exists(base_path): return True - - try: - # Find all directories matching the pattern in the base directory - for item in os.listdir(base_path): - item_path = os.path.join(base_path, item) - if os.path.isdir(item_path) and fnmatch.fnmatch(item, pattern): - shutil.rmtree(item_path) - logger.debug(f"Directory removed: {item_path}") - except OSError as e: - logger.error(f"Error removing directories with pattern {pattern}: {e}") - return False + # Find all directories matching the pattern in the base directory + for item in os.listdir(base_path): + item_path = os.path.join(base_path, item) + if os.path.isdir(item_path) and fnmatch.fnmatch(item, pattern): + shutil.rmtree(item_path) + logger.debug(f"Directory removed: {item_path}") return True