Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
workspace manager for python virtualenv and golang
root=$(cd $(dirname ${BASH_SOURCE})/../; pwd)
export VIRTUALENV_GO=$root
export GOPATH=$VIRTUALENV_GO
export GOBIN=$VIRTUALENV_GO/bin
export PATH=$VIRTUALENV_GO/bin:$PATH
ws () {
# get virtualenv
if [ ! -z ${VIRTUAL_ENV} ];then
VIRTUALENV=${VIRTUAL_ENV}
fi
if [ ! -z ${VIRTUALENV_GO} ];then
VIRTUALENV=${VIRTUALENV_GO}
fi
is_activated () {
if [ -z ${VIRTUALENV} ];then
echo 0
return
fi
echo 1
}
ws_deactivate () {
type deactivate &>/dev/null
if [ $? -eq 1 ];then
return
fi
if [ ! -z ${VIRTUALENV} ];then
cd ${VIRTUALENV}
deactivate
fi
}
# parse options
args=("$@")
newargs=("")
no_activate=0
go_home=0
for a in "${args[@]}"
do
case $a in
"home")
go_home=1
return
;;
"-q")
ws_deactivate
return
;;
"-na")
no_activate=1
;;
*)
echo "$a" | grep '^\-' &>/dev/null
if [ $? -eq 0 ];then
continue
fi
newargs+=("$a")
;;
esac
done
cd $HOME/workspace
project=${newargs[@]}
# read properties from projects.json
get_project_path () {
python <<EOF
import sys
import json
try :
_j = json.loads(file('projects.json').read())
print _j.get("${project}", "${project}")
sys.exit(0)
except (TypeError, ValueError):
print "${project}"
sys.exit(0)
EOF
}
path=$(get_project_path)
if [ -z "${path}" ];then
if [ $(is_activated) -eq 1 ];then
cd ${VIRTUALENV}
return
fi
fi
ws_deactivate
cd $HOME/workspace/${path}
if [ ${no_activate} -eq 0 ];then
activate="$(pwd)/bin/activate"
if [ -f ${activate} ];then
source ${activate}
fi
fi
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment