-
Notifications
You must be signed in to change notification settings - Fork 0
/
inve
executable file
·66 lines (56 loc) · 1.53 KB
/
inve
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
#!/bin/bash
# inve
#
# usage: inve [COMMAND [ARGS]]
#
# For use with Ian Bicking's virtualenv tool. Attempts to find the root of
# a virtual environment. Then, executes COMMAND with ARGS in the context of
# the activated environment. If no COMMAND is given, activate defaults to a
# subshell.
start="$PWD"
# First, locate the root of the current virtualenv
while [ "$PWD" != "/" ]; do
if [ -x bin/python ]; then
# we're alreay in a directory containing a (executable) bin/python
break
fi
# dotglob -> "*" also matches names starting with "."
shopt -s dotglob
# nullglob -> no result if pattern doesn't match
shopt -s nullglob
PYs=(*/bin/python)
shopt -u dotglob
shopt -u nullglob
# strip "/bin/python" off the end of each match
PYs=(${PYs[@]//\/bin\/python/})
if [ "${#PYs[@]}" -eq "1" ]; then
# one match -> use it!
cd ${PYs[0]}
break
elif [ ${#PYs[@]} -ge 2 ]; then
# multiple matches -> ask the user
PS3="Multiple environments found - please choose: "
select PY in ${PYs[@]}; do
if [ ! -z "$PY" ]; then
cd $PY
break
else
echo " > invalid choice!"
fi
done
break
fi
cd ..
done
if [ "$PWD" = "/" ]; then
cd "$start"
echo "Could not activate: no virtual environment found." >&2
exit 1
fi
venv="$PWD"
cd "$start"
# Activate
export VIRTUAL_ENV="$venv"
export PATH="$VIRTUAL_ENV/bin:$PATH"
unset PYTHON_HOME
exec "${@:-$SHELL}"