mirror of
https://github.com/pyenv/pyenv-virtualenv.git
synced 2025-11-14 06:23:52 -05:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
695c2fd618 | ||
|
|
bc433be75c | ||
|
|
16470ec39c | ||
|
|
c8b9969619 | ||
|
|
3b7be75458 | ||
|
|
404918c7d9 | ||
|
|
20e78d8ffa | ||
|
|
db37bb9643 | ||
|
|
a9903fab6e | ||
|
|
b594b99a18 | ||
|
|
9e438ee3e2 | ||
|
|
1389d840d1 |
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,6 +1,6 @@
|
|||||||
/libexec/*.class
|
/libexec/pyenv-virtualenv/*/*.class
|
||||||
/libexec/*.pyc
|
/libexec/pyenv-virtualenv/*/*.pyc
|
||||||
/libexec/*.pyo
|
/libexec/pyenv-virtualenv/*/*.pyo
|
||||||
/libexec/__pycache__
|
/libexec/pyenv-virtualenv/*/__pycache__
|
||||||
*.swo
|
*.swo
|
||||||
*.swp
|
*.swp
|
||||||
|
|||||||
24
README.md
24
README.md
@@ -1,7 +1,7 @@
|
|||||||
# pyenv-virtualenv (a.k.a. [python-virtualenv](https://github.com/yyuu/python-virtualenv))
|
# pyenv-virtualenv (a.k.a. [python-virtualenv](https://github.com/yyuu/python-virtualenv))
|
||||||
|
|
||||||
pyenv-virtualenv is a [pyenv](https://github.com/yyuu/pyenv) plugin
|
pyenv-virtualenv is a [pyenv](https://github.com/yyuu/pyenv) plugin
|
||||||
that provides an `pyenv virtualenv` command to create virtualenv for Python
|
that provides a `pyenv virtualenv` command to create virtualenv for Python
|
||||||
on UNIX-like systems.
|
on UNIX-like systems.
|
||||||
|
|
||||||
(NOTICE: If you are an existing user of [virtualenvwrapper](http://pypi.python.org/pypi/virtualenvwrapper)
|
(NOTICE: If you are an existing user of [virtualenvwrapper](http://pypi.python.org/pypi/virtualenvwrapper)
|
||||||
@@ -15,9 +15,7 @@ to manage your virtualenvs.)
|
|||||||
Installing pyenv-virtualenv as a pyenv plugin will give you access to the
|
Installing pyenv-virtualenv as a pyenv plugin will give you access to the
|
||||||
`pyenv virtualenv` command.
|
`pyenv virtualenv` command.
|
||||||
|
|
||||||
$ mkdir -p ~/.pyenv/plugins
|
$ git clone git://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
|
||||||
$ cd ~/.pyenv/plugins
|
|
||||||
$ git clone git://github.com/yyuu/pyenv-virtualenv.git
|
|
||||||
|
|
||||||
This will install the latest development version of pyenv-virtualenv into
|
This will install the latest development version of pyenv-virtualenv into
|
||||||
the `~/.pyenv/plugins/pyenv-virtualenv` directory. From that directory, you
|
the `~/.pyenv/plugins/pyenv-virtualenv` directory. From that directory, you
|
||||||
@@ -37,9 +35,27 @@ virtualenv. For example,
|
|||||||
virtualenvs will be created into a directory of the same name
|
virtualenvs will be created into a directory of the same name
|
||||||
under `~/.pyenv/versions`.
|
under `~/.pyenv/versions`.
|
||||||
|
|
||||||
|
### Special environment variables
|
||||||
|
|
||||||
|
You can set certain environment variables to control the pyenv-virtualenv.
|
||||||
|
|
||||||
|
* `PYENV_VIRTUALENV_CACHE_PATH`, if set, specifies a directory to use for
|
||||||
|
caching downloaded package files.
|
||||||
|
* `PYENV_VIRTUALENV_SCRIPT_PATH`, if set, specified a directory to use for
|
||||||
|
storing virtualenv scripts.
|
||||||
|
* `VIRTUALENV_VERSION`, if set, forces pyenv-virtualenv to use desired
|
||||||
|
version of virtualenv. If the version has not been installed,
|
||||||
|
pyenv-virtualenv will try to download it.
|
||||||
|
|
||||||
|
|
||||||
## Version History
|
## Version History
|
||||||
|
|
||||||
|
#### 20130527
|
||||||
|
|
||||||
|
* Remove `python-virtualenv` which was no longer used.
|
||||||
|
* Change the installation path of the `virtualenv.py` script. (`./libexec` -> `./libexec/pyenv-virtualenv/${VIRTUALENV_VERSION}`)
|
||||||
|
* Download `virtualenv.py` if desired version has not been installed.
|
||||||
|
|
||||||
#### 20130507
|
#### 20130507
|
||||||
|
|
||||||
* Display virtualenv information in `--help` and `--version`
|
* Display virtualenv information in `--help` and `--version`
|
||||||
|
|||||||
@@ -7,7 +7,8 @@
|
|||||||
# pyenv virtualenv --help
|
# pyenv virtualenv --help
|
||||||
#
|
#
|
||||||
|
|
||||||
PYENV_VIRTUALENV_VERSION="20130507"
|
PYENV_VIRTUALENV_VERSION="20130527"
|
||||||
|
VIRTUALENV_VERSION="${VIRTUALENV_VERSION:-1.9.1}"
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
[ -n "$PYENV_DEBUG" ] && set -x
|
[ -n "$PYENV_DEBUG" ] && set -x
|
||||||
@@ -63,9 +64,41 @@ abs_dirname() {
|
|||||||
cd "$cwd"
|
cd "$cwd"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
http() {
|
||||||
|
local method="$1"
|
||||||
|
local url="$2"
|
||||||
|
local file="$3"
|
||||||
|
[ -n "$url" ] || return 1
|
||||||
|
|
||||||
|
if type curl &>/dev/null; then
|
||||||
|
"http_${method}_curl" "$url" "$file"
|
||||||
|
elif type wget &>/dev/null; then
|
||||||
|
"http_${method}_wget" "$url" "$file"
|
||||||
|
else
|
||||||
|
echo "error: please install \`curl\` or \`wget\` and try again" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
http_head_curl() {
|
||||||
|
curl -qsILf "$1" >&4 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
http_get_curl() {
|
||||||
|
curl -C - -o "${2:--}" -qsSLf "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
http_head_wget() {
|
||||||
|
wget -q --spider "$1" >&4 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
http_get_wget() {
|
||||||
|
wget -nv -c -O "${2:--}" "$1"
|
||||||
|
}
|
||||||
|
|
||||||
version() {
|
version() {
|
||||||
local VIRTUALENV_VERSION="$(pyenv-exec python "${VIRTUALENV}" --version || true)"
|
local version="$(pyenv-exec python "${VIRTUALENV}" --version || true)"
|
||||||
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv ${VIRTUALENV_VERSION:-unknown})"
|
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv ${version:-unknown})"
|
||||||
}
|
}
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
@@ -75,12 +108,35 @@ usage() {
|
|||||||
[ -z "$1" ] || exit "$1"
|
[ -z "$1" ] || exit "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ensure_virtualenv() {
|
||||||
|
local file="$1"
|
||||||
|
local url="$2"
|
||||||
|
[ -f "${file}" ] || {
|
||||||
|
mkdir -p "$(dirname "${file}")"
|
||||||
|
http get "${url}" "${file}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PYENV_VIRTUALENV_ROOT="$(abs_dirname "$0")/.."
|
PYENV_VIRTUALENV_ROOT="$(abs_dirname "$0")/.."
|
||||||
VIRTUALENV="${PYENV_VIRTUALENV_ROOT}/libexec/pyenv-virtualenv/virtualenv.py"
|
if [ -z "${PYENV_VIRTUALENV_CACHE_PATH}" ]; then
|
||||||
[ -f "${VIRTUALENV}" ] || VIRTUALENV="${PYENV_VIRTUALENV_ROOT}/libexec/python-virtualenv/virtualenv.py" # backward compatibility before v20130307
|
PYENV_VIRTUALENV_CACHE_PATH="${PYTHON_BUILD_CACHE_PATH:-${PYENV_ROOT}/cache}"
|
||||||
[ -f "${VIRTUALENV}" ] || VIRTUALENV="${PYENV_VIRTUALENV_ROOT}/libexec/virtualenv.py"
|
fi
|
||||||
|
if [ -z "${PYENV_VIRTUALENV_SCRIPT_PATH}" ]; then
|
||||||
|
PYENV_VIRTUALENV_SCRIPT_PATH="${PYENV_VIRTUALENV_ROOT}/libexec/pyenv-virtualenv"
|
||||||
|
fi
|
||||||
|
VIRTUALENV="${PYENV_VIRTUALENV_SCRIPT_PATH}/${VIRTUALENV_VERSION}/virtualenv.py"
|
||||||
|
VIRTUALENV_URL="https://raw.github.com/pypa/virtualenv/${VIRTUALENV_VERSION}/virtualenv.py"
|
||||||
VIRTUALENV_OPTIONS=()
|
VIRTUALENV_OPTIONS=()
|
||||||
|
|
||||||
|
ensure_virtualenv "${VIRTUALENV}" "${VIRTUALENV_URL}" || {
|
||||||
|
echo "pyenv-virtualenv: could not find virtualenv script: ${VIRTUALENV}" 1>&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Unset environment variables which starts with `VIRTUALENV_`.
|
||||||
|
# These variables are reserved for virtualenv.
|
||||||
|
unset VIRTUALENV_VERSION
|
||||||
|
|
||||||
parse_options "$@"
|
parse_options "$@"
|
||||||
for option in "${OPTIONS[@]}"; do
|
for option in "${OPTIONS[@]}"; do
|
||||||
case "$option" in
|
case "$option" in
|
||||||
@@ -97,16 +153,35 @@ for option in "${OPTIONS[@]}"; do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# The first argument contains the source version to create virtualenv.
|
||||||
VERSION_NAME="${ARGUMENTS[0]}"
|
VERSION_NAME="${ARGUMENTS[0]}"
|
||||||
[ -n "$VERSION_NAME" ] || usage 1
|
[ -n "$VERSION_NAME" ] || usage 1
|
||||||
|
|
||||||
|
|
||||||
|
# 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
|
for script in $(pyenv-hooks virtualenv); do
|
||||||
source "$script"
|
source "$script"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
echo "pyenv-virtualenv: could not obtain python executable: ${PYTHON_BIN}" >&2
|
echo "pyenv-virtualenv: could not find python executable: ${PYTHON_BIN}" 1>&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -129,15 +204,39 @@ 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"
|
||||||
|
|
||||||
|
[ -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) "
|
||||||
|
|
||||||
|
case "$REPLY" in
|
||||||
|
y* | Y* ) ;;
|
||||||
|
* ) exit 1 ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Execute `before_virtualenv` hooks.
|
||||||
|
for hook in "${before_hooks[@]}"; do eval "$hook"; done
|
||||||
|
|
||||||
|
# Plan cleanup on unsuccessful installation.
|
||||||
|
cleanup() {
|
||||||
|
[ -z "${PREFIX_EXISTS}" ] && rm -rf "$VIRTUALENV_PATH"
|
||||||
|
}
|
||||||
|
|
||||||
|
trap cleanup SIGINT
|
||||||
|
|
||||||
|
# Invoke virtualenv and record exit status in $STATUS.
|
||||||
|
STATUS=0
|
||||||
# virtualenv may download distribute/setuptools in current directory.
|
# virtualenv may download distribute/setuptools in current directory.
|
||||||
# change to cache directory to reuse them between invocation.
|
# change to cache directory to reuse them between invocation.
|
||||||
VIRTUALENV_CACHE_PATH="${PYTHON_BUILD_CACHE_PATH:-${PYENV_ROOT}/cache}"
|
mkdir -p "${PYENV_VIRTUALENV_CACHE_PATH}"
|
||||||
mkdir -p "${VIRTUALENV_CACHE_PATH}"
|
cd "${PYENV_VIRTUALENV_CACHE_PATH}"
|
||||||
{
|
"${PYTHON_BIN}" "${VIRTUALENV}" "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS=$?
|
||||||
cd "${VIRTUALENV_CACHE_PATH}"
|
|
||||||
"${PYTHON_BIN}" "${VIRTUALENV}" "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# create symlink of `python' bound for actual executable
|
# create symlink of `python' bound for actual executable
|
||||||
if [ ! -f "$VIRTUALENV_PYTHON_BIN" ]; then
|
if [ ! -f "$VIRTUALENV_PYTHON_BIN" ]; then
|
||||||
@@ -149,4 +248,14 @@ if [ ! -f "$VIRTUALENV_PYTHON_BIN" ]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
pyenv-rehash
|
# Execute `after_virtualenv` hooks
|
||||||
|
for hook in "${after_hooks[@]}"; do eval "$hook"; done
|
||||||
|
|
||||||
|
# Run `pyenv-rehash` after a successful installation.
|
||||||
|
if [ "$STATUS" == "0" ]; then
|
||||||
|
pyenv-rehash
|
||||||
|
else
|
||||||
|
cleanup
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit "$STATUS"
|
||||||
|
|||||||
@@ -1,130 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
PYTHON_VIRTUALENV_VERSION="20130507"
|
|
||||||
|
|
||||||
set -E
|
|
||||||
exec 3<&2 # preserve original stderr at fd 3
|
|
||||||
|
|
||||||
|
|
||||||
lib() {
|
|
||||||
parse_options() {
|
|
||||||
OPTIONS=()
|
|
||||||
ARGUMENTS=()
|
|
||||||
local arg option index
|
|
||||||
|
|
||||||
for arg in "$@"; do
|
|
||||||
if [ "${arg:0:1}" = "-" ]; then
|
|
||||||
if [ "${arg:1:1}" = "-" ]; then
|
|
||||||
OPTIONS[${#OPTIONS[*]}]="${arg:2}"
|
|
||||||
else
|
|
||||||
index=1
|
|
||||||
while option="${arg:$index:1}"; do
|
|
||||||
[ -n "$option" ] || break
|
|
||||||
OPTIONS[${#OPTIONS[*]}]="$option"
|
|
||||||
index=$(($index+1))
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
ARGUMENTS[${#ARGUMENTS[*]}]="$arg"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve_link() {
|
|
||||||
$(type -p greadlink readlink | head -1) "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
abs_dirname() {
|
|
||||||
local cwd="$(pwd)"
|
|
||||||
local path="$1"
|
|
||||||
|
|
||||||
while [ -n "$path" ]; do
|
|
||||||
cd "${path%/*}"
|
|
||||||
local name="${path##*/}"
|
|
||||||
path="$(resolve_link "$name" || true)"
|
|
||||||
done
|
|
||||||
|
|
||||||
pwd
|
|
||||||
cd "$cwd"
|
|
||||||
}
|
|
||||||
|
|
||||||
if [ "$1" == "--$FUNCNAME" ]; then
|
|
||||||
declare -f "$FUNCNAME"
|
|
||||||
echo "$FUNCNAME \"\$1\";"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
lib "$1"
|
|
||||||
|
|
||||||
version() {
|
|
||||||
echo "python-virtualenv ${PYTHON_VIRTUALENV_VERSION}"
|
|
||||||
}
|
|
||||||
|
|
||||||
usage() {
|
|
||||||
{ version
|
|
||||||
echo "usage: python-virtualenv [-v|--verbose] [VIRTUALENV_OPTIONS] python_bin virtualenv_path"
|
|
||||||
} >&2
|
|
||||||
|
|
||||||
if [ -z "$1" ]; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
unset VERBOSE
|
|
||||||
PYTHON_VIRTUALENV_ROOT="$(abs_dirname "$0")/.."
|
|
||||||
VIRTUALENV_OPTIONS=()
|
|
||||||
|
|
||||||
parse_options "$@"
|
|
||||||
|
|
||||||
for option in "${OPTIONS[@]}"; do
|
|
||||||
case "$option" in
|
|
||||||
"h" | "help" )
|
|
||||||
usage without_exiting
|
|
||||||
{ echo
|
|
||||||
echo " -v/--verbose Verbose mode: print compilation status to stdout"
|
|
||||||
echo
|
|
||||||
} >&2
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
"v" | "verbose" )
|
|
||||||
VERBOSE=true
|
|
||||||
;;
|
|
||||||
"version" )
|
|
||||||
version
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
* )
|
|
||||||
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
PYTHON_BIN="${ARGUMENTS[0]}"
|
|
||||||
if [ -z "${PYTHON_BIN}" ]; then
|
|
||||||
usage
|
|
||||||
elif [ ! -x "${PYTHON_BIN}" ]; then
|
|
||||||
echo "python-virtualenv: given python is not an executable: ${PYTHON_BIN}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
VIRTUALENV_PATH="${ARGUMENTS[1]}"
|
|
||||||
if [ -z "$VIRTUALENV_PATH" ]; then
|
|
||||||
usage
|
|
||||||
fi
|
|
||||||
|
|
||||||
VIRTUALENV_PYTHON_BIN="${VIRTUALENV_PATH}/bin/python"
|
|
||||||
|
|
||||||
# create virtualenv
|
|
||||||
VIRTUALENV="${PYTHON_VIRTUALENV_ROOT}/libexec/python-virtualenv/virtualenv.py"
|
|
||||||
[ -f "${VIRTUALENV}" ] || VIRTUALENV="${PYTHON_VIRTUALENV_ROOT}/libexec/virtualenv.py"
|
|
||||||
"${PYTHON_BIN}" "${VIRTUALENV}" "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_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
|
|
||||||
}
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
@@ -16,8 +16,8 @@ for file in bin/*; do
|
|||||||
cp "${file}" "${BIN_PATH}"
|
cp "${file}" "${BIN_PATH}"
|
||||||
done
|
done
|
||||||
|
|
||||||
for file in libexec/*; do
|
for file in libexec/pyenv-virtualenv/*; do
|
||||||
cp "${file}" "${LIBEXEC_PATH}"
|
cp -Rp "${file}" "${LIBEXEC_PATH}"
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Installed pyenv-virtualenv at ${PREFIX}"
|
echo "Installed pyenv-virtualenv at ${PREFIX}"
|
||||||
|
|||||||
Reference in New Issue
Block a user