Skip to content

Commit 2785653

Browse files
authored
remove similar folder names when (re)install tool(chains)
1 parent 6fdcd03 commit 2785653

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

platform.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import fnmatch
1516
import os
1617
import contextlib
1718
import json
@@ -117,6 +118,25 @@ def safe_remove_directory(path: str) -> bool:
117118
return True
118119

119120

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+
120140
@safe_file_operation
121141
def safe_copy_file(src: str, dst: str) -> bool:
122142
"""Safely copy files with error handling."""
@@ -297,9 +317,19 @@ def _handle_existing_tool(
297317
logger.debug(f"Tool {tool_name} found with correct version")
298318
return True
299319

300-
# Wrong version, reinstall
320+
# Wrong version, reinstall - remove similar paths too
301321
logger.info(f"Reinstalling {tool_name} due to version mismatch")
322+
323+
# Remove the main tool directory
302324
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+
303333
return self.install_tool(tool_name, retry_count + 1)
304334

305335
def _configure_arduino_framework(self, frameworks: List[str]) -> None:

0 commit comments

Comments
 (0)