Skip to content

Instantly share code, notes, and snippets.

@cianmce
Created March 18, 2017 15:27
Show Gist options
  • Save cianmce/41d54200a09c09f5f63538d79cac9d22 to your computer and use it in GitHub Desktop.
Save cianmce/41d54200a09c09f5f63538d79cac9d22 to your computer and use it in GitHub Desktop.
#!/bin/bash
# install by adding:
# alias venv=". ~/location/venv.sh"
# to .bashrc
help_string="venv [option] [VENV_NAME]
VENV_NAME
activates VENV_NAME is it exists
-l
DEFAULT
Lists all virtualenvs and shows which is currently activated
-c, --create
create and activates new virtualenv VENV_NAME
-q, --quit
same as 'deactivate'
deactivates current virtualenv
-h, --help
shows help string
"
[[ "$VIRTUAL_ENV" == "" ]]; INVENV=$?
arg=$1
typeset -l arg
if [[ "$arg" == "-l" ]] || [[ "$arg" == "" ]]
then
# Listing
for D in $(find ~/.virtualevns -mindepth 1 -maxdepth 1 -type d);
do
directory=$(echo $D | cut -d'/' -f 5)
if [[ "$D" == "$VIRTUAL_ENV" ]]
then
echo "[*] $directory"
else
echo "[ ] $directory"
fi
done
elif [[ "$arg" == "-q" ]] || [[ "$arg" == "--quit" ]]
then
if [[ "$INVENV" == "1" ]]
then
echo "deactivating..."
deactivate
else
echo "Not in a virtualenv"
fi
elif [[ "$arg" == "-c" ]] || [[ "$arg" == "--create" ]]
then
name=$2
if [[ "$name" != "" ]]
then
curr_dir=$PWD
echo "creating $name"
cd ~/.virtualevns
virtualenv $name
echo "created $name"
source ~/.virtualevns/$name/bin/activate
echo "'deactivate' to exit"
venv -l
cd $curr_dir
else
echo "No name specified"
echo "venv -c <name>"
fi
elif [[ "$arg" == "-h" ]] || [[ "$arg" == "--help" ]]
then
echo "$help_string"
else
if [[ "$arg" != "" ]]
then
# setting
# check if valid
valid=0
venv_path=""
for D in $(find ~/.virtualevns -mindepth 1 -maxdepth 1 -type d);
do
directory=$(echo $D | cut -d'/' -f 5)
if [[ "$arg" == "$directory" ]]
then
valid=1
venv_path=$D
fi
done
if [[ "$valid" == "1" ]]
then
echo "activating '$arg'"
curr_dir=$PWD
source ~/.virtualevns/$arg/bin/activate
echo "'deactivate' to exit"
venv -l
else
echo "\"$arg\" not found"
fi
else
echo $help_string
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment