Skip to content

Instantly share code, notes, and snippets.

@jan-swiecki
Last active June 9, 2021 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jan-swiecki/eb990b26f8bb2190b16b4ca079d2e7f3 to your computer and use it in GitHub Desktop.
Save jan-swiecki/eb990b26f8bb2190b16b4ca079d2e7f3 to your computer and use it in GitHub Desktop.
venv
#!/bin/bash
set -eo pipefail
. ./uninstall.sh
echo "$script_line" >> ~/.bashrc
mkdir -p "$HOME/.local/var/venv/"
#!/bin/bash
set -eo pipefail
path="$PWD"
script_line="source $PWD/venv"
cp ~/.bashrc ~/.bashrc.bak
cat ~/.bashrc | grep -v "${script_line}" > tmp && mv tmp ~/.bashrc
rm -rf "$HOME/.local/var/venv"
#!/bin/bash
venv () {
if [ -z "${1:-}" ]; then
cat <<EOF
usage:
venv create <name> create virtualenv named <name>
venv create2 <name> create virtualenv named <name> for python2
venv load [name] load virtualenv [name] (default: load from .venv file)
venv unload unload virtualenv
venv delete <name> delete virtualenv named <name>
venv ls list available virtualenvs
venv install install venv
venv uninstall uninstall venv
EOF
return
fi
if [ "$1" == "create" ]; then
virtualenv -p /usr/bin/python3 "$HOME/.local/var/venv/${2:-$(cat .venv)}"
venv load "${2:-$(cat .venv)}"
elif [ "$1" == "create2" ]; then
virtualenv -p /usr/bin/python2 "$HOME/.local/var/venv/${2:-$(cat .venv)}"
venv load "${2:-$(cat .venv)}"
elif [ "$1" == "load" ]; then
if [ "$2" == "." -o -z "$2" ]; then
if [ ! -f ".venv" ]; then
echo "no such file .venv"
else
source "$HOME/.local/var/venv/$(cat .venv)/bin/activate"
CURRENT_VENV="$(cat .venv)"
fi
else
source "$HOME/.local/var/venv/${2:-default}/bin/activate"
CURRENT_VENV="${2:-default}"
fi
elif [ "$1" == "unload" ]; then
if [ -z "$CURRENT_VENV" ]; then
echo "no such venv loaded"
else
"$HOME/.local/var/venv/$CURRENT_VENV/bin/deactivate"
fi
elif [ "$1" == "delete" ]; then
rm -rf "$HOME/.local/var/venv/${2:-$(cat .venv)}"
elif [ "$1" == "ls" ]; then
shift
ls -1 ~/.local/var/venv
elif [ "$1" == "install" ]; then
mkdir -p ~/.local/bin
mkdir -p ~/.local/var/venv
elif [ "$1" == "uninstall" ]; then
rm -rf ~/.local/var/venv
else
echo "error: no such command: $1" >&2
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment