Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion readmeai/preprocessor/directory_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,34 @@ async def remove_directory(path: Path) -> None:
await asyncio.to_thread(shutil.rmtree, path, ignore_errors=True)


def onerror(func, path, exc_info):
"""
Error handler for ``shutil.rmtree``.

Windows may experience an access error when a file is labeled "read
only". In this instance, the function will attempt to add write
permissions and then retry the function. If the error persists or raises
for another reason, the original error gets re-raised.

Usage: ``shutil.rmtree(path, onerror=onerror)``
"""
import stat

# Is the error an access error?
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise


async def remove_hidden_contents(directory: Path) -> None:
"""Remove hidden files and directories from a specified directory."""
for item in directory.iterdir():
if ".github" in item.parts:
continue
if item.name.startswith("."):
if item.is_dir():
shutil.rmtree(item)
shutil.rmtree(item, onerror=onerror)
else:
item.unlink()