Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
πŸ›  Fix Windows PermissionError in shutil.rmtree (#65)
Browse files Browse the repository at this point in the history
* πŸ›  Fix Windows PermissionError in shutil.rmtree

* 🎨 Apply code-style with pre-commit
  • Loading branch information
kadirnar authored Nov 1, 2023
1 parent a816e89 commit a27f132
Showing 1 changed file with 18 additions and 2 deletions.
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

0 comments on commit a27f132

Please sign in to comment.