Browse Source

chmod rm when rm fails (#4755)

Khaleel Al-Adhami 3 months ago
parent
commit
88f9424df7
1 changed files with 16 additions and 1 deletions
  1. 16 1
      reflex/utils/path_ops.py

+ 16 - 1
reflex/utils/path_ops.py

@@ -6,6 +6,7 @@ import json
 import os
 import os
 import re
 import re
 import shutil
 import shutil
+import stat
 from pathlib import Path
 from pathlib import Path
 
 
 from reflex import constants
 from reflex import constants
@@ -15,6 +16,19 @@ from reflex.config import environment, get_config
 join = os.linesep.join
 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):
 def rm(path: str | Path):
     """Remove a file or directory.
     """Remove a file or directory.
 
 
@@ -23,7 +37,8 @@ def rm(path: str | Path):
     """
     """
     path = Path(path)
     path = Path(path)
     if path.is_dir():
     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():
     elif path.is_file():
         path.unlink()
         path.unlink()