Handle known read-only errors during rmtree. The returned value is used only by our own tests.
(
func: Callable[..., Any] | None,
path: str,
excinfo: BaseException
| tuple[type[BaseException], BaseException, types.TracebackType | None],
*,
start_path: Path,
)
| 70 | |
| 71 | |
| 72 | def on_rm_rf_error( |
| 73 | func: Callable[..., Any] | None, |
| 74 | path: str, |
| 75 | excinfo: BaseException |
| 76 | | tuple[type[BaseException], BaseException, types.TracebackType | None], |
| 77 | *, |
| 78 | start_path: Path, |
| 79 | ) -> bool: |
| 80 | """Handle known read-only errors during rmtree. |
| 81 | |
| 82 | The returned value is used only by our own tests. |
| 83 | """ |
| 84 | if isinstance(excinfo, BaseException): |
| 85 | exc = excinfo |
| 86 | else: |
| 87 | exc = excinfo[1] |
| 88 | |
| 89 | # Another process removed the file in the middle of the "rm_rf" (xdist for example). |
| 90 | # More context: https://github.com/pytest-dev/pytest/issues/5974#issuecomment-543799018 |
| 91 | if isinstance(exc, FileNotFoundError): |
| 92 | return False |
| 93 | |
| 94 | if not isinstance(exc, PermissionError): |
| 95 | warnings.warn( |
| 96 | PytestWarning(f"(rm_rf) error removing {path}\n{type(exc)}: {exc}") |
| 97 | ) |
| 98 | return False |
| 99 | |
| 100 | if func not in (os.rmdir, os.remove, os.unlink): |
| 101 | if func not in (os.open,): |
| 102 | warnings.warn( |
| 103 | PytestWarning( |
| 104 | f"(rm_rf) unknown function {func} when removing {path}:\n{type(exc)}: {exc}" |
| 105 | ) |
| 106 | ) |
| 107 | return False |
| 108 | |
| 109 | # Chmod + retry. |
| 110 | import stat |
| 111 | |
| 112 | def chmod_rw(p: str) -> None: |
| 113 | mode = os.stat(p).st_mode |
| 114 | os.chmod(p, mode | stat.S_IRUSR | stat.S_IWUSR) |
| 115 | |
| 116 | # For files, we need to recursively go upwards in the directories to |
| 117 | # ensure they all are also writable. |
| 118 | p = Path(path) |
| 119 | if p.is_file(): |
| 120 | for parent in p.parents: |
| 121 | chmod_rw(str(parent)) |
| 122 | # Stop when we reach the original path passed to rm_rf. |
| 123 | if parent == start_path: |
| 124 | break |
| 125 | chmod_rw(str(path)) |
| 126 | |
| 127 | func(path) |
| 128 | return True |
| 129 |