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

Skip to content

Commit 179a1dc

Browse files
committed
helper: repo creation functions now handle errors on windows during os.remove by changing the mode to 777 and delete the file again. Otherwise the whole operation would fail on read-only files. Why is windows as it is ? Why does it do that to me ?
1 parent b372fdd commit 179a1dc

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

test/testlib/helper.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,18 @@ def wait(self):
6363

6464
poll = wait
6565

66-
66+
67+
def _rmtree_onerror(osremove, fullpath, exec_info):
68+
"""
69+
Handle the case on windows that read-only files cannot be deleted by
70+
os.remove by setting it to mode 777, then retry deletion.
71+
"""
72+
if os.name != 'nt' or osremove is not os.remove:
73+
raise
74+
75+
os.chmod(fullpath, 0777)
76+
os.remove(fullpath)
77+
6778
def with_bare_rw_repo(func):
6879
"""
6980
Decorator providing a specially made read-write repository to the test case
@@ -83,7 +94,7 @@ def bare_repo_creator(self):
8394
return func(self, rw_repo)
8495
finally:
8596
rw_repo.git.clear_cache()
86-
shutil.rmtree(repo_dir)
97+
shutil.rmtree(repo_dir, onerror=_rmtree_onerror)
8798
# END cleanup
8899
# END bare repo creator
89100
bare_repo_creator.__name__ = func.__name__
@@ -109,7 +120,7 @@ def repo_creator(self):
109120
return func(self, rw_repo)
110121
finally:
111122
rw_repo.git.clear_cache()
112-
shutil.rmtree(repo_dir)
123+
shutil.rmtree(repo_dir, onerror=_rmtree_onerror)
113124
# END cleanup
114125
# END rw repo creator
115126
repo_creator.__name__ = func.__name__
@@ -186,8 +197,8 @@ def remote_repo_creator(self):
186197
finally:
187198
rw_repo.git.clear_cache()
188199
rw_remote_repo.git.clear_cache()
189-
shutil.rmtree(repo_dir)
190-
shutil.rmtree(remote_repo_dir)
200+
shutil.rmtree(repo_dir, onerror=_rmtree_onerror)
201+
shutil.rmtree(remote_repo_dir, onerror=_rmtree_onerror)
191202
# END cleanup
192203
# END bare repo creator
193204
remote_repo_creator.__name__ = func.__name__

0 commit comments

Comments
 (0)