#!/usr/bin/env bash # # Summary: Create a Python virtualenv using the python-virtualenv plugin # # Usage: pyenv virtualenv [-v|--verbose] [VIRTUALENV_OPTIONS] # # -v/--verbose Verbose mode: print compilation status to stdout # set -e [ -n "$PYENV_DEBUG" ] && set -x # Provide pyenv completions if [ "$1" = "--complete" ]; then exec pyenv-versions --bare fi if [ -z "$PYENV_ROOT" ]; then PYENV_ROOT="${HOME}/.pyenv" fi # Load shared library functions eval "$(python-virtualenv --lib)" usage() { # We can remove the sed fallback once pyenv 0.2.0 is widely available. pyenv-help virtualenv 2>/dev/null || sed -ne '/^#/!q;s/.//;s/.//;1,4d;p' < "$0" [ -z "$1" ] || exit "$1" } unset VERBOSE VIRTUALENV_OPTIONS=() parse_options "$@" for option in "${OPTIONS[@]}"; do case "$option" in "h" | "help" ) usage 0 ;; "v" | "verbose" ) VERBOSE="-v" ;; "version" ) exec python-virtualenv --version ;; * ) VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option" ;; esac done VERSION_NAME="${ARGUMENTS[0]}" [ -n "$VERSION_NAME" ] || usage 1 for script in $(pyenv-hooks virtualenv); do source "$script" done PYTHON_BIN=$(PYENV_VERSION="${VERSION_NAME}" pyenv-which python) if [ ! -x "${PYTHON_BIN}" ]; then echo "pyenv-virtualenv: could not obtain python executable: ${PYTHON_BIN}" >&2 exit 1 fi # find canonical name of python executable. # virtualenv will create "bin/python" executable as same name as its bootstraped python. if [ -L "${PYTHON_BIN}" ]; then while [ -L "${PYTHON_BIN}" ]; do # retrieve symlinks PYTHON_BIN="$(dirname "${PYTHON_BIN}")/$(resolve_link "${PYTHON_BIN}")" done else # python 2.6 and older don't have "bin/python" as symlink. # so we must traverse files like "bin/python*" to obtain canonical name. for python in ${PYENV_ROOT}/versions/${VERSION_NAME}/bin/python*; do if ( basename "$python" | grep '^python[0-9][0-9]*\.[0-9][0-9]*$' && cmp "$PYTHON_BIN" "$python" ) >/dev/null; then PYTHON_BIN="${python}" break fi done fi VIRTUALENV_NAME="${ARGUMENTS[1]##*/}" VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME}" python-virtualenv $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "$PYTHON_BIN" "$VIRTUALENV_PATH" pyenv rehash