12 Commits

Author SHA1 Message Date
Yamashita Yuu
2a5a483c5d v20130614 2013-06-14 21:31:24 +09:00
Yamashita Yuu
a2c878f284 remove --distribute from example.
it will be replaced by new setuptools in near future.
2013-06-04 20:32:22 +09:00
Yamashita Yuu
b7c498ce03 add pyenv-virtualenv-prefix 2013-06-04 18:56:58 +09:00
Yamashita Yuu
6915cd0320 no need to import print_function in this case 2013-06-04 17:52:10 +09:00
Yamashita Yuu
4263783195 add pyenv virtualenvs command 2013-06-04 17:07:48 +09:00
Yamashita Yuu
65b58770a7 display package list on unsuccessful upgrade 2013-05-28 21:07:35 +09:00
Yamashita Yuu
66546cea12 ignore stderr from pyenv-exec 2013-05-28 16:54:25 +09:00
Yamashita Yuu
7562795c42 test the existence of ${VIRTUALENV_PIP_BIN} before invoking it 2013-05-28 16:30:39 +09:00
Yamashita Yuu
72592fde6b Merge branch 'upgrade-virtualenv' 2013-05-28 13:44:37 +09:00
Yamashita Yuu
8ac27a7a77 add --upgrade option to upgrade existing virtualenv 2013-05-28 13:43:17 +09:00
Yamashita Yuu
f9532f6c4e load hooks just before creating virtualenv 2013-05-28 13:37:32 +09:00
Yamashita Yuu
67d6cfc0d2 handle --force option expectedly 2013-05-27 23:50:13 +09:00
4 changed files with 159 additions and 35 deletions

View File

@@ -30,7 +30,7 @@ To create a virtualenv for the Python version use with pyenv, run
`pyenv virtualenv` with tha exact name of the version you want to create
virtualenv. For example,
$ pyenv virtualenv --distribute 2.7.3 venv27
$ pyenv virtualenv 2.7.3 venv27
virtualenvs will be created into a directory of the same name
under `~/.pyenv/versions`.
@@ -50,6 +50,11 @@ You can set certain environment variables to control the pyenv-virtualenv.
## Version History
#### 20130614
* Add `pyenv virtualenvs` to list all virtualenv versions.
* *EXPERIMENTAL* Add `--upgrade` option to re-create virtualenv with migrating packages
#### 20130527
* Remove `python-virtualenv` which was no longer used.

View File

@@ -2,12 +2,15 @@
#
# Summary: Create a Python virtualenv using the pyenv-virtualenv plugin
#
# Usage: pyenv virtualenv [VIRTUALENV_OPTIONS] <version> <virtualenv-name>
# Usage: pyenv virtualenv [-f|--force] [-u|--upgrade] [VIRTUALENV_OPTIONS] <version> <virtualenv-name>
# pyenv virtualenv --version
# pyenv virtualenv --help
#
# -u/--upgrade Upgrade existing virtualenv with migrating installed packages
# -f/--force Install even if the version appears to be installed already
#
PYENV_VIRTUALENV_VERSION="20130527"
PYENV_VIRTUALENV_VERSION="20130614"
VIRTUALENV_VERSION="${VIRTUALENV_VERSION:-1.9.1}"
set -e
@@ -97,7 +100,7 @@ http_get_wget() {
}
version() {
local version="$(pyenv-exec python "${VIRTUALENV}" --version || true)"
local version="$(pyenv-exec python "${VIRTUALENV}" --version 2>/dev/null || true)"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv ${version:-unknown})"
}
@@ -133,6 +136,8 @@ ensure_virtualenv "${VIRTUALENV}" "${VIRTUALENV_URL}" || {
exit 1
}
unset FORCE
unset UPGRADE
# Unset environment variables which starts with `VIRTUALENV_`.
# These variables are reserved for virtualenv.
unset VIRTUALENV_VERSION
@@ -140,9 +145,15 @@ unset VIRTUALENV_VERSION
parse_options "$@"
for option in "${OPTIONS[@]}"; do
case "$option" in
"f" | "force" )
FORCE=true
;;
"h" | "help" )
usage 0
;;
"u" | "upgrade" )
UPGRADE=true
;;
"version" )
version
exit 0
@@ -157,29 +168,19 @@ done
VERSION_NAME="${ARGUMENTS[0]}"
[ -n "$VERSION_NAME" ] || usage 1
if [ -z "$TMPDIR" ]; then
TMP="/tmp"
else
TMP="${TMPDIR%/}"
fi
# Define `before_virtualenv` and `after_virtualenv` functions that allow
# plugin hooks to register a string of code for execution before or
# after the installation process.
declare -a before_hooks after_hooks
before_virtualenv() {
local hook="$1"
before_hooks["${#before_hooks[@]}"]="$hook"
}
after_virtualenv() {
local hook="$1"
after_hooks["${#after_hooks[@]}"]="$hook"
}
# Load plugin hooks.
for script in $(pyenv-hooks virtualenv); do
source "$script"
done
SEED="$(date "+%Y%m%d%H%M%S").$$"
UPGRADE_PATH="${TMP}/pyenv-virtualenv.${SEED}"
UPGRADE_LIST="${TMP}/pyenv-virtualenv.${SEED}.txt"
PYTHON_BIN=$(PYENV_VERSION="${VERSION_NAME}" pyenv-which python)
PYTHON_BIN="$(PYENV_VERSION="${VERSION_NAME}" pyenv-which python)"
if [ ! -x "${PYTHON_BIN}" ]; then
echo "pyenv-virtualenv: could not find python executable: ${PYTHON_BIN}" 1>&2
exit 1
@@ -205,19 +206,54 @@ fi
VIRTUALENV_NAME="${ARGUMENTS[1]##*/}"
VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME}"
VIRTUALENV_PYTHON_BIN="${VIRTUALENV_PATH}/bin/python"
VIRTUALENV_PIP_BIN="${VIRTUALENV_PATH}/bin/pip"
# Define `before_virtualenv` and `after_virtualenv` functions that allow
# plugin hooks to register a string of code for execution before or
# after the installation process.
declare -a before_hooks after_hooks
before_virtualenv() {
local hook="$1"
before_hooks["${#before_hooks[@]}"]="$hook"
}
after_virtualenv() {
local hook="$1"
after_hooks["${#after_hooks[@]}"]="$hook"
}
# Load plugin hooks.
for script in $(pyenv-hooks virtualenv); do
source "$script"
done
[ -d "${VIRTUALENV_PATH}" ] && PREFIX_EXISTS=1
# If the virtualenv exists, prompt for confirmation unless
# the --force option was specified.
if [ -z "$FORCE" ] && [ -d "${VIRTUALENV_PATH}/bin" ]; then
echo "pyenv: $VIRTUALENV_PATH already exists" >&2
read -p "continue with installation? (y/N) "
if [ -d "${VIRTUALENV_PATH}/bin" ]; then
if [ -z "$FORCE" ]; then
echo "pyenv: ${VIRTUALENV_PATH} already exists" 1>&2
read -p "continue with installation? (y/N) "
case "$REPLY" in
y* | Y* ) ;;
* ) exit 1 ;;
esac
case "$REPLY" in
y* | Y* ) ;;
* ) exit 1 ;;
esac
fi
if [ -n "$UPGRADE" ]; then
if [ -x "${VIRTUALENV_PIP_BIN}" ]; then
"${VIRTUALENV_PIP_BIN}" freeze > "${UPGRADE_LIST}"
mv -f "${VIRTUALENV_PATH}" "${UPGRADE_PATH}"
else
echo "pyenv: pip is not installed in ${VIRTUALENV_PATH}" 1>&2
exit 1
fi
fi
fi
# Execute `before_virtualenv` hooks.
@@ -241,10 +277,32 @@ cd "${PYENV_VIRTUALENV_CACHE_PATH}"
# create symlink of `python' bound for actual executable
if [ ! -f "$VIRTUALENV_PYTHON_BIN" ]; then
if [ -f "${VIRTUALENV_PATH}/bin/$(basename "${PYTHON_BIN}")" ]; then
{
cd ${VIRTUALENV_PATH}/bin
ln -fs "$(basename "${PYTHON_BIN}")" python
}
( cd "${VIRTUALENV_PATH}/bin" && ln -fs "$(basename "${PYTHON_BIN}")" python )
fi
fi
## Migrate previously installed packages from requirements.txt
if [ -n "$UPGRADE" ]; then
UPGRADE_STATUS=0
if [ -x "${VIRTUALENV_PIP_BIN}" ]; then
"${VIRTUALENV_PIP_BIN}" install --requirement "${UPGRADE_LIST}" || UPGRADE_STATUS=$?
else
echo "pyenv: pip is not installed in ${VIRTUALENV_PATH}" 1>&2
UPGRADE_STATUS=1
fi
if [ "$UPGRADE_STATUS" == "0" ]; then
rm -f "${UPGRADE_LIST}"
rm -fr "${UPGRADE_PATH}"
else
{ echo
echo "UPGRADE FAILED"
echo
echo "Inspect or clean up the original tree at ${UPGRADE_PATH}"
echo
echo "Package list:"
cat "${UPGRADE_LIST}"
} 1>&2
STATUS="$UPGRADE_STATUS"
fi
fi

19
bin/pyenv-virtualenv-prefix Executable file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/env bash
#
# Summary: Display real_prefix for a Python virtualenv version
# Usage: pyenv virtualenv-prefix [<virtualenv>]
#
set -e
[ -n "$PYENV_DEBUG" ] && set -x
if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="${HOME}/.pyenv"
fi
if [ -n "$1" ]; then
export PYENV_VERSION="$1"
fi
PYTHON_BIN="$(pyenv-which python)"
"${PYTHON_BIN}" -c 'import sys;print(sys.real_prefix)' 2>/dev/null

42
bin/pyenv-virtualenvs Executable file
View File

@@ -0,0 +1,42 @@
#!/usr/bin/env bash
#
# Summary: List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
#
# Usage: pyenv virtualenvs [--bare]
#
set -e
[ -n "$PYENV_DEBUG" ] && set -x
if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="${HOME}/.pyenv"
fi
unset BARE
if [ "$1" = "--bare" ]; then
BARE=true
fi
print_version() {
local version="$1"
if [ -n "${BARE}" ]; then
echo "${version}"
else
echo "${version} (created from $(pyenv-virtualenv-prefix "${version}"))"
fi
}
virtualenv_version() {
local version="$1"
local prefix="$(pyenv-prefix "${version}")"
[ -f "${prefix}/bin/activate" ] && "${prefix}/bin/python" -c 'import sys;sys.real_prefix' 1>/dev/null 2>&1
}
versions=($(pyenv-versions --bare))
for version in "${versions[@]}"; do
if virtualenv_version "${version}"; then
print_version "${version}"
fi
done