Clean up directories on failure. Closes #58.

This commit is contained in:
Anthony Sottile
2014-04-03 22:54:27 -07:00
parent 443b62d56a
commit bcb00726a1
8 changed files with 103 additions and 38 deletions

View File

@@ -3,6 +3,7 @@ import contextlib
from pre_commit.languages import helpers
from pre_commit.languages import python
from pre_commit.prefixed_command_runner import CalledProcessError
from pre_commit.util import clean_path_on_failure
NODE_ENV = 'node_env'
@@ -30,22 +31,30 @@ def install_environment(repo_cmd_runner):
if repo_cmd_runner.exists(NODE_ENV):
return
repo_cmd_runner.run(['virtualenv', '{{prefix}}{0}'.format(python.PY_ENV)])
with clean_path_on_failure(repo_cmd_runner.path(python.PY_ENV)):
repo_cmd_runner.run(
['virtualenv', '{{prefix}}{0}'.format(python.PY_ENV)],
)
with python.in_env(repo_cmd_runner) as python_env:
python_env.run('pip install nodeenv')
with python.in_env(repo_cmd_runner) as python_env:
python_env.run('pip install nodeenv')
# Try and use the system level node executable first
try:
python_env.run('nodeenv -n system {{prefix}}{0}'.format(NODE_ENV))
except CalledProcessError:
# TODO: log failure here
# cleanup
# TODO: local.path(NODE_ENV).delete()
python_env.run('nodeenv --jobs 4 {{prefix}}{0}'.format(NODE_ENV))
with clean_path_on_failure(repo_cmd_runner.path(NODE_ENV)):
# Try and use the system level node executable first
try:
python_env.run(
'nodeenv -n system {{prefix}}{0}'.format(NODE_ENV),
)
except CalledProcessError:
# TODO: log failure here
# cleanup
# TODO: local.path(NODE_ENV).delete()
python_env.run(
'nodeenv --jobs 4 {{prefix}}{0}'.format(NODE_ENV),
)
with in_env(repo_cmd_runner) as node_env:
node_env.run('cd {prefix} && npm install -g')
with in_env(repo_cmd_runner) as node_env:
node_env.run('cd {prefix} && npm install -g')
def run_hook(repo_cmd_runner, hook, file_args):

View File

@@ -2,6 +2,7 @@
import contextlib
from pre_commit.languages import helpers
from pre_commit.util import clean_path_on_failure
PY_ENV = 'py_env'
@@ -25,9 +26,10 @@ def install_environment(repo_cmd_runner):
return
# Install a virtualenv
repo_cmd_runner.run(['virtualenv', '{{prefix}}{0}'.format(PY_ENV)])
with in_env(repo_cmd_runner) as env:
env.run('cd {prefix} && pip install .')
with clean_path_on_failure(repo_cmd_runner.path(PY_ENV)):
repo_cmd_runner.run(['virtualenv', '{{prefix}}{0}'.format(PY_ENV)])
with in_env(repo_cmd_runner) as env:
env.run('cd {prefix} && pip install .')
def run_hook(repo_cmd_runner, hook, file_args):

View File

@@ -2,6 +2,7 @@
import contextlib
from pre_commit.languages import helpers
from pre_commit.util import clean_path_on_failure
RVM_ENV = 'rvm_env'
@@ -23,9 +24,10 @@ def install_environment(repo_cmd_runner):
if repo_cmd_runner.exists(RVM_ENV):
return
repo_cmd_runner.run(['__rvm-env.sh', '{{prefix}}{0}'.format(RVM_ENV)])
with in_env(repo_cmd_runner) as env:
env.run('cd {prefix} && bundle install')
with clean_path_on_failure(repo_cmd_runner.path(RVM_ENV)):
repo_cmd_runner.run(['__rvm-env.sh', '{{prefix}}{0}'.format(RVM_ENV)])
with in_env(repo_cmd_runner) as env:
env.run('cd {prefix} && bundle install')
def run_hook(repo_cmd_runner, hook, file_args):

View File

@@ -9,6 +9,7 @@ from pre_commit.languages.all import languages
from pre_commit.ordereddict import OrderedDict
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.util import cached_property
from pre_commit.util import clean_path_on_failure
class Repository(object):
@@ -62,9 +63,10 @@ class Repository(object):
# Project already exists, no reason to re-create it
return
local['git']['clone', '--no-checkout', self.repo_url, self.sha]()
with self.in_checkout():
local['git']['checkout', self.sha]()
with clean_path_on_failure(unicode(local.path(self.sha))):
local['git']['clone', '--no-checkout', self.repo_url, self.sha]()
with self.in_checkout():
local['git']['checkout', self.sha]()
def require_installed(self, cmd_runner):
if self.__installed:

View File

@@ -1,6 +1,9 @@
import contextlib
import functools
import os
import os.path
import shutil
import sys
@@ -49,3 +52,14 @@ def entry(func):
argv = sys.argv[1:]
return func(argv)
return wrapper
@contextlib.contextmanager
def clean_path_on_failure(path):
"""Cleans up the directory on an exceptional failure."""
try:
yield
except BaseException:
if os.path.exists(path):
shutil.rmtree(path)
raise