port hook template to bash

this avoids some version-specific code in python

this also makes the bootstrap script slightly more portable
This commit is contained in:
Anthony Sottile 2021-09-27 19:34:04 -04:00
parent ef7b126ee3
commit e622f793c3
3 changed files with 20 additions and 49 deletions

View file

@ -1,6 +1,7 @@
import itertools import itertools
import logging import logging
import os.path import os.path
import shlex
import shutil import shutil
import sys import sys
from typing import Optional from typing import Optional
@ -100,19 +101,17 @@ def _install_hook_script(
args = ['hook-impl', f'--config={config_file}', f'--hook-type={hook_type}'] args = ['hook-impl', f'--config={config_file}', f'--hook-type={hook_type}']
if skip_on_missing_config: if skip_on_missing_config:
args.append('--skip-on-missing-config') args.append('--skip-on-missing-config')
params = {'INSTALL_PYTHON': sys.executable, 'ARGS': args}
with open(hook_path, 'w') as hook_file: with open(hook_path, 'w') as hook_file:
contents = resource_text('hook-tmpl') contents = resource_text('hook-tmpl')
before, rest = contents.split(TEMPLATE_START) before, rest = contents.split(TEMPLATE_START)
to_template, after = rest.split(TEMPLATE_END) _, after = rest.split(TEMPLATE_END)
before = before.replace('#!/usr/bin/env python3', shebang())
hook_file.write(before + TEMPLATE_START) hook_file.write(before + TEMPLATE_START)
for line in to_template.splitlines(): hook_file.write(f'INSTALL_PYTHON={shlex.quote(sys.executable)}\n')
var = line.split()[0] # TODO: python3.8+: shlex.join
hook_file.write(f'{var} = {params[var]!r}\n') args_s = ' '.join(shlex.quote(part) for part in args)
hook_file.write(f'ARGS=({args_s})\n')
hook_file.write(TEMPLATE_END + after) hook_file.write(TEMPLATE_END + after)
make_executable(hook_path) make_executable(hook_path)

View file

@ -1,44 +1,20 @@
#!/usr/bin/env python3 #!/usr/bin/env bash
# File generated by pre-commit: https://pre-commit.com # File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03 # ID: 138fd403232d2ddd5efb44317e38bf03
import os
import sys
# we try our best, but the shebang of this script is difficult to determine:
# - macos doesn't ship with python3
# - windows executables are almost always `python.exe`
# therefore we continue to support python2 for this small script
if sys.version_info < (3, 3):
from distutils.spawn import find_executable as which
else:
from shutil import which
# work around https://github.com/Homebrew/homebrew-core/issues/30445
os.environ.pop('__PYVENV_LAUNCHER__', None)
# start templated # start templated
INSTALL_PYTHON = '' INSTALL_PYTHON=''
ARGS = ['hook-impl'] ARGS=(hook-impl)
# end templated # end templated
ARGS.extend(('--hook-dir', os.path.realpath(os.path.dirname(__file__))))
ARGS.append('--')
ARGS.extend(sys.argv[1:])
DNE = '`pre-commit` not found. Did you forget to activate your virtualenv?' HERE="$(cd "$(dirname "$0")" && pwd)"
if os.access(INSTALL_PYTHON, os.X_OK): ARGS+=(--hook-dir "$HERE" -- "$@")
CMD = [INSTALL_PYTHON, '-mpre_commit']
elif which('pre-commit'):
CMD = ['pre-commit']
else:
raise SystemExit(DNE)
CMD.extend(ARGS) if [ -x "$INSTALL_PYTHON" ]; then
if sys.platform == 'win32': # https://bugs.python.org/issue19124 exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}"
import subprocess elif command -v pre-commit; then
exec pre-commit "${ARGS[@]}"
if sys.version_info < (3, 7): # https://bugs.python.org/issue25942 else
raise SystemExit(subprocess.Popen(CMD).wait()) echo '`pre-commit` not found. Did you forget to activate your virtualenv?' 1>&2
else: exit 1
raise SystemExit(subprocess.call(CMD)) fi
else:
os.execvp(CMD[0], CMD)

View file

@ -278,11 +278,7 @@ def test_environment_not_sourced(tempdir_factory, store):
hook = os.path.join(path, '.git/hooks/pre-commit') hook = os.path.join(path, '.git/hooks/pre-commit')
with open(hook) as f: with open(hook) as f:
src = f.read() src = f.read()
src = re.sub( src = re.sub('\nINSTALL_PYTHON=.*\n', '\nINSTALL_PYTHON="/dne"\n', src)
'\nINSTALL_PYTHON =.*\n',
'\nINSTALL_PYTHON = "/dne"\n',
src,
)
with open(hook, 'w') as f: with open(hook, 'w') as f:
f.write(src) f.write(src)