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

feat: adds error handler to change file permissions if delete fails #2474

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
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):
bitwise-constructs marked this conversation as resolved.
Show resolved Hide resolved
"""
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)
bitwise-constructs marked this conversation as resolved.
Show resolved Hide resolved

# 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
Loading