From 331e65d41982cd24b37782f813635e2f9baa541e Mon Sep 17 00:00:00 2001 From: Daniel Perez Date: Sun, 22 Jan 2017 18:31:29 +0900 Subject: [PATCH] Avoid redownloading python-build, use asdf_dir instead of hard-coded path, close #11 --- .gitignore | 1 + bin/get-version-from-legacy-file | 10 +++++----- bin/install | 19 ++++++------------- bin/list-all | 14 ++++---------- bin/utils.sh | 19 +++++++++++++++++++ shims/python | 6 +++--- 6 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 .gitignore create mode 100644 bin/utils.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5444184 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/pyenv diff --git a/bin/get-version-from-legacy-file b/bin/get-version-from-legacy-file index fae55b9..e423a07 100755 --- a/bin/get-version-from-legacy-file +++ b/bin/get-version-from-legacy-file @@ -1,13 +1,13 @@ #!/usr/bin/env bash get_legacy_version() { - current_directory=$1 - python_version_file="$current_directory/.python-version" + local current_directory="$1" + local python_version_file="$current_directory/.python-version" # Get version from .python-version file. .python-version is used by pyenv - if [ -f $python_version_file ]; then - cat $python_version_file + if [ -f "$python_version_file" ]; then + cat "$python_version_file" fi } -get_legacy_version $1 +get_legacy_version "$1" diff --git a/bin/install b/bin/install index 6d02b90..c16eecb 100755 --- a/bin/install +++ b/bin/install @@ -1,27 +1,20 @@ #!/usr/bin/env bash +source $(dirname $0)/utils.sh + install_python() { local version="$1" local install_path="$2" - local tmp_download_dir="$(mktemp -d -t asdf-python.XXX)" + local tmp_dir="$(mktemp -d -t asdf-python.XXX)" - echo "Downloading python-build..." - download_python_build $tmp_download_dir > /dev/null 2>&1 - - local python_build="$tmp_download_dir/pyenv/plugins/python-build/bin/python-build" + ensure_python_build_installed ASDF_CONCURRENCY="${ASDF_CONCURRENCY:-1}" echo "python-build $version $install_path" - MAKE_OPTS="$MAKE_OPTS -j$ASDF_CONCURRENCY" TMP_DIR="$tmp_download_dir" $python_build $version $install_path + MAKE_OPTS="$MAKE_OPTS -j$ASDF_CONCURRENCY" TMP_DIR="$tmp_dir" $(python_build_path) $version $install_path echo "Cleaning up" - rm -rf "$tmp_download_dir" -} - -download_python_build() { - local tmp_dir="$1" - local pyenv_url="git://github.com/yyuu/pyenv.git" - git clone $pyenv_url $tmp_dir/pyenv + rm -rf "$tmp_dir" } install_python $ASDF_INSTALL_VERSION $ASDF_INSTALL_PATH diff --git a/bin/list-all b/bin/list-all index f208cd0..0f12fee 100755 --- a/bin/list-all +++ b/bin/list-all @@ -1,17 +1,11 @@ #!/usr/bin/env bash +source $(dirname $0)/utils.sh + list_all() { - local tmp_download_dir="$(mktemp -d -t asdf-python.XXX)" - download_python_build $tmp_download_dir > /dev/null 2>&1 - local python_build="$tmp_download_dir/pyenv/plugins/python-build/bin/python-build" - $python_build --definitions | tr '\n' ' ' + ensure_python_build_installed + $(python_build_path) --definitions | tr '\n' ' ' rm -rf "$tmp_download_dir" } -download_python_build() { - local tmp_dir="$1" - local pyenv_url="git://github.com/yyuu/pyenv.git" - git clone $pyenv_url $tmp_dir/pyenv -} - list_all diff --git a/bin/utils.sh b/bin/utils.sh new file mode 100644 index 0000000..c05cbf7 --- /dev/null +++ b/bin/utils.sh @@ -0,0 +1,19 @@ +ensure_python_build_installed() { + if [ ! -f "$(python_build_path)" ]; then + download_python_build + fi +} + +download_python_build() { + echo "Downloading python-build..." + local pyenv_url="git://github.com/yyuu/pyenv.git" + git clone $pyenv_url "$(pyenv_path)" +} + +python_build_path() { + echo "$(pyenv_path)/plugins/python-build/bin/python-build" +} + +pyenv_path() { + echo "$(dirname $(dirname $0))/pyenv" +} diff --git a/shims/python b/shims/python index 039305b..8de8e04 100755 --- a/shims/python +++ b/shims/python @@ -3,9 +3,9 @@ source $(dirname $(dirname $0))/lib/utils.sh run_python() { - plugin_name="python" + local plugin_name="python" - full_version=$(get_preset_version_for $plugin_name) + local full_version=$(get_preset_version_for $plugin_name) if [ "$full_version" == "" ]; then echo "No version set for ${plugin_name}" @@ -16,7 +16,7 @@ run_python() { local short_python_version="${splitted_version[0]}.${splitted_version[1]}" - exec $HOME/.asdf/bin/private/asdf-exec python bin/python${short_python_version} "$@" + exec $(asdf_dir)/bin/private/asdf-exec python bin/python${short_python_version} "$@" } run_python "$@"