Use a template for git pre-commit hook

Previously, we forced Flake8 to be installed in whatever Python
environment that the user was using. This allows someone to use Flake8
from a virtual environment, e.g., if you use the tox.ini from this
commit, you can do:

    tox -e venv -- flake8 --install-hook git

And that will allow you to use the Python and Flake8 from
./.tox/venv/bin/python. This means that you can avoid installing
Flake8 globally and still have a working commit hook.
This commit is contained in:
Ian Cordasco 2016-06-16 06:30:07 -05:00
parent 07101231d9
commit 29896e648a
No known key found for this signature in database
GPG key ID: 656D3395E4A9791A

View file

@ -9,6 +9,7 @@ import os
import shutil
import stat
import subprocess
import sys
import tempfile
from flake8 import defaults
@ -80,8 +81,10 @@ def install():
path=pre_commit_file,
)
executable = get_executable()
with open(pre_commit_file, 'w') as fd:
fd.write(_HOOK_TEMPLATE)
fd.write(_HOOK_TEMPLATE.format(executable=executable))
# NOTE(sigmavirus24): The following sets:
# - read, write, and execute permissions for the owner
@ -94,6 +97,12 @@ def install():
return True
def get_executable():
if sys.executable is not None:
return sys.executable
return '/usr/bin/env python'
def find_git_directory():
rev_parse = piped_process(['git', 'rev-parse', '--git-dir'])
@ -182,7 +191,7 @@ def config_for(parameter):
return value.lower() in defaults.TRUTHY_VALUES
_HOOK_TEMPLATE = """#!/usr/bin/env python
_HOOK_TEMPLATE = """#!{executable}
import os
import sys