Make asdf-python a wrapper of python-build

This commit is contained in:
Daniel Perez 2016-08-30 15:25:56 +09:00
parent 90c040bcbf
commit 286cfcd5fe
3 changed files with 28 additions and 126 deletions

View file

@ -14,7 +14,6 @@ asdf plugin-add python https://github.com/tuvistavie/asdf-python.git
Check [asdf](https://github.com/asdf-vm/asdf) readme for instructions on how to install & manage versions of Python. 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: Under the hood, asdf-python uses [python-build](https://github.com/yyuu/pyenv/tree/master/plugins/python-build)
to build and install Python, check its [README](https://github.com/yyuu/pyenv/tree/master/plugins/python-build)
* `PYTHON_CONFIGURE_OPTIONS` - use only your configure options for more information about build options.
* `PYTHON_EXTRA_CONFIGURE_OPTIONS` - append these configure options along with ones that this plugin already uses

View file

@ -1,130 +1,27 @@
#!/usr/bin/env bash #!/usr/bin/env bash
install_python() { install_python() {
local version=$1 local version="$1"
local install_path=$2 local install_path="$2"
if [ "$TMPDIR" = "" ]; then local tmp_download_dir="$(mktemp -d -t asdf-python.XXX)"
local tmp_download_dir=$(mktemp -d)
else
local tmp_download_dir=$TMPDIR
fi
# path to the tar file echo "Downloading python-build..."
local source_path="${tmp_download_dir}/Python-${version}.tgz" download_python_build $tmp_download_dir > /dev/null 2>&1
echo "Downloading Python $version" local python_build="$tmp_download_dir/pyenv/plugins/python-build/bin/python-build"
local full_version=$(download_source $version $source_path)
# running this in a subshell ASDF_CONCURRENCY="${ASDF_CONCURRENCY:-1}"
# because we don't want to disturb current working dir echo "python-build $version $install_path"
( MAKE_OPTS="$MAKE_OPTS -j$ASDF_CONCURRENCY" TMP_DIR="$tmp_download_dir" $python_build $version $install_path
cd $(dirname $source_path) echo "Cleaning up"
tar zxf $source_path || exit 1 rm -rf "$tmp_download_dir"
cd $full_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
ASDF_CONCURRENCY="${ASDF_CONCURRENCY:-1}"
echo "Building with options: $configure_options"
./configure $configure_options || exit 1
make -j $ASDF_CONCURRENCY || exit 1
make -j $ASDF_CONCURRENCY install || exit 1
)
} }
construct_configure_options() { download_python_build() {
local install_path=$1 local tmp_dir="$1"
local pyenv_url="git://github.com/yyuu/pyenv.git"
if [ "$PYTHON_CONFIGURE_OPTIONS" = "" ]; then git clone $pyenv_url $tmp_dir/pyenv
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_base_url="https://www.python.org/ftp/python/$version/"
local full_version=$(curl -s $download_base_url | grep -Eo 'href="[^"]+.tgz' | head -n 1)
full_version=${full_version:6}
local download_url="$download_base_url$full_version"
if [ -f $download_path ]; then
rm $download_path
fi
curl -#Lo $download_path $download_url
echo "$full_version" | sed -e 's/\.tgz//'
} }
install_python $ASDF_INSTALL_VERSION $ASDF_INSTALL_PATH install_python $ASDF_INSTALL_VERSION $ASDF_INSTALL_PATH

View file

@ -1,11 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
list_all() { list_all() {
local http_endpoint="https://www.python.org/ftp/python/" local tmp_download_dir="$(mktemp -d -t asdf-python.XXX)"
local extract_regexp='s|<a href="([0-9](\.[0-9]){1,2})/".*|\1|p' download_python_build $tmp_download_dir > /dev/null 2>&1
local black_list='2.0' local python_build="$tmp_download_dir/pyenv/plugins/python-build/bin/python-build"
$python_build --definitions
rm -rf "$tmp_download_dir"
}
curl -s $http_endpoint | sed -E -ne "$extract_regexp" | grep -vE "^($black_list)$" | tr '\n' ' ' 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 list_all