Skip to content

Commit

Permalink
feat: adds error handler to change file permissions if delete fails
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwise-constructs committed Jan 21, 2025
1 parent a87fba6 commit c5e1622
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/ape/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __getattr__(name: str):
"get_relative_path",
"in_tempdir",
"path_match",
"remove_readonly",
"run_in_tempdir",
"use_temp_sys_path",
):
Expand Down Expand Up @@ -171,6 +172,7 @@ def __getattr__(name: str):
"parse_gas_table",
"path_match",
"raises_not_implemented",
"remove_readonly",
"returns_array",
"request_with_retry",
"RPCHeaders",
Expand Down
9 changes: 9 additions & 0 deletions src/ape/utils/os.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import re
import stat
import sys
import tarfile
import zipfile
Expand Down Expand Up @@ -372,6 +373,14 @@ def extract_archive(archive_file: Path, destination: Optional[Path] = None):
raise ValueError(f"Unsupported zip format: '{archive_file.suffix}'.")


def remove_readonly(func, path, excinfo):
"""
Error handler for shutil.rmtree that handles removing read-only files.
"""
os.chmod(path, stat.S_IWRITE)
func(path)


class CacheDirectory:
"""
A directory for caching data where each data item is named
Expand Down
9 changes: 6 additions & 3 deletions src/ape_pm/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ape.managers.project import _version_to_options
from ape.utils._github import _GithubClient, github_client
from ape.utils.basemodel import ManagerAccessMixin
from ape.utils.os import clean_path, extract_archive, get_package_path, in_tempdir
from ape.utils.os import clean_path, extract_archive, get_package_path, in_tempdir, remove_readonly


def _fetch_local(src: Path, destination: Path, config_override: Optional[dict] = None):
Expand Down Expand Up @@ -204,8 +204,11 @@ def __repr__(self) -> str:
def fetch(self, destination: Path):
destination.parent.mkdir(exist_ok=True, parents=True)
if ref := self.ref:
# NOTE: destination path should not exist at this point,
# so delete it in case it's left over from a failure.
shutil.rmtree(destination, onerror=remove_readonly)

# Fetch using git-clone approach (by git-reference).
# NOTE: destination path does not exist at this point.
self._fetch_ref(ref, destination)
else:
# Fetch using Version API from GitHub.
Expand All @@ -222,7 +225,7 @@ def fetch(self, destination: Path):
# NOTE: When using ref-from-a-version, ensure
# it didn't create the destination along the way;
# else, the ref is cloned in the wrong spot.
shutil.rmtree(destination, ignore_errors=True)
shutil.rmtree(destination, onerror=remove_readonly)
try:
self._fetch_ref(version, destination)
except Exception:
Expand Down

0 comments on commit c5e1622

Please sign in to comment.