Skip to content

Commit

Permalink
Fix virtualenvwrapper; rewrite PATH stuff in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
impekablechad committed Aug 14, 2020
1 parent f6ed028 commit 247297e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
38 changes: 16 additions & 22 deletions .ccgrc
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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'
Expand All @@ -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.
Expand All @@ -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"
Expand Down
30 changes: 30 additions & 0 deletions .pathmod.py
Original file line number Diff line number Diff line change
@@ -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()))

0 comments on commit 247297e

Please sign in to comment.