From 179a1dcc65366a70369917d87d00b558a6d0c04d Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 5 Nov 2009 22:24:01 +0100 Subject: 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 ? --- test/testlib/helper.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'test/testlib/helper.py') diff --git a/test/testlib/helper.py b/test/testlib/helper.py index e9a15d5c..25b183a3 100644 --- a/test/testlib/helper.py +++ b/test/testlib/helper.py @@ -63,7 +63,18 @@ class ListProcessAdapter(object): poll = wait - + +def _rmtree_onerror(osremove, fullpath, exec_info): + """ + Handle the case on windows that read-only files cannot be deleted by + os.remove by setting it to mode 777, then retry deletion. + """ + if os.name != 'nt' or osremove is not os.remove: + raise + + os.chmod(fullpath, 0777) + os.remove(fullpath) + def with_bare_rw_repo(func): """ Decorator providing a specially made read-write repository to the test case @@ -83,7 +94,7 @@ def with_bare_rw_repo(func): return func(self, rw_repo) finally: rw_repo.git.clear_cache() - shutil.rmtree(repo_dir) + shutil.rmtree(repo_dir, onerror=_rmtree_onerror) # END cleanup # END bare repo creator bare_repo_creator.__name__ = func.__name__ @@ -109,7 +120,7 @@ def with_rw_repo(working_tree_ref): return func(self, rw_repo) finally: rw_repo.git.clear_cache() - shutil.rmtree(repo_dir) + shutil.rmtree(repo_dir, onerror=_rmtree_onerror) # END cleanup # END rw repo creator repo_creator.__name__ = func.__name__ @@ -186,8 +197,8 @@ def with_rw_and_rw_remote_repo(working_tree_ref): finally: rw_repo.git.clear_cache() rw_remote_repo.git.clear_cache() - shutil.rmtree(repo_dir) - shutil.rmtree(remote_repo_dir) + shutil.rmtree(repo_dir, onerror=_rmtree_onerror) + shutil.rmtree(remote_repo_dir, onerror=_rmtree_onerror) # END cleanup # END bare repo creator remote_repo_creator.__name__ = func.__name__ -- cgit v1.2.1