chmod rm when rm fails (#4755)

This commit is contained in:
Khaleel Al-Adhami 2025-02-05 10:25:47 -08:00 committed by GitHub
parent 59d8d1eb62
commit 88f9424df7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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()