From 88f9424df767e0d07a08e43c0fe3f712ac540fda Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 5 Feb 2025 10:25:47 -0800 Subject: [PATCH] chmod rm when rm fails (#4755) --- reflex/utils/path_ops.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/reflex/utils/path_ops.py b/reflex/utils/path_ops.py index edab085ff..07a541201 100644 --- a/reflex/utils/path_ops.py +++ b/reflex/utils/path_ops.py @@ -6,6 +6,7 @@ import json import os import re import shutil +import stat from pathlib import Path from reflex import constants @@ -15,6 +16,19 @@ from reflex.config import environment, get_config join = os.linesep.join +def chmod_rm(path: Path): + """Remove a file or directory with chmod. + + Args: + path: The path to the file or directory. + """ + path.chmod(stat.S_IWRITE) + if path.is_dir(): + shutil.rmtree(path) + elif path.is_file(): + path.unlink() + + def rm(path: str | Path): """Remove a file or directory. @@ -23,7 +37,8 @@ def rm(path: str | Path): """ path = Path(path) if path.is_dir(): - shutil.rmtree(path) + # In Python 3.12, onerror is deprecated in favor of onexc + shutil.rmtree(path, onerror=lambda _func, _path, _info: chmod_rm(path)) elif path.is_file(): path.unlink()