下面是对脚本各部分的详细解释:
1. 初始化和设置
#!/usr/bin/env bash
set -e
[ -n "$PYENV_DEBUG" ] && set -x
#!/usr/bin/env bash
:指定脚本使用bash
解释器执行。set -e
:如果脚本中的任何命令返回非零状态(失败),脚本将立即退出。[ -n "$PYENV_DEBUG" ] && set -x
:如果环境变量PYENV_DEBUG
存在,则启用命令跟踪,以便调试脚本。
2. 设置 PYENV_ROOT
if [ -z "$PYENV_ROOT" ]; then
if [ -z "$HOME" ]; then
printf "$0: %s\n" \
"Either \$PYENV_ROOT or \$HOME must be set to determine the install location." \
>&2
exit 1
fi
export PYENV_ROOT="${HOME}/.pyenv"
fi
- 检查是否设置了
PYENV_ROOT
环境变量。如果没有,则将其设置为HOME
目录下的.pyenv
目录。
3. 定义 colorize
函数
colorize() {
if [ -t 1 ]; then printf "\e[%sm%s\e[m" "$1" "$2"
else echo -n "$2"
fi
}
colorize
函数用于在终端中打印带颜色的文本。如果标准输出是终端,则使用颜色代码,否则直接打印文本。
4. 检查 .pyenv
目录
if [ -d "${PYENV_ROOT}" ]; then
{ echo
colorize 1 "WARNING"
echo ": Can not proceed with installation. Kindly remove the '${PYENV_ROOT}' directory first."
echo
} >&2
exit 1
fi
- 如果
PYENV_ROOT
目录已经存在,则打印警告信息并退出。这样可以避免在已有的pyenv
目录中重新安装。
5. failed_checkout
和 checkout
函数
failed_checkout() {
echo "Failed to git clone $1"
exit -1
}
checkout() {
[ -d "$2" ] || git -c advice.detachedHead=0 clone --branch "$3" --depth 1 "$1" "$2" || failed_checkout "$1"
}
failed_checkout
:如果git clone
失败,打印错误信息并退出。checkout
:检查目标目录是否存在,如果不存在,则使用git clone
从指定的远程仓库克隆代码。
6. 检查 Git 和 SSH
if ! command -v git 1>/dev/null 2>&1; then
echo "pyenv: Git is not installed, can't continue." >&2
exit 1
fi
if [ -n "${USE_SSH}" ]; then
if ! command -v ssh 1>/dev/null 2>&1; then
echo "pyenv: configuration USE_SSH found but ssh is not installed, can't continue." >&2
exit 1
fi
ssh -T git@github.com 1>/dev/null 2>&1 || EXIT_CODE=$?
if [[ ${EXIT_CODE} != 1 ]]; then
echo "pyenv: github ssh authentication failed."
echo
echo "In order to use the ssh connection option, you need to have an ssh key set up."
echo "Please generate an ssh key by using ssh-keygen, or follow the instructions at the following URL for more information:"
echo
echo "> https://docs.github.com/en/repositories/creating-and-managing-repositories/troubleshooting-cloning-errors#check-your-ssh-access"
echo
echo "Once you have an ssh key set up, try running the command again."
exit 1
fi
fi
- 检查是否安装了
git
,如果没有,打印错误信息并退出。 - 如果设置了
USE_SSH
环境变量,检查是否安装了ssh
。如果安装了,验证SSH连接到GitHub的身份认证是否成功。
7. 设置 GitHub URL 和克隆插件
if [ -n "${USE_SSH}" ]; then
GITHUB="git@github.com:"
else
GITHUB="https://github.com/"
fi
checkout "${GITHUB}pyenv/pyenv.git" "${PYENV_ROOT}" "${PYENV_GIT_TAG:-master}"
checkout "${GITHUB}pyenv/pyenv-doctor.git" "${PYENV_ROOT}/plugins/pyenv-doctor" "master"
checkout "${GITHUB}pyenv/pyenv-update.git" "${PYENV_ROOT}/plugins/pyenv-update" "master"
checkout "${GITHUB}pyenv/pyenv-virtualenv.git" "${PYENV_ROOT}/plugins/pyenv-virtualenv" "master"
- 根据
USE_SSH
环境变量选择GitHub的访问方式(SSH或HTTPS)。 - 克隆
pyenv
及其插件(如pyenv-doctor
、pyenv-update
和pyenv-virtualenv
)到指定目录。
8. 检查 pyenv
是否在路径中
if ! command -v pyenv 1>/dev/null; then
{ echo
colorize 1 "WARNING"
echo ": seems you still have not added 'pyenv' to the load path."
echo
} >&2
{ # Without args, `init` commands print installation help
"${PYENV_ROOT}/bin/pyenv" init || true
"${PYENV_ROOT}/bin/pyenv" virtualenv-init || true
} >&2
fi
- 检查
pyenv
命令是否在系统路径中。如果没有,打印警告信息,并尝试打印pyenv
的初始化帮助,以便用户知道如何将pyenv
添加到加载路径中。
总结
这个脚本的目的是安装pyenv
,确保所需的工具(如git
和ssh
)已经安装,并且处理了使用ssh
和https
克隆的情况。它还检查pyenv
是否已经存在于系统路径中,并提供相关的帮助信息。