Skip to content

Commit

Permalink
On Windows, try harder to remove files in long_path_rmtree
Browse files Browse the repository at this point in the history
  • Loading branch information
pv committed Jun 25, 2017
1 parent c7ef898 commit 9c1a382
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions asv/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,11 +964,23 @@ def long_path(path):
return path
return "\\\\?\\" + os.path.abspath(path)

def _remove_readonly(func, path, exc_info):
"""Clear the readonly bit and reattempt the removal;
Windows rmtree doesn't do this by default"""
os.chmod(path, stat.S_IWRITE)
func(path)
def _remove_readonly(func, path, exc_info, wait_flag):
"""Try harder to remove files on Windows"""

if not wait_flag[0]:
# Wait a bit before trying again, but only once
wait_flag[0] = True
time.sleep(1)

if exc_info[1].errno == errno.EACCES:
# Clear any read-only bits
os.chmod(path, stat.S_IRWXU | stat.S_IRWXG)

try:
func(path)
except:
# Reraise original error
six.reraise(*exc_info)

def long_path_open(filename, *a, **kw):
return open(long_path(filename), *a, **kw)
Expand All @@ -977,7 +989,8 @@ def long_path_rmtree(path, ignore_errors=False):
if ignore_errors:
onerror = None
else:
onerror = _remove_readonly
wait_flag = [False]
onerror = lambda f, p, e: _remove_readonly(f, p, e, wait_flag)
shutil.rmtree(long_path(path),
ignore_errors=ignore_errors,
onerror=onerror)
Expand Down

0 comments on commit 9c1a382

Please sign in to comment.