-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.venv
executable file
·145 lines (126 loc) · 4.2 KB
/
.venv
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# (c) 2013 Mike Wadsten. http://github.com/mikewadsten
# Bash wrapper around Python virtualenv functionality.
# Allow for easy activation of virtualenv with command 'venv'
# (includes tab autocompletion!). Also includes the 'mkenv'
# command to easily create a new virtual environment within
# the ENVS_DIR location specified, and 'rmenv' to delete
# virtual environments.
ENVS_DIR=~/.envs
# Handles autocompletion of virtual environment names
_venv() {
local cur envs
cur=${COMP_WORDS[COMP_CWORD]}
envs=$(\ls $ENVS_DIR)
COMPREPLY=( $( compgen -W "$envs" -- $cur ))
return 0
}
venv() {
# Check argument count
if [ "$#" == "0" ]; then
echo "usage: $FUNCNAME <environment name>"
return 1
fi
local env envdir env_activate
env="$1"
envdir=$ENVS_DIR/$env
env_activate=$envdir/bin/activate
# Enter the Matrix
if [ -d "$envdir" ] ; then
if [ -f "$env_activate" ] ; then
source "$env_activate"
else
echo "Can't find activate script for virtual environment '$env'"
fi
else
echo "There is no virtual environment '$env'"
fi
}
mkenv() {
# Check argument count
if [ "$#" == "0" ]; then
echo "usage: $FUNCNAME <new environment name>"
return 1
fi
local dir env
env="$1"
dir=$ENVS_DIR/$env
# Environments starting with . interfere with my cunning plan to prevent deleting directories
# outside of $ENVS_DIR (or that directory itself) by stopping rmenv if the environment name
# given starts with .
case "$env" in
"."* )
echo "Please don't try to make environments starting with '.', I could get confused..."
return 1;;
*".."* )
echo "Please don't make environments containing '..', that's just weird!"
return 1;;
*"/"* ) # Doesn't mess with the filesystem, just might get weird...
echo "No slashes, please. ($1)"
return 1;;
*"\\"* ) # Doesn't mess with the filesystem, just might get weird...
echo "No slashes, please. ($1)"
return 1;;
*" "* ) # Would make it so you have to wrap arguments in quotes
echo "No spaces in environment name, please. ($1)"
return 1;;
esac
if [ -d "$dir" ]; then # already exists
echo "There is already a virtual environment '$1'"
return 1
else
virtualenv "$dir"
fi
}
rmenv() {
# Check argument count
if [ "$#" == "0" ]; then
echo "usage: $FUNCNAME <environment name>"
return 1
fi
local dir env
env="$1"
dir=$ENVS_DIR/$env
if [ -d "$dir" ]; then
case "$env" in
"."* )
echo "The given environment ($1) starts with . -- I don't like that."
return 1;;
*".."* )
echo "Looks like you're trying to mess with me ($1) -- son, just don't."
return 1;;
esac
# The virtual environment dir does exist
read -p "Are you sure you want to delete virtual environment '$1'? (y/n) " -n 1 -r
echo # Newline for some spacing
echo # Another one for good measure
if [[ $REPLY =~ ^[Yy]$ ]]; then
# See if we are already inside the environment
case $PS1 in
"($1)"*)
echo "Calling $FUNCNAME on active virtual environment! Deactivating..."
deactivate
# Presumably the virtual environment exists anyway...
# Check we're not inside of it
case "$(pwd)" in
"$dir"* )
echo "You're physically inside the environment right now! Leaving it..."
builtin cd ~ ;;
*) ;;
esac ;;
*) ;;
esac
rm "$dir" -rf
echo "Deleted virtual environment '$1'"
else
echo "Never mind then!"
fi
else
echo "There is no virtual environment '$1'"
fi
}
activate() {
venv $@
}
complete -F _venv -o nospace venv
complete -F _venv -o nospace rmenv
complete -F _venv -o nospace activate