From a953a1a6b94f0860cc5d05ad2304ab5752f19332 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Fri, 25 Sep 2020 16:33:56 -0500 Subject: [PATCH] clean up pyenv and python resources --- completion/available/pyenv.completion.bash | 6 +++++ plugins/available/pyenv.plugin.bash | 30 +++++++++++++++++----- plugins/available/python.plugin.bash | 13 +++++++--- 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 completion/available/pyenv.completion.bash diff --git a/completion/available/pyenv.completion.bash b/completion/available/pyenv.completion.bash new file mode 100644 index 00000000..12420854 --- /dev/null +++ b/completion/available/pyenv.completion.bash @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +# Bash completion support for pyenv + +if _command_exists pyenv && [[ -r "$(dirname $(readlink -f $(which pyenv)))/../completions/pyenv.bash" ]] ; then + source "$(dirname $(readlink -f $(which pyenv)))/../completions/pyenv.bash" +fi diff --git a/plugins/available/pyenv.plugin.bash b/plugins/available/pyenv.plugin.bash index 4d8db4fb..8b41fa37 100644 --- a/plugins/available/pyenv.plugin.bash +++ b/plugins/available/pyenv.plugin.bash @@ -1,12 +1,30 @@ cite about-plugin about-plugin 'load pyenv, if you are using it' -export PYENV_ROOT="$HOME/.pyenv" -pathmunge "$PYENV_ROOT/bin" +# Load after basher +# BASH_IT_LOAD_PRIORITY: 275 -[[ `which pyenv 2>/dev/null` ]] && eval "$(pyenv init - bash)" +# Don't modify the environment if we can't find the tool: +# - Check if in $PATH already +# - Check if installed manually to $PYENV_ROOT +# - Check if installed manually to $HOME +_command_exists pyenv || + [[ -n "$PYENV_ROOT" && -x "$PYENV_ROOT/bin/pyenv" ]] || + [[ -x "$HOME/.pyenv/bin/pyenv" ]] || + return 0 -#Load pyenv virtualenv if the virtualenv plugin is installed. -if pyenv virtualenv-init - &> /dev/null; then - eval "$(pyenv virtualenv-init - bash)" +# Set PYENV_ROOT, if not already set +export PYENV_ROOT="${PYENV_ROOT:-$HOME/.pyenv}" + +# Add PYENV_ROOT/bin to PATH, if that's where it's installed +if ! _command_exists pyenv && [[ -x "$PYENV_ROOT/bin/pyenv" ]] ; then + pathmunge "$PYENV_ROOT/bin" fi + +# Initialize pyenv +eval "$(pyenv init - bash)" + +# Load pyenv virtualenv if the plugin is installed +! _command_exists 'pyenv virtualenv --help' \ + "pyenv plugin 'virtualenv' is not installed - skipping" || + eval "$(pyenv virtualenv-init - bash)" diff --git a/plugins/available/python.plugin.bash b/plugins/available/python.plugin.bash index ceba6268..ef83dcee 100644 --- a/plugins/available/python.plugin.bash +++ b/plugins/available/python.plugin.bash @@ -1,8 +1,13 @@ cite about-plugin about-plugin 'alias "shttp" to SimpleHTTPServer' -if [ $(uname) = "Linux" ] -then +# Load after pyenv +# BASH_IT_LOAD_PRIORITY: 285 + +# Check python version to ensure pyenv can find python +{ _command_exists python && python --version &>/dev/null ; } || return 0 + +if [[ "$(uname -s)" == 'Linux' ]] ; then alias shttp='python2 -m SimpleHTTPServer' else alias shttp='python -m SimpleHTTPServer' @@ -14,9 +19,9 @@ function pyedit() { example '$ pyedit requests' group 'python' - xpyc=`python -c "import os, sys; f = open(os.devnull, 'w'); sys.stderr = f; module = __import__('$1'); sys.stdout.write(module.__file__)"` + xpyc=$(python -c "import os, sys; f = open(os.devnull, 'w'); sys.stderr = f; module = __import__('$1'); sys.stdout.write(module.__file__)") - if [ "$xpyc" == "" ]; then + if [[ "$xpyc" == "" ]]; then echo "Python module $1 not found" return -1