Configure Python develop tool chain

- use pyenv to manage the develop environments
  + install several Python versions (3.7 - 3.10 and 2.7)
  + each version receives its own copies of black, pipenv, and poetry
- add two more virtual environments based off the latest version:
  + "interactive" => default environment optimized for interactive
                     usage with with black, bpython, and ipython
                     (also receives accidental `pip install`s)
  + "utils" => hosts various globally available tools/apps
               (e.g., mackup and youtube-dl)
- add installation and update scripts for the entire tool chain
- set up completions for bash and zsh
- set up convenient aliases
- configure bpython
- configure poetry
This commit is contained in:
Alexander Hess 2022-08-09 13:53:43 +02:00
commit 0319e614b8
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
9 changed files with 233 additions and 1 deletions

View file

@ -86,6 +86,25 @@ alias more='less'
alias tree='tree -C --dirsfirst'
# Make working with Python more convenient
# Interactive shells
alias py='python'
alias bpy='bpython'
alias ipy='ipython'
if _command_exists poetry; then
alias pr='poetry run'
fi
if _command_exists pyenv; then
alias pyvenvs='pyenv virtualenvs --bare --skip-aliases'
alias pyver='pyenv version'
alias pyvers='pyenv versions --skip-aliases'
alias pywhich='pyenv which'
fi
# Various one-line utilities
alias datetime='date +"%Y-%m-%d %H:%M:%S %z (%Z)"'
alias datetime-iso='date --iso-8601=seconds'

View file

@ -9,6 +9,15 @@ _in_zsh() {
[ -n "$ZSH_VERSION" ]
}
prepend-to-path () { # if not already there
if [ -d "$1" ] ; then
case :$PATH: in
*:$1:*) ;;
*) PATH=$1:$PATH ;;
esac
fi
}
# Configure the keyboard:
@ -19,6 +28,16 @@ _command_exists xcape && xcape -e "Caps_Lock=Escape"
_init_pyenv () { # used further below as well
_command_exists pyenv || return
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
}
_init_pyenv
# ==============================
# Working with files and folders
# ==============================
@ -174,6 +193,94 @@ genemail() {
# ===================================================
# Set up & maintain the Python (develop) environments
# ===================================================
# TODO: This needs to be updated regularly (or find an automated solution)
# The Python versions pyenv creates (in descending order
# Important: The first version also holds the "interactive" and "utils" environments)
_py3_versions=('3.10.6' '3.9.13' '3.8.13' '3.7.13')
_py2_version='2.7.18'
# Each Python version receives its own copy of black, pipenv, and poetry
# (e.g., to avoid possible integration problems between pyenv and poetry
# Source: https://github.com/python-poetry/poetry/issues/5252#issuecomment-1055697424)
_py3_site_packages=('black' 'pipenv' 'poetry')
# The pyenv virtualenv "utils" contains some globally available tools (e.g., mackup)
_py3_utils=('mackup' 'youtube-dl')
# Important: this REMOVES the old ~/.pyenv installation
_install_pyenv() {
echo "(Re-)Installing pyenv"
# Ensure that pyenv is on the $PATH
# (otherwise, the pyenv installer emits warnings)
mkdir -p "$PYENV_ROOT/bin"
prepend-to-path "$PYENV_ROOT/bin"
# Remove old pyenv for clean install
rm -rf "$PYENV_ROOT" >/dev/null
# Run the official pyenv installer
curl https://pyenv.run | bash
# Make pyenv usable after this installation in the same shell session
_init_pyenv
}
create-or-update-python-envs() {
_command_exists pyenv || _install_pyenv
eval "$(pyenv init --path)"
# Keep a legacy Python 2.7, just in case
echo "Installing/updating Python $_py2_version"
pyenv install --skip-existing $_py2_version
pyenv rehash # needed on a first install
PYENV_VERSION=$_py2_version pip install --upgrade pip setuptools
PYENV_VERSION=$_py2_version python -c "import sys; print sys.version"
for version in ${_py3_versions[@]}; do
echo "Installing/updating Python $version"
pyenv install --skip-existing $version
pyenv rehash # needed on a first install
# Start the new environment with the latest pip and setuptools versions
PYENV_VERSION=$version pip install --upgrade pip setuptools
PYENV_VERSION=$version python -c "import sys; print(sys.version)"
# Put the specified utilities in the fresh environments or update them
for lib in ${_py3_site_packages[@]}; do
PYENV_VERSION=$version pip install --upgrade $lib
done
done
# Create a virtualenv based off the latest Python version to host global utilities
echo "Installing/updating the global Python utilities"
pyenv virtualenv $_py3_versions[1] 'utils'
pyenv rehash # needed on a first install
PYENV_VERSION='utils' pip install --upgrade pip setuptools
for util in ${_py3_utils[@]}; do
PYENV_VERSION='utils' pip install --upgrade $util
done
# Create a virtualenv based off the latest Python version for interactive usage
echo "Installing/updating the default/interactive Python environment"
pyenv virtualenv $_py3_versions[1] 'interactive'
pyenv rehash # needed on a first install
PYENV_VERSION='interactive' pip install --upgrade pip setuptools
# Install some tools to make interactive usage nicer
PYENV_VERSION='interactive' pip install --upgrade black bpython ipython
# Put all Python binaries/virtualenvs and the utilities on the $PATH
pyenv global 'interactive' $_py3_versions 'utils' $_py2_version
}
# =============================
# Automate the update machinery
# =============================
@ -280,6 +387,21 @@ _update_zplug() {
}
_update_python() {
echo 'Updating the Python tool chain'
if _command_exists pyenv; then
pyenv update
create-or-update-python-envs
fi
if _command_exists zsh-pip-cache-packages; then
zsh-pip-clear-cache
zsh-pip-cache-packages
fi
}
run-private-scripts() { # in the Nextcloud
if [ -d "$HOME/data/getraenkemarkt/shell" ]; then
for file in $HOME/data/getraenkemarkt/shell/*.sh; do
@ -298,6 +420,7 @@ update-machine() {
_command_exists snap && sudo snap refresh && _remove_old_snaps
_update_repositories
_update_zsh
_update_python
sudo --reset-timestamp
}