Add logic to update pyenv every hour

This commit is contained in:
Daniel Perez 2018-04-06 17:14:25 +09:00
parent f7c594eedc
commit c7a4d5d5c5
4 changed files with 36 additions and 3 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/pyenv
/pyenv_last_update

View file

@ -2,7 +2,7 @@
set -e
source "$(dirname "$0")"/utils.sh
source "$(dirname "$0")/utils.sh"
install_python() {
local install_type=$1
@ -14,6 +14,7 @@ install_python() {
echoerr "For a list of available versions, see \`asdf list-all python\`."
exit 1
fi
install_or_update_python_build
echo "python-build $version $install_path"
$(python_build_path) "$version" "$install_path"

View file

@ -3,8 +3,7 @@
source "$(dirname "$0")/utils.sh"
list_all() {
ensure_python_build_installed
install_or_update_python_build
$(python_build_path) --definitions | tr '\n' ' '
}

View file

@ -18,6 +18,38 @@ python_build_path() {
echo "$(pyenv_path)/plugins/python-build/bin/python-build"
}
update_python_build() {
cd "$(pyenv_path)" && git fetch && git reset --hard origin/master > /dev/null 2>&1
}
pyenv_path() {
echo "$(dirname $(dirname $0))/pyenv"
}
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
}