From 2fc0fe7590066ba4d3cf2d10cf82595f6fd370ac Mon Sep 17 00:00:00 2001 From: Daniel Perez Date: Sun, 24 Apr 2016 18:28:48 +0900 Subject: [PATCH] Initial commit. --- README.md | 18 +++++ bin/get-version-from-legacy-file | 13 ++++ bin/install | 123 +++++++++++++++++++++++++++++++ bin/list-all | 10 +++ 4 files changed, 164 insertions(+) create mode 100644 README.md create mode 100755 bin/get-version-from-legacy-file create mode 100755 bin/install create mode 100755 bin/list-all diff --git a/README.md b/README.md new file mode 100644 index 0000000..f7fccb4 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# asdf-python + +Python plugin for [asdf](https://github.com/asdf-vm/asdf) version manager + +## Install + +``` +asdf plugin-add python https://github.com/tuvistavie/asdf-python.git +``` + +## Use + +Check [asdf](https://github.com/asdf-vm/asdf) readme for instructions on how to install & manage versions of Python. + +When installing Python using `asdf install`, you can pass custom configure options with the following env vars: + +* `PYTHON_CONFIGURE_OPTIONS` - use only your configure options +* `PYTHON_EXTRA_CONFIGURE_OPTIONS` - append these configure options along with ones that this plugin already uses diff --git a/bin/get-version-from-legacy-file b/bin/get-version-from-legacy-file new file mode 100755 index 0000000..fae55b9 --- /dev/null +++ b/bin/get-version-from-legacy-file @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +get_legacy_version() { + current_directory=$1 + 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 + fi +} + +get_legacy_version $1 diff --git a/bin/install b/bin/install new file mode 100755 index 0000000..66b5912 --- /dev/null +++ b/bin/install @@ -0,0 +1,123 @@ +#!/usr/bin/env bash + +install_python() { + local version=$1 + local install_path=$2 + + if [ "$TMPDIR" = "" ]; then + local tmp_download_dir=$(mktemp -d) + else + local tmp_download_dir=$TMPDIR + fi + + # path to the tar file + local source_path="${tmp_download_dir}/Python-${version}.tgz" + + download_source $version $source_path + + # running this in a subshell + # because we don't want to disturb current working dir + ( + cd $(dirname $source_path) + tar zxf $source_path || exit 1 + + cd "Python-${version}" + + local configure_options="$(construct_configure_options $install_path)" + + if [ "$ASDF_PKG_MISSING" != "" ]; then + echo "WARNING: Might use OS-provided pkgs for the following: $ASDF_PKG_MISSING" + fi + + # set in os_based_configure_options + # we unset it here because echo-ing changes the return value of the function + unset ASDF_PKG_MISSING + + echo "Building with options: $configure_options" + + ./configure $configure_options || exit 1 + make || exit 1 + make install || exit 1 + ) +} + +construct_configure_options() { + local install_path=$1 + + if [ "$PYTHON_CONFIGURE_OPTIONS" = "" ]; then + local configure_options="$(os_based_configure_options) --prefix=$install_path" + + if [ "$PYTHON_EXTRA_CONFIGURE_OPTIONS" != "" ]; then + configure_options="$configure_options $PYTHON_EXTRA_CONFIGURE_OPTIONS" + fi + else + local configure_options="$PYTHON_CONFIGURE_OPTIONS --prefix=$install_path" + fi + + echo "$configure_options" +} + + +homebrew_package_path() { + local package_name=$1 + + if [ "$(brew ls --versions $package_name)" = "" ]; then + echo "" + else + echo "$(brew --prefix $package_name)" + fi +} + + +exit_if_homebrew_not_installed() { + if [ "$(brew --version 2>/dev/null)" = "" ]; then + echo "ERROR: Please install homebrew for OSX" + exit 1 + fi +} + + +os_based_configure_options() { + local operating_system=$(uname -a) + local configure_options="" + + if [[ "$operating_system" =~ "Darwin" ]]; then + + exit_if_homebrew_not_installed + + local openssl_path=$(homebrew_package_path openssl) + local readline_path=$(homebrew_package_path readline) + else + local openssl_path=/usr + fi + + if [ "$openssl_path" = "" ]; then + export ASDF_PKG_MISSING="openssl" + else + configure_options="--with-openssl-dir=$openssl_path" + fi + + if [ "$readline_path" != "" ]; then + configure_options="$configure_options --with-readline-dir=$readline_path" + fi + + configure_options="$configure_options --enable-shared --disable-install-doc" + echo $configure_options +} + + +download_source() { + local version=$1 + local download_path=$2 + local download_url="https://www.python.org/ftp/python/$version/Python-$version.tgz" + + + if [ -f $download_path ]; then + rm $download_path + fi + + echo "Downloading from $download_url" + curl -#Lo $download_path $download_url +} + +install_python $ASDF_INSTALL_VERSION $ASDF_INSTALL_PATH diff --git a/bin/list-all b/bin/list-all new file mode 100755 index 0000000..eba8c97 --- /dev/null +++ b/bin/list-all @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +list_all() { + local http_endpoint="https://www.python.org/ftp/python/" + local extract_regexp='s|.*\?\([0-9].*\?\)/.*\?|\1|p' + + curl -s $http_endpoint | sed -ne "$extract_regexp" | tr '\n' ' ' +} + +list_all