Skip to content

Commit

Permalink
Remove read-only files upon cleanup (#2501)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
robgom and pre-commit-ci[bot] committed Sep 17, 2022
1 parent 2e0def4 commit f233d53
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -107,6 +107,7 @@ Pierre-Jean Campigotto
Pierre-Luc Tessier Gagné
Prakhar Gurunani
Rahul Bangar
Robert Gomulka
Ronald Evers
Ronny Pfannschmidt
Ryuichi Ohori
Expand Down
1 change: 1 addition & 0 deletions docs/changelog/2498.bugfix.rst
@@ -0,0 +1 @@
Remove read-only files in ``ensure_empty_dir``.
18 changes: 17 additions & 1 deletion src/tox/util/path.py
@@ -1,10 +1,26 @@
import errno
import os
import shutil
import stat

from tox import reporter


def ensure_empty_dir(path):
if path.check():
reporter.info(" removing {}".format(path))
shutil.rmtree(str(path), ignore_errors=True)
shutil.rmtree(str(path), onerror=_remove_readonly)
path.ensure(dir=1)


def _remove_readonly(func, path, exc_info):
"""Clear the readonly bit and reattempt the removal."""
if isinstance(exc_info[1], OSError):
if exc_info[1].errno == errno.EACCES:
try:
os.chmod(path, stat.S_IWRITE)
func(path)
except Exception:
# when second attempt fails, ignore the problem
# to maintain some level of backward compatibility
pass
19 changes: 19 additions & 0 deletions tests/integration/test_path_utils_removal.py
@@ -0,0 +1,19 @@
import os
from stat import S_IREAD

from tox.util.path import ensure_empty_dir


def test_remove_read_only(tmpdir):
nested_dir = tmpdir / "nested_dir"
nested_dir.mkdir()

# create read-only file
read_only_file = nested_dir / "tmpfile.txt"
with open(str(read_only_file), "w"):
pass
os.chmod(str(read_only_file), S_IREAD)

ensure_empty_dir(nested_dir)

assert not os.listdir(str(nested_dir))

0 comments on commit f233d53

Please sign in to comment.