pre-commit/pre_commit/resources/hook-tmpl
Martin Trautmann dbdba3c67f Activate conda environment in pre-commit hook.
Save conda environment that was active during conda install when using
option --hooks-activate-conda. The saved environment will be activated
before calling pre-commit hooks.

Especially on Windows, more and more actions within a conda environment
require the conda environment to be activated. Thus saving just the
python executable is not enough any more.

There is currently one downside of using the option
--hooks-activate-conda. It uses "conda run" which will only show
console output after the run is completed. We have a pull request to
conda open which introduces an option to show interactive console
output in conda run. Once this is approved, it might be ok to make this
option the default behaviour.
2020-02-10 16:56:29 +01:00

48 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python3
# File generated by pre-commit: https://pre-commit.com
# 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
INSTALL_PYTHON = ''
INSTALL_CONDA = ''
INSTALL_CONDA_PREFIX = ''
ARGS = ['hook-impl']
# 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?'
if os.access(INSTALL_CONDA, os.X_OK):
CMD = [INSTALL_CONDA, 'run', '-p', INSTALL_CONDA_PREFIX, 'pre-commit']
elif os.access(INSTALL_PYTHON, os.X_OK):
CMD = [INSTALL_PYTHON, '-mpre_commit']
elif which('pre-commit'):
CMD = ['pre-commit']
else:
raise SystemExit(DNE)
CMD.extend(ARGS)
if sys.platform == 'win32': # https://bugs.python.org/issue19124
import subprocess
if sys.version_info < (3, 7): # https://bugs.python.org/issue25942
raise SystemExit(subprocess.Popen(CMD).wait())
else:
raise SystemExit(subprocess.call(CMD))
else:
os.execvp(CMD[0], CMD)