Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove read-only files upon cleanup (#2501)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
2e0def4
commit f233d53
Showing
4 changed files
with
38 additions
and
1 deletion.
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
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 @@ | ||
Remove read-only files in ``ensure_empty_dir``. |
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 |
---|---|---|
@@ -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 |
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,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)) |