From a4d4bb625e8f013bc99c633aa46f7300cd25696e Mon Sep 17 00:00:00 2001 From: Andrew Christianson Date: Tue, 5 Mar 2019 11:22:57 -0800 Subject: [PATCH] use realpath of scripts to determine relative locations Just using paths relative to ${BASH_SOURCE} fails if the scripts are symlinked to a location, as that variable reports the path at which the script is invoked, not where it actually resides. This commit implements a simple version of the realpath functionality in order to determine the path to the more comprehensive realpath script --- bin/pyenv-virtualenv-prefix | 16 +++++++++++++++- bin/pyenv-virtualenvs | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/bin/pyenv-virtualenv-prefix b/bin/pyenv-virtualenv-prefix index aa71777d..bbe58719 100755 --- a/bin/pyenv-virtualenv-prefix +++ b/bin/pyenv-virtualenv-prefix @@ -6,7 +6,21 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x -. "${BASH_SOURCE%/*}"/../libexec/pyenv-virtualenv-realpath +if [ -L "${BASH_SOURCE}" ]; then + READLINK=$(type -p greadlink readlink | head -1) + if [ -z "$READLINK" ]; then + echo "pyenv: cannot find readlink - are you missing GNU coreutils?" >&2 + exit 1 + fi + resolve_link() { + $READLINK -f "$1" + } + script_path=$(resolve_link ${BASH_SOURCE}) +else + script_path=${BASH_SOURCE} +fi + +. ${script_path%/*}/../libexec/pyenv-virtualenv-realpath if [ -z "$PYENV_ROOT" ]; then PYENV_ROOT="${HOME}/.pyenv" diff --git a/bin/pyenv-virtualenvs b/bin/pyenv-virtualenvs index 173278be..b441e78c 100755 --- a/bin/pyenv-virtualenvs +++ b/bin/pyenv-virtualenvs @@ -7,7 +7,21 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x -. "${BASH_SOURCE%/*}"/../libexec/pyenv-virtualenv-realpath +if [ -L "${BASH_SOURCE}" ]; then + READLINK=$(type -p greadlink readlink | head -1) + if [ -z "$READLINK" ]; then + echo "pyenv: cannot find readlink - are you missing GNU coreutils?" >&2 + exit 1 + fi + resolve_link() { + $READLINK -f "$1" + } + script_path=$(resolve_link ${BASH_SOURCE}) +else + script_path=${BASH_SOURCE} +fi + +. ${script_path%/*}/../libexec/pyenv-virtualenv-realpath if [ -z "$PYENV_ROOT" ]; then PYENV_ROOT="${HOME}/.pyenv"