Skip to content

Commit 2927017

Browse files
authored
remove leftovers in folder packages
1 parent 9bf8b3f commit 2927017

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

platform.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
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
18-
import requests
1919
import subprocess
2020
import sys
2121
import shutil
@@ -107,6 +107,20 @@ def safe_remove_directory(path: str) -> bool:
107107
return True
108108

109109

110+
@safe_file_operation
111+
def safe_remove_directory_pattern(base_path: str, pattern: str) -> bool:
112+
"""Safely remove directories matching a pattern with error handling."""
113+
if not os.path.exists(base_path):
114+
return True
115+
# Find all directories matching the pattern in the base directory
116+
for item in os.listdir(base_path):
117+
item_path = os.path.join(base_path, item)
118+
if os.path.isdir(item_path) and fnmatch.fnmatch(item, pattern):
119+
shutil.rmtree(item_path)
120+
logger.debug(f"Directory removed: {item_path}")
121+
return True
122+
123+
110124
@safe_file_operation
111125
def safe_copy_file(src: str, dst: str) -> bool:
112126
"""Safely copy files with error handling."""
@@ -138,6 +152,17 @@ def _get_tool_paths(self, tool_name: str) -> Dict[str, str]:
138152
"""Get centralized path calculation for tools with caching."""
139153
if tool_name not in self._tools_cache:
140154
tool_path = os.path.join(self.packages_dir, tool_name)
155+
# Remove all directories containing '@' in their name
156+
try:
157+
for item in os.listdir(self.packages_dir):
158+
if '@' in item and item.startswith(tool_name):
159+
item_path = os.path.join(self.packages_dir, item)
160+
if os.path.isdir(item_path):
161+
safe_remove_directory(item_path)
162+
logger.debug(f"Removed directory with '@' in name: {item_path}")
163+
except OSError as e:
164+
logger.error(f"Error scanning packages directory for '@' directories: {e}")
165+
141166
self._tools_cache[tool_name] = {
142167
'tool_path': tool_path,
143168
'package_path': os.path.join(tool_path, "package.json"),
@@ -287,9 +312,19 @@ def _handle_existing_tool(
287312
logger.debug(f"Tool {tool_name} found with correct version")
288313
return True
289314

290-
# Wrong version, reinstall
315+
# Wrong version, reinstall - remove similar paths too
291316
logger.info(f"Reinstalling {tool_name} due to version mismatch")
317+
318+
tool_base_name = os.path.basename(paths['tool_path'])
319+
packages_dir = os.path.dirname(paths['tool_path'])
320+
321+
# Remove similar directories with version suffixes FIRST (e.g., xtensa@src, xtensa.12232)
322+
safe_remove_directory_pattern(packages_dir, f"{tool_base_name}@*")
323+
safe_remove_directory_pattern(packages_dir, f"{tool_base_name}.*")
324+
325+
# Then remove the main tool directory (if it still exists)
292326
safe_remove_directory(paths['tool_path'])
327+
293328
return self.install_tool(tool_name, retry_count + 1)
294329

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

0 commit comments

Comments
 (0)