From 29896e648a67a4195429047621b10511a9312d3c Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Thu, 16 Jun 2016 06:30:07 -0500 Subject: [PATCH] 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. --- flake8/main/git.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/flake8/main/git.py b/flake8/main/git.py index 39c2ad1..bae0233 100644 --- a/flake8/main/git.py +++ b/flake8/main/git.py @@ -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