Skip to content

Instantly share code, notes, and snippets.

@markfaine
Last active March 7, 2019 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markfaine/1530331f50a6c70f69bb7753fe9ad68e to your computer and use it in GitHub Desktop.
Save markfaine/1530331f50a6c70f69bb7753fe9ad68e to your computer and use it in GitHub Desktop.
Installs pyenv, virtualenvwrapper, pyenv-virtualenvwrapper, pyenv-default-packages, and installs python3 with pyenv
#shellcheck shell=bash source=/dev/null
# This script is sourced in order to setup python3 with pyenv/virtualenvwrapper with the
# pyenv-virtualenvwrapper plugin. The pyenv-default-packages plugin is also installed
# to install ansible and dependencies. This is for a service account to be used by admins
# who aren't experienced with python ecosystem it must also not modify or use the
# system python environment.
# function: _install_os_deps: installs operating system package dependencies
function _install_os_deps(){
if yum list installed zlib-devel bzip2 bzip2-devel readline-devel sqlite \
sqlite-devel openssl-devel xz xz-devel libffi-devel findutils >/dev/null 2>&1; then
return
else
if sudo yum install -y zlib-devel bzip2 bzip2-devel readline-devel sqlite \
sqlite-devel openssl-devel xz xz-devel libffi-devel findutils >/dev/null 2>&1; then # needed for python builds
return 0
fi
fi
}
# function: _install_pyenv: installs python version manager
function _install_pyenv() {
if [[ -d ~/.pyenv ]]; then
return
else
echo "==> Installing pyenv and dependencies..."
echo "==> This process should only occur once unless ~/.pyenv is deleted"
if ! _install_os_deps; then
echo "==> Could not install python build dependencies!"
return $?
fi
if git clone https://github.com/pyenv/pyenv.git "$HOME/.pyenv" >/dev/null 2>&1; then
echo "==> Success"
else
return $?
fi
fi
}
# function: _install_pyenv-default-packages: installs plugin to install default packages automatically
function _install_pyenv-default-packages(){
if [[ -d "${PYENV_ROOT}/plugins/pyenv-default-packages" ]]; then
return
else
echo "==> Installing pyenv-default-packages plugin..."
echo "==> This process should only occur once unless ~/.pyenv/plugins/pyenv-default-packages is deleted"
# Install pyenv virtualenvwrapper plugin
if git clone https://github.com/jawshooah/pyenv-default-packages.git "$HOME/.pyenv/plugins/pyenv-default-packages" >/dev/null 2>&1; then
PATH="$PATH:${PYENV_ROOT}/plugins/pyenv-default-packages/libexec"; export PATH
echo -en "ansible\npywinrm[kerberos]>=0.3.0\n" > "$(pyenv root)/default-packages"
echo "==> Success"
fi
fi
}
# function: _install_python
function _install_python() {
# Install python 3.7.2 if not already installed, python is needed for virtualenvwraper
if pyenv which python3.7 >/dev/null 2>&1; then
return
else
echo "==> Installing local python 3.7.2 using pyenv"
CFLAGS='-O2'; export CFLAGS
if ! pyenv install 3.7.2 >/dev/null 2>&1; then
return $?
fi
fi
}
# function: _install_pyenv_wrapper: installs plugin to combine pyenv and virtualenvwrapper functions
function _install_pyenv_wrapper(){
if [[ -d "${PYENV_ROOT}/plugins/pyenv-virtualenvwrapper" ]]; then
return
else
echo "==> Installing pyenv-virtualenvwrapper plugin..."
echo "==> This process should only occur once unless ~/.pyenv/plugins/pyenv-virtualenvwrapper is deleted"
# Install pyenv virtualenvwrapper plugin
if git clone https://github.com/pyenv/pyenv-virtualenvwrapper.git "$HOME/.pyenv/plugins/pyenv-virtualenvwrapper" >/dev/null 2>&1; then
PATH="$PATH:${PYENV_ROOT}/plugins/pyenv-virtualenvwrapper/bin"; export PATH
echo "==> Success"
else
return $?
fi
fi
}
#function: virtualenvwrapper: setup virtualenvwrapper
function _pyenv_virtualenvwrapper() {
if [[ ! -d "${PYENV_ROOT}/plugins/pyenv-virtualenvwrapper" ]]; then
return
else
WORKON_HOME=~/.virtualenvs; export VIRTUALENVWRAPPER_VIRTUALENV_ARGS
PROJECT_HOME=~/projects; export PROJECT_HOME
VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'; export WORKON_HOME
mkdir -p "${WORKON_HOME}"
mkdir -p "${PROJECT_HOME}"
pyenv virtualenvwrapper
fi
}
# If ~/.local exist delete it
if [[ -d ~/.local ]]; then
rm -rf ~/.local
fi
# Install pyenv
_install_pyenv || return
PYENV_ROOT="$HOME/.pyenv"; export PYENV_ROOT
PATH="${PYENV_ROOT}/bin:$PATH"; export PATH
# Install plugins for pyenv
_install_pyenv-default-packages || return
_install_pyenv_wrapper || return
# Enable shims and autocompletion
eval "$(pyenv init -)" || return
# Install python
_install_python || return
pyenv global 3.7.2 >/dev/null 2>&1 # set default python installation
pyenv shell 3.7.2 # set shell python version
# Initialize pyenv-virtualenvwrapper
_pyenv_virtualenvwrapper || return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment