Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28664d6

Browse files
authored
Merge pull request #1043 from pre-commit/rmtree_readonly_dirs
Fix rmtree for readonly directories
2 parents 77947f2 + da44d42 commit 28664d6

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

pre_commit/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,14 @@ def cmd_output(*cmd, **kwargs):
158158

159159
def rmtree(path):
160160
"""On windows, rmtree fails for readonly dirs."""
161-
def handle_remove_readonly(func, path, exc): # pragma: no cover (windows)
161+
def handle_remove_readonly(func, path, exc):
162162
excvalue = exc[1]
163163
if (
164164
func in (os.rmdir, os.remove, os.unlink) and
165165
excvalue.errno == errno.EACCES
166166
):
167-
os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
167+
for p in (path, os.path.dirname(path)):
168+
os.chmod(p, os.stat(p).st_mode | stat.S_IWUSR)
168169
func(path)
169170
else:
170171
raise

tests/util_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from __future__ import unicode_literals
22

33
import os.path
4+
import stat
45

56
import pytest
67

78
from pre_commit.util import CalledProcessError
89
from pre_commit.util import clean_path_on_failure
910
from pre_commit.util import cmd_output
1011
from pre_commit.util import parse_version
12+
from pre_commit.util import rmtree
1113
from pre_commit.util import tmpdir
1214

1315

@@ -90,3 +92,14 @@ def test_parse_version():
9092
assert parse_version('0.0') == parse_version('0.0')
9193
assert parse_version('0.1') > parse_version('0.0')
9294
assert parse_version('2.1') >= parse_version('2')
95+
96+
97+
def test_rmtree_read_only_directories(tmpdir):
98+
"""Simulates the go module tree. See #1042"""
99+
tmpdir.join('x/y/z').ensure_dir().join('a').ensure()
100+
mode = os.stat(str(tmpdir.join('x'))).st_mode
101+
mode_no_w = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
102+
tmpdir.join('x/y/z').chmod(mode_no_w)
103+
tmpdir.join('x/y/z').chmod(mode_no_w)
104+
tmpdir.join('x/y/z').chmod(mode_no_w)
105+
rmtree(str(tmpdir.join('x')))

0 commit comments

Comments
 (0)