From fa0a6f6a53342601442245d9a9418ced081ab671 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 11 Jun 2016 11:47:48 +0300 Subject: [PATCH] Deal with read-only files when removing directories Fixes error on windows when installing from non-default git repo (for some reason a few files in .git are read-only and shutil.rmtree fails to remove them). --- hererocks.py | 17 ++++++++++++++--- test/cli_test.py | 3 +++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/hererocks.py b/hererocks.py index 9467106..edef26b 100755 --- a/hererocks.py +++ b/hererocks.py @@ -11,6 +11,7 @@ import platform import re import shutil +import stat import string import subprocess import sys @@ -177,6 +178,16 @@ def check_existence(path): def copy_dir(src, dst): shutil.copytree(src, dst, ignore=lambda _, __: {".git"}) +def remove_read_only_or_reraise(func, path, exc_info): + if not os.access(path, os.W_OK): + os.chmod(path, stat.S_IWUSR) + func(path) + else: + raise + +def remove_dir(path): + shutil.rmtree(path, onerror=remove_read_only_or_reraise) + clever_http_git_whitelist = [ "http://github.com/", "https://github.com/", "http://bitbucket.com/", "https://bitbucket.com/" @@ -1198,7 +1209,7 @@ def make_install(self): opts.location, "share", "lua", self.major_version, "jit") if os.path.exists(jitlib_path): - shutil.rmtree(jitlib_path) + remove_dir(jitlib_path) copy_dir("jit", jitlib_path) @@ -1489,7 +1500,7 @@ def setup_vs_and_rerun(vs_version, arch): argv_h.write("\r\n".join(sys.argv).encode("UTF-8")) exit_code = subprocess.call([bat_name]) - shutil.rmtree(temp_dir) + remove_dir(temp_dir) sys.exit(exit_code) def setup_vs(target): @@ -1689,7 +1700,7 @@ def main(argv=None): os.chdir(start_dir) - shutil.rmtree(temp_dir) + remove_dir(temp_dir) print("Done.") sys.exit(0) diff --git a/test/cli_test.py b/test/cli_test.py index bf112f5..b670dee 100644 --- a/test/cli_test.py +++ b/test/cli_test.py @@ -61,6 +61,9 @@ def test_install_latest_lua_with_latest_luarocks(self): self.assertHererocksSuccess(["--lua", "latest", "--luarocks", "latest"], ["already installed"]) self.assertHererocksSuccess(["--luarocks", "latest", "--ignore-installed"], ["Fetching", "cached"]) + def test_install_latest_lua_with_luarocks_from_git(self): + self.assertHererocksSuccess(["--lua", "latest", "--luarocks", "https://github.com/mpeterv/luarocks@master"]) + def test_verbose_install_bleeding_edge_luajit_with_latest_luarocks(self): self.assertHererocksSuccess(["--luajit", "@v2.1", "--luarocks", "latest", "--verbose"]) self.assertSuccess(["lua", "-v"], ["LuaJIT 2.1.0"])