asdf-python/bin/utils.sh

56 lines
1.2 KiB
Bash
Raw Permalink Normal View History

echoerr() {
printf "\033[0;31m%s\033[0m" "$1" >&2
}
ensure_python_build_installed() {
if [ ! -f "$(python_build_path)" ]; then
download_python_build
fi
}
download_python_build() {
2018-07-14 03:33:24 +01:00
echo "Downloading python-build..." >&2
2018-03-23 14:34:25 +00:00
local pyenv_url="https://github.com/pyenv/pyenv.git"
git clone $pyenv_url "$(pyenv_path)"
}
python_build_path() {
echo "$(pyenv_path)/plugins/python-build/bin/python-build"
}
2018-04-06 09:14:25 +01:00
update_python_build() {
cd "$(pyenv_path)" && git fetch && git reset --hard origin/master > /dev/null 2>&1
}
pyenv_path() {
echo "$(dirname $(dirname $0))/pyenv"
}
2018-04-06 09:14:25 +01:00
pyenv_update_timestamp_path() {
echo "$(dirname $(dirname "$0"))/pyenv_last_update"
}
pyenv_should_update() {
update_timeout=3600
update_timestamp_path=$(pyenv_update_timestamp_path)
if [ ! -f "$update_timestamp_path" ]; then
return 0
fi
last_update=$(cat "$update_timestamp_path")
current_timestamp=$(date +%s)
invalidated_at=$(($last_update + $update_timeout))
[ $invalidated_at -lt $current_timestamp ]
}
install_or_update_python_build() {
if [ ! -f "$(python_build_path)" ]; then
download_python_build
elif pyenv_should_update; then
update_python_build
date +%s > "$(pyenv_update_timestamp_path)"
fi
}