From 13122ca9bfcaf67485f19ea1f8b29ce8015faf53 Mon Sep 17 00:00:00 2001 From: Yamashita Yuu Date: Fri, 7 Sep 2012 19:16:42 +0900 Subject: [PATCH] fixed to read multiple versions from `PYENV_VERSION` environment variable --- README.md | 36 ++++++++++++++++++++++++++------ libexec/pyenv-global | 10 ++++----- libexec/pyenv-local | 12 +++++------ libexec/pyenv-prefix | 10 +++++---- libexec/pyenv-sh-shell | 11 +++++----- libexec/pyenv-version-file-write | 13 ++++-------- libexec/pyenv-version-name | 15 ++++++++----- libexec/pyenv-which | 4 ++-- 8 files changed, 69 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 954ee8a5..40d67f1e 100644 --- a/README.md +++ b/README.md @@ -170,15 +170,19 @@ The special version name `system` tells pyenv to use the system Python When run without a version number, `pyenv global` reports the currently configured global version. -_pyenv extension_ - -You can specify multiple versions for global Python. Commands within -these versions are searched by specified order. +And also, you can specify multiple versions as global Python. Commands +within these Python versions are searched by specified order. $ pyenv global 2.7.3 3.2.3 $ pyenv global 2.7.3 3.2.3 + $ pyenv which python2.7 + /home/yyuu/.pyenv/versions/2.7.3/bin/python2.7 + $ pyenv which python3.2 + /home/yyuu/.pyenv/versions/3.2.3/bin/python3.2 + $ pyenv which python + /home/yyuu/.pyenv/versions/2.7.3/bin/python ### 3.2 pyenv local @@ -195,9 +199,19 @@ configured local version. You can also unset the local version: $ pyenv local --unset -_pyenv extension_ +And also, you can specify multiple versions as local Python. Commands +within these Python versions are searched by specified order. -You can specify multiple versions for local Python. + $ pyenv local 2.7.3 3.2.3 + $ pyenv local + 2.7.3 + 3.2.3 + $ pyenv which python2.7 + /home/yyuu/.pyenv/versions/2.7.3/bin/python2.7 + $ pyenv which python3.2 + /home/yyuu/.pyenv/versions/3.2.3/bin/python3.2 + $ pyenv which python + /home/yyuu/.pyenv/versions/2.7.3/bin/python ### 3.3 pyenv shell @@ -219,6 +233,16 @@ prefer not to use shell integration, you may simply set the $ export PYENV_VERSION=pypy-1.9 +And also, you can specify multiple versions via `PYENV_VERSION` +environment variable in your shell. + + $ pyenv shell pypy-1.9 2.7.3 + $ echo $PYENV_VERSION + pypy-1.9:2.7.3 + $ pyenv version + pypy-1.9 (set by PYENV_VERSION environment variable) + 2.7.3 (set by PYENV_VERSION environment variable) + ### 3.4 pyenv versions Lists all Python versions known to pyenv, and shows an asterisk next to diff --git a/libexec/pyenv-global b/libexec/pyenv-global index 70af07b2..f3dc712b 100755 --- a/libexec/pyenv-global +++ b/libexec/pyenv-global @@ -8,19 +8,19 @@ if [ "$1" = "--complete" ]; then exec pyenv-versions --bare fi -PYENV_VERSION=($@) +versions=($@) PYENV_VERSION_FILE="${PYENV_ROOT}/version" -if [ -n "$PYENV_VERSION" ]; then - pyenv-version-file-write "$PYENV_VERSION_FILE" "${PYENV_VERSION[@]}" +if [ -n "$versions" ]; then + pyenv-version-file-write "$PYENV_VERSION_FILE" "${versions[@]}" else - IFS=: PYENV_VERSION=($( + IFS=: versions=($( pyenv-version-file-read "$PYENV_VERSION_FILE" || pyenv-version-file-read "${PYENV_ROOT}/global" || pyenv-version-file-read "${PYENV_ROOT}/default" || echo system )) - for version in "${PYENV_VERSION[@]}"; do + for version in "${versions[@]}"; do echo "$version" done fi diff --git a/libexec/pyenv-local b/libexec/pyenv-local index 8f41feab..c7c72d35 100755 --- a/libexec/pyenv-local +++ b/libexec/pyenv-local @@ -9,21 +9,21 @@ if [ "$1" = "--complete" ]; then exec pyenv-versions --bare fi -PYENV_VERSION=($@) +versions=($@) PYENV_VERSION_FILE=".pyenv-version" -if [ "$PYENV_VERSION" = "--unset" ]; then +if [ "$versions" = "--unset" ]; then rm -f "$PYENV_VERSION_FILE" -elif [ -n "$PYENV_VERSION" ]; then - pyenv-version-file-write "$PYENV_VERSION_FILE" "${PYENV_VERSION[@]}" +elif [ -n "$versions" ]; then + pyenv-version-file-write "$PYENV_VERSION_FILE" "${versions[@]}" else - IFS=: PYENV_VERSION=($( + IFS=: versions=($( pyenv-version-file-read "$PYENV_VERSION_FILE" || { echo "pyenv: no local version configured for this directory" exit 1 } >&2 )) - for version in "${PYENV_VERSION[@]}"; do + for version in "${versions[@]}"; do echo "$version" done fi diff --git a/libexec/pyenv-prefix b/libexec/pyenv-prefix index c1569f7b..1869983e 100755 --- a/libexec/pyenv-prefix +++ b/libexec/pyenv-prefix @@ -9,19 +9,21 @@ if [ "$1" = "--complete" ]; then fi if [ -n "$1" ]; then - export PYENV_VERSION=($@) + versions=($@) + IFS=: PYENV_VERSION="${versions[*]}" + export PYENV_VERSION else - IFS=: PYENV_VERSION=($(pyenv-version-name)) + IFS=: versions=($(pyenv-version-name)) fi -if [ "$PYENV_VERSION" = "system" ]; then +if [ "$versions" = "system" ]; then PYTHON_PATH="$(pyenv-which python)" echo "${PYTHON_PATH%/*}" exit fi PYENV_PREFIX_PATHS=() -for version in "${PYENV_VERSION[@]}"; do +for version in "${versions[@]}"; do PYENV_PREFIX_PATH="${PYENV_ROOT}/versions/${version}" if [ -d "$PYENV_PREFIX_PATH" ]; then PYENV_PREFIX_PATHS=("${PYENV_PREFIX_PATHS[@]}" "$PYENV_PREFIX_PATH") diff --git a/libexec/pyenv-sh-shell b/libexec/pyenv-sh-shell index b1f3fafa..5b47b739 100755 --- a/libexec/pyenv-sh-shell +++ b/libexec/pyenv-sh-shell @@ -9,9 +9,9 @@ if [ "$1" = "--complete" ]; then exec pyenv-versions --bare fi -version="$1" +versions=("$@") -if [ -z "$version" ]; then +if [ -z "$versions" ]; then if [ -z "$PYENV_VERSION" ]; then echo "pyenv: no shell-specific version configured" >&2 exit 1 @@ -21,12 +21,13 @@ if [ -z "$version" ]; then fi fi -if [ "$version" = "--unset" ]; then +if [ "$versions" = "--unset" ]; then echo "unset PYENV_VERSION" exit 1 fi # Make sure the specified version is installed. -pyenv-prefix $version >/dev/null +pyenv-prefix "${versions[@]}" >/dev/null -echo "export PYENV_VERSION=\"${version}\"" +IFS=: PYENV_VERSION="${versions[*]}" +echo "export PYENV_VERSION=\"${PYENV_VERSION}\"" diff --git a/libexec/pyenv-version-file-write b/libexec/pyenv-version-file-write index 9904c8ef..cca0c36a 100755 --- a/libexec/pyenv-version-file-write +++ b/libexec/pyenv-version-file-write @@ -4,23 +4,18 @@ set -e PYENV_VERSION_FILE="$1" shift -PYENV_VERSION=() -for version in "$@"; do - PYENV_VERSION=("${PYENV_VERSION[@]}" "$version") -done +versions=("$@") -if [ -z "$PYENV_VERSION" ] || [ -z "$PYENV_VERSION_FILE" ]; then +if [ -z "$versions" ] || [ -z "$PYENV_VERSION_FILE" ]; then echo "usage: pyenv write-version-file FILENAME VERSIONS..." >&2 exit 1 fi # Make sure the specified version is installed. -for version in "${PYENV_VERSION[@]}"; do - pyenv-prefix "$version" >/dev/null -done +pyenv-prefix "${vesions[@]}" >/dev/null # Write the version out to disk. rm -f "$PYENV_VERSION_FILE" -for version in "${PYENV_VERSION[@]}"; do +for version in "${versions[@]}"; do echo "$version" >> "$PYENV_VERSION_FILE" done diff --git a/libexec/pyenv-version-name b/libexec/pyenv-version-name index c09f7255..763a2094 100755 --- a/libexec/pyenv-version-name +++ b/libexec/pyenv-version-name @@ -2,17 +2,22 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x -if [ -z "$PYENV_VERSION" ]; then +if [ -n "$PYENV_VERSION" ]; then +# IFS=: versions=($(${PYENV_VERSION})) # this does not work on zsh without `setopt shwordsplit`. + versions=($(echo "${PYENV_VERSION}" | tr ":" " ")) +else PYENV_VERSION_FILE="$(pyenv-version-file)" - IFS=: PYENV_VERSION=($(pyenv-version-file-read "$PYENV_VERSION_FILE" || true)) + IFS=: versions=($(pyenv-version-file-read "$PYENV_VERSION_FILE" || true)) + IFS=: PYENV_VERSION="${versions[*]}" + export PYENV_VERSION fi -if [ -z "$PYENV_VERSION" ] || [ "$PYENV_VERSION" = "system" ] ; then +if [ -z "$versions" ] || [ "$versions" = "system" ] ; then echo "system" exit fi -for version in "${PYENV_VERSION[@]}"; do +for version in "${versions[@]}"; do PYENV_VERSION_PATH="${PYENV_ROOT}/versions/${version}" if [ ! -d "$PYENV_VERSION_PATH" ]; then @@ -21,4 +26,4 @@ for version in "${PYENV_VERSION[@]}"; do fi done -IFS=: echo "${PYENV_VERSION[*]}" +echo "${PYENV_VERSION}" diff --git a/libexec/pyenv-which b/libexec/pyenv-which index bf246bc0..030ea6dc 100755 --- a/libexec/pyenv-which +++ b/libexec/pyenv-which @@ -41,7 +41,7 @@ remove_from_path() { } IFS=: versions=($(pyenv-version-name)) -PYENV_VERSION=("${versions[@]}") +IFS=: PYENV_VERSION="${versions[*]}" PYENV_COMMAND="$1" if [ -z "$PYENV_COMMAND" ]; then @@ -49,7 +49,7 @@ if [ -z "$PYENV_COMMAND" ]; then exit 1 fi -for version in "${PYENV_VERSION[@]}"; do +for version in "${versions[@]}"; do if [ "$version" = "system" ]; then PATH="$(remote_from_path "${PYENV_ROOT}/shims")" PYENV_COMMAND_PATH="$(command -v "$PYENV_COMMAND")"