add --upgrade option to upgrade existing virtualenv

This commit is contained in:
Yamashita Yuu
2013-05-28 13:43:17 +09:00
parent f9532f6c4e
commit 8ac27a7a77

View File

@@ -2,10 +2,11 @@
# #
# Summary: Create a Python virtualenv using the pyenv-virtualenv plugin # Summary: Create a Python virtualenv using the pyenv-virtualenv plugin
# #
# Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] <version> <virtualenv-name> # Usage: pyenv virtualenv [-f|--force] [-u|--upgrade] [VIRTUALENV_OPTIONS] <version> <virtualenv-name>
# pyenv virtualenv --version # pyenv virtualenv --version
# pyenv virtualenv --help # pyenv virtualenv --help
# #
# -u/--upgrade Upgrade existing virtualenv with migrating installed packages
# -f/--force Install even if the version appears to be installed already # -f/--force Install even if the version appears to be installed already
# #
@@ -136,6 +137,7 @@ ensure_virtualenv "${VIRTUALENV}" "${VIRTUALENV_URL}" || {
} }
unset FORCE unset FORCE
unset UPGRADE
# Unset environment variables which starts with `VIRTUALENV_`. # Unset environment variables which starts with `VIRTUALENV_`.
# These variables are reserved for virtualenv. # These variables are reserved for virtualenv.
unset VIRTUALENV_VERSION unset VIRTUALENV_VERSION
@@ -149,6 +151,9 @@ for option in "${OPTIONS[@]}"; do
"h" | "help" ) "h" | "help" )
usage 0 usage 0
;; ;;
"u" | "upgrade" )
UPGRADE=true
;;
"version" ) "version" )
version version
exit 0 exit 0
@@ -163,6 +168,17 @@ done
VERSION_NAME="${ARGUMENTS[0]}" VERSION_NAME="${ARGUMENTS[0]}"
[ -n "$VERSION_NAME" ] || usage 1 [ -n "$VERSION_NAME" ] || usage 1
if [ -z "$TMPDIR" ]; then
TMP="/tmp"
else
TMP="${TMPDIR%/}"
fi
SEED="$(date "+%Y%m%d%H%M%S").$$"
UPGRADE_PATH="${TMP}/pyenv-virtualenv.${SEED}"
UPGRADE_REQUIREMENTS="${UPGRADE_PATH}/requirements.txt"
PYTHON_BIN=$(PYENV_VERSION="${VERSION_NAME}" pyenv-which python) PYTHON_BIN=$(PYENV_VERSION="${VERSION_NAME}" pyenv-which python)
if [ ! -x "${PYTHON_BIN}" ]; then if [ ! -x "${PYTHON_BIN}" ]; then
@@ -190,6 +206,7 @@ fi
VIRTUALENV_NAME="${ARGUMENTS[1]##*/}" VIRTUALENV_NAME="${ARGUMENTS[1]##*/}"
VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME}" VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME}"
VIRTUALENV_PYTHON_BIN="${VIRTUALENV_PATH}/bin/python" VIRTUALENV_PYTHON_BIN="${VIRTUALENV_PATH}/bin/python"
VIRTUALENV_PIP_BIN="${VIRTUALENV_PATH}/bin/pip"
# Define `before_virtualenv` and `after_virtualenv` functions that allow # Define `before_virtualenv` and `after_virtualenv` functions that allow
@@ -217,14 +234,22 @@ done
# If the virtualenv exists, prompt for confirmation unless # If the virtualenv exists, prompt for confirmation unless
# the --force option was specified. # the --force option was specified.
if [ -z "$FORCE" ] && [ -d "${VIRTUALENV_PATH}/bin" ]; then if [ -d "${VIRTUALENV_PATH}/bin" ]; then
echo "pyenv: $VIRTUALENV_PATH already exists" >&2 if [ -z "$FORCE" ]; then
echo "pyenv: ${VIRTUALENV_PATH} already exists" 1>&2
read -p "continue with installation? (y/N) " read -p "continue with installation? (y/N) "
case "$REPLY" in case "$REPLY" in
y* | Y* ) ;; y* | Y* ) ;;
* ) exit 1 ;; * ) exit 1 ;;
esac esac
fi
if [ -n "$UPGRADE" ]; then
mkdir -p "${UPGRADE_PATH}"
[ -x "${VIRTUALENV_PIP_BIN}" ] && "${VIRTUALENV_PIP_BIN}" freeze > "${UPGRADE_REQUIREMENTS}"
mv -f "${VIRTUALENV_PATH}" "${UPGRADE_PATH}"
fi
fi fi
# Execute `before_virtualenv` hooks. # Execute `before_virtualenv` hooks.
@@ -255,6 +280,14 @@ if [ ! -f "$VIRTUALENV_PYTHON_BIN" ]; then
fi fi
fi fi
## Migrate previously installed packages from requirements.txt
if [ -n "$UPGRADE" ]; then
[ -x "${VIRTUALENV_PIP_BIN}" ] && [ -f "${UPGRADE_REQUIREMENTS}" ] && {
"${VIRTUALENV_PIP_BIN}" install --requirement "${UPGRADE_REQUIREMENTS}"
rm -fr "${UPGRADE_PATH}"
}
fi
# Execute `after_virtualenv` hooks # Execute `after_virtualenv` hooks
for hook in "${after_hooks[@]}"; do eval "$hook"; done for hook in "${after_hooks[@]}"; do eval "$hook"; done