-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract convenience wrapper of rmtree to setuptools._shutil for reuse
- Loading branch information
1 parent
e14cfec
commit d9975c6
Showing
2 changed files
with
44 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Convenience layer on top of stdlib's shutil and os""" | ||
|
||
import os | ||
import stat | ||
from typing import Callable, TypeVar | ||
|
||
from .compat import py311 | ||
|
||
from distutils import log | ||
|
||
try: | ||
from os import chmod | ||
except ImportError: | ||
# Jython compatibility | ||
def chmod(*args: object, **kwargs: object) -> None: # type: ignore[misc] # Mypy reuses the imported definition anyway | ||
pass | ||
|
||
|
||
_T = TypeVar("_T") | ||
|
||
|
||
def attempt_chmod_verbose(path, mode): | ||
log.debug("changing mode of %s to %o", path, mode) | ||
try: | ||
chmod(path, mode) | ||
except OSError as e: | ||
log.debug("chmod failed: %s", e) | ||
|
||
|
||
# Must match shutil._OnExcCallback | ||
def _auto_chmod(func: Callable[..., _T], arg: str, exc: BaseException) -> _T: | ||
"""shutils onexc callback to automatically call chmod for certain functions.""" | ||
# Only retry for scenarios known to have an issue | ||
if func in [os.unlink, os.remove] and os.name == 'nt': | ||
attempt_chmod_verbose(arg, stat.S_IWRITE) | ||
return func(arg) | ||
raise exc | ||
|
||
|
||
def rmtree(path, ignore_errors=False, onexc=_auto_chmod): | ||
return py311.shutil_rmtree(path, ignore_errors, onexc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters