Skip to content

Commit

Permalink
Deal with read-only files when removing directories
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
mpeterv committed Jun 11, 2016
1 parent 3eadfc5 commit fa0a6f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 14 additions & 3 deletions hererocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import platform
import re
import shutil
import stat
import string
import subprocess
import sys
Expand Down Expand Up @@ -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/"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions test/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit fa0a6f6

Please sign in to comment.