-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualenv_cd_alias
94 lines (73 loc) · 1.97 KB
/
virtualenv_cd_alias
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
activator_suffix="bin/activate"
# Quick check to see if we're in a virtualenv directory.
function activate_global_cd {
ROOT="$PWD"
package=$(echo $PWD | awk '{n = split($0,a,"/"); print n }')
for (( $package; package>0; package-- ))
do
activator="${ROOT}/$activator_suffix"
python_binary="${ROOT}/bin/python"
if [ -f "$activator" ]
then
if [ -f "$python_binary" ]
then
echo "sourcing $activator"
source "$activator"
fi
fi
ROOT="$ROOT/.."
done
}
# Detect whether we've entered or left a virtualenv directory.
function activate_cd {
found=0
ROOT="$PWD"
package=$(echo $PWD | awk '{n = split($0,a,"/"); print n }')
for (( $package; package>0; package-- ))
do
activator="${ROOT}/$activator_suffix"
python_binary="${ROOT}/bin/python"
if [ -f "$activator" ]
then
if [ -f "$python_binary" ]
then
found=1
fi
fi
ROOT="$ROOT/.."
done
# Actually change directory
if [ -z "$1" ]
then
cd ~
else
cd "$1"
fi
found2=0
ROOT="$PWD"
package=$(echo $PWD | awk '{n = split($0,a,"/"); print n }')
for (( $package; package>0; package-- ))
do
activator="${ROOT}/$activator_suffix"
python_binary="${ROOT}/bin/python"
if [ -f "$activator" ]
then
if [ -f "$python_binary" ]
then
found2=1
if [ $found -eq 0 ]
then
echo "sourcing $activator"
source "$activator"
fi
fi
fi
ROOT="$ROOT/.."
done
if [[ $found -eq 1 && $found2 -eq 0 ]];then
deactivate
fi
}
alias cd='activate_cd'
# Always do a global search when starting a new terminal instance.
activate_global_cd