Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🛠 Fix Windows PermissionError in shutil.rmtree #65

Merged
merged 2 commits into from
Nov 1, 2023
Merged
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
20 changes: 18 additions & 2 deletions autollm/utils/document_reading.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import logging
import os
import shutil
import stat
from pathlib import Path
from typing import List, Optional, Sequence
from typing import Callable, List, Optional, Sequence, Tuple

from llama_index.readers.file.base import SimpleDirectoryReader
from llama_index.schema import Document
Expand Down Expand Up @@ -54,6 +56,20 @@ def read_files_as_documents(
return documents


# From http://stackoverflow.com/a/4829285/548792
def on_rm_error(func: Callable, path: str, exc_info: Tuple):
"""
Error handler for `shutil.rmtree` to handle permission errors.

Parameters:
func (Callable): The function that raised the error.
path (str): The path to the file or directory which couldn't be removed.
exc_info (Tuple): Exception information returned by sys.exc_info().
"""
os.chmod(path, stat.S_IWRITE)
os.unlink(path)


def read_github_repo_as_documents(
git_repo_url: str,
relative_folder_path: Optional[str] = None,
Expand Down Expand Up @@ -89,6 +105,6 @@ def read_github_repo_as_documents(
logger.info(f"Operations complete, deleting temporary directory {temp_dir}..")
finally:
# Delete the temporary directory
shutil.rmtree(temp_dir)
shutil.rmtree(temp_dir, onerror=on_rm_error)

return documents
Loading