From 8934fec5ba7645077cd4bc0d6e516af2517562b5 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Tue, 26 Jul 2016 17:51:21 -0700 Subject: [PATCH] Start using pyenv This required a significant refactor to shellenv.sh. Some strange, and not yet fully understood behavior of add_to_path was uncovered in doing this refactoring. More investigation is needed. --- dotfiles/cider/bootstrap.json | 1 + dotfiles/lib/python/shell_path.py | 11 +++ dotfiles/lib/shellenv.sh | 119 +++++++++++++++++++++--------- 3 files changed, 95 insertions(+), 36 deletions(-) diff --git a/dotfiles/cider/bootstrap.json b/dotfiles/cider/bootstrap.json index e96e21f9..7e3b658a 100644 --- a/dotfiles/cider/bootstrap.json +++ b/dotfiles/cider/bootstrap.json @@ -135,6 +135,7 @@ "plantuml", "pstree", "pv", + "pyenv", "pypy", "python", "python3", diff --git a/dotfiles/lib/python/shell_path.py b/dotfiles/lib/python/shell_path.py index ac373fdb..06a50af3 100755 --- a/dotfiles/lib/python/shell_path.py +++ b/dotfiles/lib/python/shell_path.py @@ -31,6 +31,17 @@ class PathList(object): ) def add(self, new_paths, after=False, target=None): + + # Remove the path if it already exists in self.paths to ensure + # that the new placement takes precedence + for path in new_paths: + done = False + while not done: + try: + self.paths.remove(path) + except: + done = True + if target: target_index = self.paths.index(target) else: diff --git a/dotfiles/lib/shellenv.sh b/dotfiles/lib/shellenv.sh index 1aab4229..e8e71b3a 100644 --- a/dotfiles/lib/shellenv.sh +++ b/dotfiles/lib/shellenv.sh @@ -19,18 +19,72 @@ function get_python_scripts_path { function _setup_env { _path_helper - hash brew 2>/dev/null && add_to_path "$(brew --prefix coreutils)/libexec/gnubin" - add_to_path /usr/local/lib/python2.7/site-packages --after - add_to_path "$HOME/bin" - hash brew 2>/dev/null && add_to_path --before "$(brew --prefix coreutils)/libexec/gnubin" + add_to_path "/usr/local/bin" - add_to_path "$(get_python_scripts_path)" --before + is_osx && _osx_path_setup + _java_setup + _go_setup + _rust_setup + _tex_setup + add_to_path "/usr/local/sbin" --after + add_to_path "$HOME/.local/bin" + add_to_path "$HOME/.lib/bin" + + # Travis completion + [ -f "$HOME/.travis/travis.sh" ] && source "$HOME/.travis/travis.sh" + + export ENVIRONMENT_SETUP_DONE="$(date)" +} + +function _osx_path_setup { + # What is going on here? Why does /libexec/gnubin get added twice + hash brew 2>/dev/null && add_to_path "$(brew --prefix coreutils)/libexec/gnubin" + add_to_path "$HOME/bin" + hash brew 2>/dev/null && add_to_path --before "$(brew --prefix coreutils)/libexec/gnubin" + + # Adds airport utility + add_to_path "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources" --after + + # I believe that this is here because of some issue with + # python builds in OSX + export CFLAGS=-Qunused-arguments + export CPPFLAGS=-Qunused-arguments +} + +function _python_setup { + add_to_path "$HOME/.lib/python" --after + export PYENV_ROOT="/usr/local/var/pyenv" + + if which pyenv > /dev/null; then + local PYENV_INIT_COMMANDS="$(pyenv init -)" + echo "$PYENV_INIT_COMMANDS" + eval "$PYENV_INIT_COMMANDS" + else + echo "WARNING: pyenv is installed on this machine and python will likely not function correctly" + fi + + # The following line is no longer necessary since the pyenv shim + # should handle directing to the appropriate install. + + # add_to_path "$(get_python_scripts_path)" --before + + add_to_path "$HOME/.lib/python" --path-var 'PYTHONPATH' + + # This should no longer be needed now that pyenv is used + # add_to_path /usr/local/lib/python2.7/site-packages --after +} + +function _node_setup { + # node/nvm + export NVM_DIR="/Users/imalison/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm + export NODE_PATH="/usr/local/lib/node_modules/" +} + +function _java_setup { if is_osx; then - export CFLAGS=-Qunused-arguments - export CPPFLAGS=-Qunused-arguments - add_to_path "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources" --after local JDK_LOCATION="$(find /Library/Java/JavaVirtualMachines -depth 1 | head -n 1)" export JAVA_HOME="$JDK_LOCATION/Contents/Home" export STUDIO_JDK=$JDK_LOCATION @@ -40,51 +94,35 @@ function _setup_env { # Access gnu man pages. hash brew 2> /dev/null && export MANPATH="$(brew --prefix)/opt/coreutils/libexec/gnuman:$MANPATH" - else + else + # This may be ubuntu/debian specific export JAVA_HOME="$(update-alternatives --config java | get_cols ' -1' | head -n 1)" is_osx && export VISUAL="which emacsclient -c -n" fi add_to_path "$JAVA_HOME/bin" +} - add_to_path "$HOME/.lib/python" --after - add_to_path "/usr/local/sbin" --after - - function with_shellrc { - zsh -c "source ~/.zshrc && ""$@" - } - - # node/nvm - export NVM_DIR="/Users/imalison/.nvm" - [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm - export NODE_PATH="/usr/local/lib/node_modules/" - - add_to_path "$HOME/.local/bin" - add_to_path "$HOME/.lib/python" --path-var 'PYTHONPATH' - - # golang +function _go_setup { add_to_path "$HOME/go" --path-var 'GOPATH' add_to_path "${GOPATH//://bin:}/bin" +} - # ruby +function _rust_setup { + add_to_path "$HOME/.cargo/bin" +} + +function _ruby_setup { # Load RVM into a shell session *as a function* [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" export RBENV_ROOT=/usr/local/var/rbenv add_to_path "$HOME/.rbenv/bin" add_to_path "$HOME/.rvm/bin" --after hash rbenv 2> /dev/null && eval "$(rbenv init -)" +} - # rust - add_to_path "$HOME/.cargo/bin" - - # tex +function _tex_setup { is_osx && add_to_path "/Library/TeX/texbin/" - - # Travis completion - [ -f "$HOME/.travis/travis.sh" ] && source "$HOME/.travis/travis.sh" - - add_to_path "$HOME/.lib/bin" - export ENVIRONMENT_SETUP_DONE="$(date)" } function _path_helper { @@ -97,4 +135,13 @@ function _path_helper { environment_variable_exists PATH_HELPER_RAN || _path_helper environment_variable_exists ENVIRONMENT_SETUP_DONE || _setup_env + +# TODO(imalison): These need to run every time because of how their +# version managers work. This could cause problems with the situation +# where we want to intentionally override the python/ruby/node +# versions in use in a given shell. +_node_setup +_ruby_setup +_python_setup + _source_shellenv_files