diff --git a/.gitignore b/.gitignore index 831f634e..e20fc65c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.pyc .pydevproject +.pre-commit-files .project .coverage /py_env diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index c5cab017..77819d1f 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -15,6 +15,10 @@ def in_env(): def install_environment(): assert local.path('setup.py').exists() + # Return immediately if we already have a virtualenv + if local.path('py_env').exists(): + return + # Install a virtualenv local['virtualenv'][PY_ENV]() diff --git a/pre_commit/run.py b/pre_commit/run.py index 400cbc9d..63ba44cc 100644 --- a/pre_commit/run.py +++ b/pre_commit/run.py @@ -1,20 +1,45 @@ import argparse +import os.path from pre_commit import git +from pre_commit.clientlib.validate_config import validate_config +from pre_commit.repository import Repository def install(): """Install the pre-commit hook.""" git.create_pre_commit() + def uninstall(): """Uninstall the pre-commit hook.""" - raise NotImplementedError + git.remove_pre_commit() def run_hooks(arguments): """Actually run the hooks.""" raise NotImplementedError +def run_single_hook(hook_id): + configs = validate_config([]) + for config in configs: + repo = Repository(config) + if hook_id in repo.hooks: + repo.install() + + retcode, stdout, stderr = repo.run_hook(hook_id, map(os.path.abspath, ['pre_commit/constants.py'])) + + if retcode != repo.hooks[hook_id].get('expected_return_value', 0): + for out in (stdout, stderr): + out = out.rstrip() + if len(out) > 0: + print out + return 1 + else: + return 0 + else: + print "No hook with id {0}".format(hook_id) + return 1 + def run(argv): parser = argparse.ArgumentParser() @@ -30,6 +55,10 @@ def run(argv): action='store_true', help='Uninstall the pre-commit script.', ) + group.add_argument( + '-r', '--run', + help='Run a hook' + ) args = parser.parse_args(argv) @@ -37,5 +66,7 @@ def run(argv): return install() elif args.uninstall: return uninstall() + elif args.run: + return run_single_hook(args.run) else: return run_hooks(args)