|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +import fnmatch |
15 | 16 | import os
|
16 | 17 | import contextlib
|
17 | 18 | import json
|
@@ -117,6 +118,25 @@ def safe_remove_directory(path: str) -> bool:
|
117 | 118 | return True
|
118 | 119 |
|
119 | 120 |
|
| 121 | +@safe_file_operation |
| 122 | +def safe_remove_directory_pattern(base_path: str, pattern: str) -> bool: |
| 123 | + """Safely remove directories matching a pattern with error handling.""" |
| 124 | + if not os.path.exists(base_path): |
| 125 | + return True |
| 126 | + |
| 127 | + try: |
| 128 | + # Find all directories matching the pattern in the base directory |
| 129 | + for item in os.listdir(base_path): |
| 130 | + item_path = os.path.join(base_path, item) |
| 131 | + if os.path.isdir(item_path) and fnmatch.fnmatch(item, pattern): |
| 132 | + shutil.rmtree(item_path) |
| 133 | + logger.debug(f"Directory removed: {item_path}") |
| 134 | + except OSError as e: |
| 135 | + logger.error(f"Error removing directories with pattern {pattern}: {e}") |
| 136 | + return False |
| 137 | + return True |
| 138 | + |
| 139 | + |
120 | 140 | @safe_file_operation
|
121 | 141 | def safe_copy_file(src: str, dst: str) -> bool:
|
122 | 142 | """Safely copy files with error handling."""
|
@@ -297,9 +317,19 @@ def _handle_existing_tool(
|
297 | 317 | logger.debug(f"Tool {tool_name} found with correct version")
|
298 | 318 | return True
|
299 | 319 |
|
300 |
| - # Wrong version, reinstall |
| 320 | + # Wrong version, reinstall - remove similar paths too |
301 | 321 | logger.info(f"Reinstalling {tool_name} due to version mismatch")
|
| 322 | + |
| 323 | + # Remove the main tool directory |
302 | 324 | safe_remove_directory(paths['tool_path'])
|
| 325 | + |
| 326 | + # Also remove similar directories with version suffixes (e.g., xtensa.12232) |
| 327 | + tool_base_name = os.path.basename(paths['tool_path']) |
| 328 | + packages_dir = os.path.dirname(paths['tool_path']) |
| 329 | + |
| 330 | + # Remove directories matching pattern like "toolname.*" |
| 331 | + safe_remove_directory_pattern(packages_dir, f"{tool_base_name}.*") |
| 332 | + |
303 | 333 | return self.install_tool(tool_name, retry_count + 1)
|
304 | 334 |
|
305 | 335 | def _configure_arduino_framework(self, frameworks: List[str]) -> None:
|
|
0 commit comments