From 247297ec0901be71b92d1144f4e45415662ea902 Mon Sep 17 00:00:00 2001 From: Chad Glendenin Date: Fri, 14 Aug 2020 08:29:45 -0400 Subject: [PATCH] Fix virtualenvwrapper; rewrite PATH stuff in Python --- .ccgrc | 38 ++++++++++++++++---------------------- .pathmod.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 .pathmod.py diff --git a/.ccgrc b/.ccgrc index 6cf3214..7e8e6c0 100644 --- a/.ccgrc +++ b/.ccgrc @@ -4,35 +4,20 @@ ### Functions ################################################### -# append_path() and prepend_path() adapted from Fink's init.sh +PATHMOD=$HOME/.pathmod.py # add to end of path append_path() { - if eval test -z "\$$1" - then - eval "export $1=$2" - fi - - if ! eval test -z "\"\${$1##*:$2:*}\"" -o -z "\"\${$1%%*:$2}\"" -o -z "\"\${$1##$2:*}\"" -o -z "\"\${$1##$2}\"" - then - eval "export $1=\$$1:$2" - fi + NEW_PATH=$(python ${PATHMOD} append $1 $2) + eval "export $1='${NEW_PATH}'" } # add to beginning of path prepend_path() { - if eval test -z "\$$1" - then - eval "export $1=$2" - return - fi - - if ! eval test -z "\"\${$1##*:$2:*}\"" -o -z "\"\${$1%%*:$2}\"" -o -z "\"\${$1##$2:*}\"" -o -z "\"\${$1##$2}\"" - then - eval "export $1=$2:\$$1" - fi + NEW_PATH=$(python ${PATHMOD} prepend $1 $2) + eval "export $1='${NEW_PATH}'" } prepend_path_if_exists() @@ -95,7 +80,6 @@ esac ################################################### # My most common typo. -# Dear Ubuntu, please stop telling me to "sudo apt-get install sl" alias sl=ls alias mv='mv -iv' @@ -119,7 +103,7 @@ if [ "$(uname -s)" == "Darwin" ]; then # commands take precendence: #prepend_path PATH "/usr/local/bin" # need to force it: - PATH="/usr/local/bin:$PATH" + #PATH="/usr/local/bin:$PATH" # Fix Backspace/Delete confusion: stty erase ^? # XTerm's $MANPATH gets messed up, and I can't find where it's happening. @@ -130,6 +114,16 @@ if [ "$(uname -s)" == "Darwin" ]; then alias pg_start='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start' alias pg_stop="pg_ctl -D /usr/local/var/postgres stop -s -m fast" prepend_path_if_exists PATH "/usr/local/share/npm/bin" + + # https://stackoverflow.com/a/49528037/4240806 + # Configuration for virtualenv + export WORKON_HOME=$HOME/.virtualenvs + export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 + export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv + #prepend_path_if_exists PATH "/Users/chadglendenin/Library/Python/3.8/bin" + source /usr/local/bin/virtualenvwrapper.sh + + export BASH_SILENCE_DEPRECATION_WARNING=1 else alias ls='ls -F --color=auto' BASH_GIT_COMPLETION="/etc/bash_completion.d/git" diff --git a/.pathmod.py b/.pathmod.py new file mode 100644 index 0000000..4fe6f9e --- /dev/null +++ b/.pathmod.py @@ -0,0 +1,30 @@ +import argparse +import os +import sys +from collections import OrderedDict + +parser = argparse.ArgumentParser() +parser.add_argument('action', help='either "append" or "prepend"') +parser.add_argument('variable', + help='existing shell variable name (e.g., PATH) containing PATH-type, colon-delimited list (e.g., "/bin:/usr/bin")') +parser.add_argument('value', help='new value to add to the set (optionally if exists)') +#parser.add_argument('--if-exists', help='only modify the existing path variable if the new path exists') +args = parser.parse_args() + +path = os.environ.get(args.variable) +path_split = path.split(':') if path else [] +od = OrderedDict([x, None] for x in path_split) + +action = args.action +value = args.value +if action == 'prepend': + new_od = OrderedDict({value: None}) + new_od.update(od) + od = new_od +elif action == 'append': + od[args.value] = None +else: + print('"action" must be either "append" or "prepend"') + sys.exit(1) + +print(':'.join(od.keys()))