From c7a4d5d5c52490161ec5271ebf7e62673eb8ad13 Mon Sep 17 00:00:00 2001 From: Daniel Perez Date: Fri, 6 Apr 2018 17:14:25 +0900 Subject: [PATCH] Add logic to update pyenv every hour --- .gitignore | 1 + bin/install | 3 ++- bin/list-all | 3 +-- bin/utils.sh | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5444184..5867e46 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /pyenv +/pyenv_last_update diff --git a/bin/install b/bin/install index 2439a44..115120a 100755 --- a/bin/install +++ b/bin/install @@ -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" diff --git a/bin/list-all b/bin/list-all index 5129e40..c0ecb83 100755 --- a/bin/list-all +++ b/bin/list-all @@ -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' ' ' } diff --git a/bin/utils.sh b/bin/utils.sh index effc0d1..038a447 100755 --- a/bin/utils.sh +++ b/bin/utils.sh @@ -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 +}