diff --git a/tox_pyenv.py b/tox_pyenv.py index a88884e..e9bae5f 100644 --- a/tox_pyenv.py +++ b/tox_pyenv.py @@ -67,7 +67,12 @@ class PyenvMissing(ToxPyenvException, RuntimeError): """The pyenv program is not installed.""" -class PyenvWhichFailed(ToxPyenvException): +class PyenvFailed(ToxPyenvException): + + """Calling `pyenv` failed.""" + + +class PyenvWhichFailed(PyenvFailed): """Calling `pyenv which` failed.""" @@ -173,6 +178,39 @@ def _enable_and_call(_available_version): return _enable_and_call(match) +@tox_hookimpl +def tox_get_python_executable(envconfig): + try: + pyenv = (getattr(py.path.local.sysfind('pyenv'), 'strpath', 'pyenv') + or 'pyenv') + cmd = [pyenv, 'which', envconfig.basepython] + pipe = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True + ) + out, err = pipe.communicate() + except OSError: + err = '\'pyenv\': command not found' + LOG.warning( + "pyenv doesn't seem to be installed, you probably " + "don't want this plugin installed either." + ) + else: + if pipe.poll() == 0: + return out.strip() + else: + if not envconfig.tox_pyenv_fallback: + raise PyenvFailed(err) + LOG.debug("`%s` failed thru tox-pyenv plugin, falling back. " + "STDERR: \"%s\" | To disable this behavior, set " + "tox_pyenv_fallback=False in your tox.ini or use " + " --tox-pyenv-no-fallback on the command line.", + ' '.join([str(x) for x in cmd]), err) + + + @tox_hookimpl def tox_get_python_executable(envconfig): """Hook into tox plugins to use pyenv to find executables."""