Skip to content

Instantly share code, notes, and snippets.

@mattmess1221
Created August 16, 2022 00:51
Show Gist options
  • Save mattmess1221/0d949a83df55e4172bc5ce9f6ebd169d to your computer and use it in GitHub Desktop.
Save mattmess1221/0d949a83df55e4172bc5ce9f6ebd169d to your computer and use it in GitHub Desktop.
#!/bin/bash
# .pyenv/plugins/install-latest-python/bin/pyenv-install-latest
# Summary: Install the latest version of a python release
#
# Usage: pyenv install-latest [version]
#
# Description:
# This command will pick the latest version as returned from pyenv version --list.
# If an argument is provided, it will be used as a filter.
#
# Arguments:
# version - the minor version to install. If not provided, will use the latest version
#
# Examples:
# pyenv install-latest
# pyenv install-latest 3.11
# pyenv install-latest 2.7
# pyenv install-latest anaconda3
set -e
[ -n "$PYENV_DEBUG" ] && set -x
if [ "$#" -gt 1 ]; then
pyenv-help --usage pyenv-install >&2
exit 1
fi
quote() {
python -c 'import re;print(re.escape(input()))' <<< "$1"
}
pylatest() {
pattern=$1
if [ -z "$pattern" ]; then
pattern='^\d+\.\d+\.\d+(?!b\d+)$'
fi
pyenv-install --list | xargs -n 1 | grep -P "$pattern" | sort -V | tail -n 1
}
pyresolve() {
local quoted resolved
quoted=$(quote "$1")
resolved=$(pylatest "^${quoted}[.$-]")
if [ -z "$resolved" ]; then
echo "Could not resolve '$1' to a python version" >&2
return 2
fi
echo "$resolved"
}
version=$1
if [ -z "$version" ]; then
resolved=$(pylatest)
else
resolved=$(pyresolve "$version")
fi
echo -n "Install python $resolved? (y/N) "
read -r choice
if [ "$choice" = "y" ]; then
pyenv-install "$resolved"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment