From af438abeaec9632453be96e163d787ee5e78b61b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 18 May 2015 16:34:02 +0200 Subject: [PATCH 1/3] shims: look for python* This is required for the shims to handle `#!/usr/bin/env python3` in a shebang, just like `python` is handled currently: it will set `PYENV_DIR` to the root of the invoked script, which is required for a `.python-version` script to get picked up from there. This was rejected for rbenv, where it does not make much sense (https://github.com/sstephenson/rbenv/pull/735). Ref: https://github.com/yyuu/pyenv/pull/368#issuecomment-102806837 --- libexec/pyenv-rehash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/pyenv-rehash b/libexec/pyenv-rehash index c266b1bf..56756fc3 100755 --- a/libexec/pyenv-rehash +++ b/libexec/pyenv-rehash @@ -46,7 +46,7 @@ set -e [ -n "\$PYENV_DEBUG" ] && set -x program="\${0##*/}" -if [ "\$program" = "python" ]; then +if [[ "\$program" = "python"* ]]; then for arg; do case "\$arg" in -c* | -- ) break ;; From 493f036928b8ed9e456b2112475973e23b1ec330 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 8 Jul 2015 16:20:14 +0200 Subject: [PATCH 2/3] shims: handle symlinked scripts, via new PYENV_FILE_ARG `PYENV_FILE_ARG` is used here to make use of `abs_dirname` later in `libexec/pyenv`. Fixes https://github.com/yyuu/pyenv/issues/404 --- libexec/pyenv | 13 +++++++++++++ libexec/pyenv-rehash | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/libexec/pyenv b/libexec/pyenv index 9dd464a9..8173f6dc 100755 --- a/libexec/pyenv +++ b/libexec/pyenv @@ -54,6 +54,19 @@ else fi export PYENV_ROOT +# Transfer PYENV_FILE_ARG (from shims) into PYENV_DIR. +if [ -z "${PYENV_DIR}" ]; then + if [ -n "${PYENV_FILE_ARG}" ]; then + if [ -L "${PYENV_FILE_ARG}" ]; then + PYENV_DIR="$(abs_dirname "${PYENV_FILE_ARG}")" + else + PYENV_DIR="${PYENV_FILE_ARG%/*}" + fi + export PYENV_DIR + unset PYENV_FILE_ARG + fi +fi + if [ -z "${PYENV_DIR}" ]; then PYENV_DIR="$(pwd)" else diff --git a/libexec/pyenv-rehash b/libexec/pyenv-rehash index 56756fc3..a6451e0c 100755 --- a/libexec/pyenv-rehash +++ b/libexec/pyenv-rehash @@ -52,7 +52,7 @@ if [[ "\$program" = "python"* ]]; then -c* | -- ) break ;; */* ) if [ -f "\$arg" ]; then - export PYENV_DIR="\${arg%/*}" + export PYENV_FILE_ARG="\$arg" break fi ;; From 438e828eb507536eebe42adee3e896bceb165d8c Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Tue, 22 Sep 2015 15:28:00 +0900 Subject: [PATCH 3/3] add tests for `PYENV_FILE_ARG` --- test/pyenv_ext.bats | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/pyenv_ext.bats b/test/pyenv_ext.bats index cc138cbe..fdae87fc 100644 --- a/test/pyenv_ext.bats +++ b/test/pyenv_ext.bats @@ -12,3 +12,19 @@ load test_helper PYENV_VERSION="2.7.10:system" run pyenv-prefix assert_success "${PYENV_ROOT}/versions/2.7.10:${PYENV_TEST_DIR}" } + +@test "should use dirname of file argument as PYENV_DIR" { + mkdir -p "${PYENV_TEST_DIR}/dir1" + touch "${PYENV_TEST_DIR}/dir1/file.py" + PYENV_FILE_ARG="${PYENV_TEST_DIR}/dir1/file.py" run pyenv echo PYENV_DIR + assert_output "${PYENV_TEST_DIR}/dir1" +} + +@test "should follow symlink of file argument (#379, #404)" { + mkdir -p "${PYENV_TEST_DIR}/dir1" + mkdir -p "${PYENV_TEST_DIR}/dir2" + touch "${PYENV_TEST_DIR}/dir1/file.py" + ln -s "${PYENV_TEST_DIR}/dir1/file.py" "${PYENV_TEST_DIR}/dir2/symlink.py" + PYENV_FILE_ARG="${PYENV_TEST_DIR}/dir2/symlink.py" run pyenv echo PYENV_DIR + assert_output "${PYENV_TEST_DIR}/dir1" +}