-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvenvworkon.sh
executable file
·69 lines (55 loc) · 1.75 KB
/
venvworkon.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
workon() {
# Sets WORKON_HOME to default if not set already
if [[ -z $WORKON_HOME ]]; then
WORKON_HOME="$HOME/.venv"
fi
# Creates WORKON_HOME directory if not exists
if [[ ! -e $WORKON_HOME ]]; then
mkdir -p $WORKON_HOME
echo "Virtual env directory created in $WORKON_HOME"
fi
# Quit if there is no arguments, while showing available venvs
if [[ "$#" -lt "1" ]]; then
echo "Usage: workon ENV_NAME PYTHON_VERSION"
echo "Available envs:"
ls "$WORKON_HOME"
return
fi
envdir="$WORKON_HOME/$1"
python_version="3"
projectdir=""
if [[ "$1" == "." ]]; then
target=$(basename `pwd`)
envdir="$WORKON_HOME/$target"
projectdir=$(pwd)
fi
if [[ "$#" -gt "1" ]]; then
python_version=$2
fi
python_exec="python$python_version"
# Creates virtual env if doesn't exists
if [[ ! -e "$envdir" ]]; then
$python_exec -m venv $envdir
if [[ "$projectdir" != "" ]]; then
echo "projectdir=$projectdir" > "$envdir/postactivate.sh"
fi
echo "Virtual env created in $envdir"
fi
source "$envdir/bin/activate"
unset envdir # VIRTUAL_ENV is now set by bin/activate
# Source postactivate.sh if exists in the virtual env
if [[ -e "$VIRTUAL_ENV/postactivate.sh" ]]; then
source "$VIRTUAL_ENV/postactivate.sh"
fi
if [[ -e "$projectdir/.env" ]]; then
. "$projectdir/.env"
fi
# Jumps to project directory if exists and PROJECT_HOME is set
projecthome="$PROJECT_HOME/$(basename $VIRTUAL_ENV)"
if [[ -d "$projecthome" ]]; then
cd "$projecthome"
if [[ -e "$projecthome/.env" ]]; then
. "$projecthome/.env"
fi
fi
}