Print message when installing repositories.

This commit is contained in:
Anthony Sottile 2014-04-03 23:31:45 -07:00
parent 36ecf23c2e
commit 74363e6ec2
9 changed files with 78 additions and 29 deletions

View file

@ -5,7 +5,10 @@ from pre_commit.languages import ruby
from pre_commit.languages import script from pre_commit.languages import script
from pre_commit.languages import system from pre_commit.languages import system
# A language implements the following two functions in its module: # A language implements the following constant and two functions in its module:
#
# # Use None for no environment
# ENVIRONMENT_DIR = 'foo_env'
# #
# def install_environment(repo_cmd_runner): # def install_environment(repo_cmd_runner):
# """Installs a repository in the given repository. Note that the current # """Installs a repository in the given repository. Note that the current

View file

@ -6,7 +6,7 @@ from pre_commit.prefixed_command_runner import CalledProcessError
from pre_commit.util import clean_path_on_failure from pre_commit.util import clean_path_on_failure
NODE_ENV = 'node_env' ENVIRONMENT_DIR = 'node_env'
class NodeEnv(python.PythonEnv): class NodeEnv(python.PythonEnv):
@ -15,7 +15,7 @@ class NodeEnv(python.PythonEnv):
base = super(NodeEnv, self).env_prefix base = super(NodeEnv, self).env_prefix
return ' '.join([ return ' '.join([
base, base,
'. {{prefix}}{0}/bin/activate &&'.format(NODE_ENV)] '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)]
) )
@ -27,30 +27,26 @@ def in_env(repo_cmd_runner):
def install_environment(repo_cmd_runner): def install_environment(repo_cmd_runner):
assert repo_cmd_runner.exists('package.json') assert repo_cmd_runner.exists('package.json')
# Return immediately if we already have a virtualenv with clean_path_on_failure(repo_cmd_runner.path(python.ENVIRONMENT_DIR)):
if repo_cmd_runner.exists(NODE_ENV):
return
with clean_path_on_failure(repo_cmd_runner.path(python.PY_ENV)):
repo_cmd_runner.run( repo_cmd_runner.run(
['virtualenv', '{{prefix}}{0}'.format(python.PY_ENV)], ['virtualenv', '{{prefix}}{0}'.format(python.ENVIRONMENT_DIR)],
) )
with python.in_env(repo_cmd_runner) as python_env: with python.in_env(repo_cmd_runner) as python_env:
python_env.run('pip install nodeenv') python_env.run('pip install nodeenv')
with clean_path_on_failure(repo_cmd_runner.path(NODE_ENV)): with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
# Try and use the system level node executable first # Try and use the system level node executable first
try: try:
python_env.run( python_env.run(
'nodeenv -n system {{prefix}}{0}'.format(NODE_ENV), 'nodeenv -n system {{prefix}}{0}'.format(ENVIRONMENT_DIR),
) )
except CalledProcessError: except CalledProcessError:
# TODO: log failure here # TODO: log failure here
# cleanup # cleanup
# TODO: local.path(NODE_ENV).delete() # TODO: local.path(ENVIRONMENT_DIR).delete()
python_env.run( python_env.run(
'nodeenv --jobs 4 {{prefix}}{0}'.format(NODE_ENV), 'nodeenv --jobs 4 {{prefix}}{0}'.format(ENVIRONMENT_DIR),
) )
with in_env(repo_cmd_runner) as node_env: with in_env(repo_cmd_runner) as node_env:

View file

@ -5,13 +5,13 @@ from pre_commit.languages import helpers
from pre_commit.util import clean_path_on_failure from pre_commit.util import clean_path_on_failure
PY_ENV = 'py_env' ENVIRONMENT_DIR = 'py_env'
class PythonEnv(helpers.Environment): class PythonEnv(helpers.Environment):
@property @property
def env_prefix(self): def env_prefix(self):
return '. {{prefix}}{0}/bin/activate &&'.format(PY_ENV) return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
@contextlib.contextmanager @contextlib.contextmanager
@ -21,13 +21,10 @@ def in_env(repo_cmd_runner):
def install_environment(repo_cmd_runner): def install_environment(repo_cmd_runner):
assert repo_cmd_runner.exists('setup.py') assert repo_cmd_runner.exists('setup.py')
# Return immediately if we already have a virtualenv
if repo_cmd_runner.exists(PY_ENV):
return
# Install a virtualenv # Install a virtualenv
with clean_path_on_failure(repo_cmd_runner.path(PY_ENV)): with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
repo_cmd_runner.run(['virtualenv', '{{prefix}}{0}'.format(PY_ENV)]) repo_cmd_runner.run(['virtualenv', '{{prefix}}{0}'.format(ENVIRONMENT_DIR)])
with in_env(repo_cmd_runner) as env: with in_env(repo_cmd_runner) as env:
env.run('cd {prefix} && pip install .') env.run('cd {prefix} && pip install .')

View file

@ -5,13 +5,13 @@ from pre_commit.languages import helpers
from pre_commit.util import clean_path_on_failure from pre_commit.util import clean_path_on_failure
RVM_ENV = 'rvm_env' ENVIRONMENT_DIR = 'rvm_env'
class RubyEnv(helpers.Environment): class RubyEnv(helpers.Environment):
@property @property
def env_prefix(self): def env_prefix(self):
return '. {{prefix}}{0}/bin/activate &&'.format(RVM_ENV) return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
@contextlib.contextmanager @contextlib.contextmanager
@ -21,11 +21,11 @@ def in_env(repo_cmd_runner):
def install_environment(repo_cmd_runner): def install_environment(repo_cmd_runner):
# Return immediately if we already have a virtualenv # Return immediately if we already have a virtualenv
if repo_cmd_runner.exists(RVM_ENV): if repo_cmd_runner.exists(ENVIRONMENT_DIR):
return return
with clean_path_on_failure(repo_cmd_runner.path(RVM_ENV)): with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
repo_cmd_runner.run(['__rvm-env.sh', '{{prefix}}{0}'.format(RVM_ENV)]) repo_cmd_runner.run(['__rvm-env.sh', '{{prefix}}{0}'.format(ENVIRONMENT_DIR)])
with in_env(repo_cmd_runner) as env: with in_env(repo_cmd_runner) as env:
env.run('cd {prefix} && bundle install') env.run('cd {prefix} && bundle install')

View file

@ -1,7 +1,9 @@
ENVIRONMENT_DIR = None
def install_environment(repo_cmd_runner): def install_environment(repo_cmd_runner):
"""Installation for script type is a noop.""" """Installation for script type is a noop."""
pass
def run_hook(repo_cmd_runner, hook, file_args): def run_hook(repo_cmd_runner, hook, file_args):

View file

@ -1,7 +1,9 @@
ENVIRONMENT_DIR = None
def install_environment(repo_cmd_runner): def install_environment(repo_cmd_runner):
"""Installation for system type is a noop.""" """Installation for system type is a noop."""
pass
def run_hook(repo_cmd_runner, hook, file_args): def run_hook(repo_cmd_runner, hook, file_args):

View file

@ -1,4 +1,6 @@
from __future__ import print_function
import contextlib import contextlib
from plumbum import local from plumbum import local
@ -63,6 +65,10 @@ class Repository(object):
# Project already exists, no reason to re-create it # Project already exists, no reason to re-create it
return return
# Checking out environment for the first time
print('Installing environment for {0}.'.format(self.repo_url))
print('Once installed this environment will be reused.')
print('This may take a few minutes...')
with clean_path_on_failure(unicode(local.path(self.sha))): with clean_path_on_failure(unicode(local.path(self.sha))):
local['git']['clone', '--no-checkout', self.repo_url, self.sha]() local['git']['clone', '--no-checkout', self.repo_url, self.sha]()
with self.in_checkout(): with self.in_checkout():
@ -73,6 +79,7 @@ class Repository(object):
return return
self.install(cmd_runner) self.install(cmd_runner)
self.__installed = True
def install(self, cmd_runner): def install(self, cmd_runner):
"""Install the hook repository. """Install the hook repository.
@ -82,8 +89,15 @@ class Repository(object):
""" """
self.require_created() self.require_created()
repo_cmd_runner = self.get_cmd_runner(cmd_runner) repo_cmd_runner = self.get_cmd_runner(cmd_runner)
for language in self.languages: for language_name in self.languages:
languages[language].install_environment(repo_cmd_runner) language = languages[language_name]
if (
language.ENVIRONMENT_DIR is None or
repo_cmd_runner.exists(language.ENVIRONMENT_DIR)
):
# The language is already installed
continue
language.install_environment(repo_cmd_runner)
@contextlib.contextmanager @contextlib.contextmanager
def in_checkout(self): def in_checkout(self):

View file

@ -9,3 +9,4 @@ from pre_commit.languages.all import languages
def test_all_languages_support_interface(language): def test_all_languages_support_interface(language):
assert hasattr(languages[language], 'install_environment') assert hasattr(languages[language], 'install_environment')
assert hasattr(languages[language], 'run_hook') assert hasattr(languages[language], 'run_hook')
assert hasattr(languages[language], 'ENVIRONMENT_DIR')

View file

@ -1,3 +1,5 @@
import __builtin__
import mock
import os import os
import pytest import pytest
@ -130,3 +132,35 @@ def test_sha(mock_repo_config):
def test_languages(config_for_python_hooks_repo): def test_languages(config_for_python_hooks_repo):
repo = Repository(config_for_python_hooks_repo) repo = Repository(config_for_python_hooks_repo)
assert repo.languages == set(['python']) assert repo.languages == set(['python'])
@pytest.yield_fixture
def print_mock():
with mock.patch.object(__builtin__, 'print', autospec=True) as print_mock:
yield print_mock
def test_prints_while_creating(config_for_python_hooks_repo, print_mock):
repo = Repository(config_for_python_hooks_repo)
repo.require_created()
print_mock.assert_called_with('This may take a few minutes...')
print_mock.reset_mock()
# Reinstall with same repo should not trigger another install
repo.require_created()
assert print_mock.call_count == 0
# Reinstall on another run should not trigger another install
repo = Repository(config_for_python_hooks_repo)
repo.require_created()
assert print_mock.call_count == 0
def test_reinstall(config_for_python_hooks_repo):
repo = Repository(config_for_python_hooks_repo)
repo.require_installed(PrefixedCommandRunner(C.HOOKS_WORKSPACE))
# Reinstall with same repo should not trigger another install
# TODO: how to assert this?
repo.require_installed(PrefixedCommandRunner(C.HOOKS_WORKSPACE))
# Reinstall on another run should not trigger another install
# TODO: how to assert this?
repo = Repository(config_for_python_hooks_repo)
repo.require_installed(PrefixedCommandRunner(C.HOOKS_WORKSPACE))