Skip to content

Agent Zero AI framework

directory deletion fix

frdeld7eef4d92f4c
diff --git a/python/helpers/files.py b/python/helpers/files.py
index a8011bf8f..826d5a4c0 100644
--- a/python/helpers/files.py
+++ b/python/helpers/files.py
@@ -71,7 +71,7 @@ def read_file_base64(_relative_path, _backup_dirs=None):
# read binary content and encode to base64
with open(absolute_path, "rb") as f:
- return base64.b64encode(f.read()).decode('utf-8')
+ return base64.b64encode(f.read()).decode("utf-8")
def replace_placeholders_text(_content: str, **kwargs):
@@ -214,16 +214,30 @@ def write_file_base64(relative_path: str, content: str):
f.write(data)
-def delete_file(relative_path: str):
- abs_path = get_abs_path(relative_path)
- if os.path.exists(abs_path):
- os.remove(abs_path)
-
-
def delete_dir(relative_path: str):
+ # ensure deletion of directory without propagating errors
abs_path = get_abs_path(relative_path)
if os.path.exists(abs_path):
- shutil.rmtree(abs_path)
+ # first try with ignore_errors=True which is the safest option
+ shutil.rmtree(abs_path, ignore_errors=True)
+
+ # if directory still exists, try more aggressive methods
+ if os.path.exists(abs_path):
+ try:
+ # try to change permissions and delete again
+ for root, dirs, files in os.walk(abs_path, topdown=False):
+ for name in files:
+ file_path = os.path.join(root, name)
+ os.chmod(file_path, 0o777)
+ for name in dirs:
+ dir_path = os.path.join(root, name)
+ os.chmod(dir_path, 0o777)
+
+ # try again after changing permissions
+ shutil.rmtree(abs_path, ignore_errors=True)
+ except:
+ # suppress all errors - we're ensuring no errors propagate
+ pass
def list_files(relative_path: str, filter: str = "*"):
@@ -252,6 +266,7 @@ def get_base_dir():
base_dir = os.path.dirname(os.path.abspath(os.path.join(__file__, "../../")))
return base_dir
+
def is_in_base_dir(path: str):
# check if the given path is within the base directory
base_dir = get_base_dir()
@@ -261,7 +276,11 @@ def is_in_base_dir(path: str):
return os.path.commonpath([abs_path, base_dir]) == base_dir
-def get_subdirectories(relative_path: str, include: str | list[str] = "*", exclude: str | list[str] | None = None):
+def get_subdirectories(
+ relative_path: str,
+ include: str | list[str] = "*",
+ exclude: str | list[str] | None = None,
+):
abs_path = get_abs_path(relative_path)
if not os.path.exists(abs_path):
return []
@@ -297,7 +316,9 @@ def move_file(relative_path: str, new_path: str):
os.makedirs(os.path.dirname(new_abs_path), exist_ok=True)
os.rename(abs_path, new_abs_path)
-def safe_file_name(filename:str)-> str:
+
+def safe_file_name(filename: str) -> str:
# Replace any character that's not alphanumeric, dash, underscore, or dot with underscore
import re
- return re.sub(r'[^a-zA-Z0-9-._]', '_', filename)
\ No newline at end of file
+
+ return re.sub(r"[^a-zA-Z0-9-._]", "_", filename)