This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
venver.sh
625 lines (546 loc) · 16.5 KB
/
venver.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#!/usr/bin/env bash
#
# ...~\ venver /~...
# (bash and zsh)
#
# Simple virtualenv management and auto-switching
# (https://github.com/fgimian/venver)
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Fotis Gimian
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Disable virtualenv override by default
if [[ -z $VIRTUAL_ENV_OVERRIDE ]]
then
export VIRTUAL_ENV_OVERRIDE=0
fi
# Set the virtualenv home if not set already
if [[ -z $VIRTUAL_ENV_HOME ]]
then
export VIRTUAL_ENV_HOME=$HOME/.virtualenvs
fi
# Variables to enabled colored output
red='\033[0;31m'
green='\033[0;32m'
blue='\033[0;34m'
cyan='\033[0;36m'
no_color='\033[0m'
# The main entrypoint into venv which displays usage information or runs the
# appropriate function based on user input
venv()
{
# Create the virtualenv home if it doesn't exist already
if [[ ! -d $VIRTUAL_ENV_HOME ]]
then
mkdir -p "$VIRTUAL_ENV_HOME"
fi
# Obtain the action and then remove it from the argument list
local action=$1
shift
# Display help if no command or an invalid command was provided
declare -f "_venv_${action}" > /dev/null
command_found=$?
if [[ -z $action || $command_found -ne 0 ]]
then
echo -e "${blue}Usage: venv <command> [<args>]${no_color}
${cyan}Automatically manage virtualenvs for projects:${no_color}
init Initialise and create a virtualenv for the current project
clean Remove the virtualenv assigned to the current project
${cyan}Manually manage virtaulenvs:${no_color}
create Create a virtualenv
activate Activate a virtualenv
deactivate Deactivate a virtualenv
remove Remove a virtualenv
${cyan}General:${no_color}
copy Make a copy of a virtualenv
list List all available virtualenvs
base Change into the base directory of a virtualenv
site Change into the site-packages directory of a virtualenv
Please see https://github.com/fgimian/venver for more information"
if [[ ! -z $action ]]
then
echo -e "${red}venv: unsupported command ${action}${no_color}"
fi
return 1
fi
# Call the requested command
"_venv_$action" "$@"
}
# Automatic activation and deactivation of virtualenvs with venver
cd()
{
# Perform the regular cd
builtin cd "$@"
# If the user is controlling their virtualenvs, we don't do anything
if [[ $VIRTUAL_ENV_OVERRIDE -eq 1 ]]
then
return
fi
local virtualenv_dir
virtualenv_dir=$(__venv_find_virtualenv_file "$(pwd)")
# If the .virtualenv file was found, we ensure that the environment is
# activated
if [[ ! -z $virtualenv_dir ]]
then
virtualenv=$(cat "$virtualenv_dir/.virtualenv")
if [[ -f "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate" ]]
then
source "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate"
else
echo -e "${red}venv: the virtualenv ${virtualenv} doesn't exist,"\
"use 'venv init' to create it${no_color}"
return 1
fi
else
# If no virtualenv was found and one is already activated, we
# deactivate it for the user
if [[ ! -z $VIRTUAL_ENV && $VIRTUAL_ENV == $VIRTUAL_ENV_HOME/* ]]
then
deactivate
fi
fi
}
# Creates a new virtualenv (if required), activates it and enables the
_venv_init()
{
local virtualenv
local virtualenv_dir
virtualenv_dir=$(__venv_find_virtualenv_file "$(pwd)")
if [[ ! -z $1 && $1 != -* ]]
then
virtualenv=$1
shift
elif [[ ! -z $virtualenv_dir ]]
then
virtualenv=$(cat "$virtualenv_dir/.virtualenv")
else
virtualenv=$(basename "$(pwd)")
fi
if [[ -z $virtualenv_dir ]]
then
virtualenv_dir="$(pwd)"
fi
# Create the virtualenv
if [[ ! -f "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate" ]]
then
virtualenv "$@" "$VIRTUAL_ENV_HOME/$virtualenv"
if [[ $? -ne 0 ]]
then
return $?
fi
fi
# Add it to the project's .virtualenv file if necessary
if [[ ! -f "$virtualenv_dir/.virtualenv" ||
$virtualenv != $(cat "$virtualenv_dir/.virtualenv") ]]
then
echo "$virtualenv" > "${virtualenv_dir}/.virtualenv"
fi
# Activate the virtualenv
if [[ $VIRTUAL_ENV_OVERRIDE -eq 0 ]]
then
source "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate"
else
echo -e "${blue}venv: a virtualenv has been activated manually,"\
"please deactivate it to enable ${virtualenv}${no_color}"
return 1
fi
}
# Removes a project's virtualenv and related .virtualenv file
_venv_clean()
{
local virtualenv_dir
virtualenv_dir=$(__venv_find_virtualenv_file "$(pwd)")
if [[ -z $virtualenv_dir ]]
then
echo -e "${red}venv: no virtualenv was found in a .virtualenv"\
"file${no_color}"
return 1
fi
virtualenv=$(cat "$virtualenv_dir/.virtualenv")
if [[ ! -z $VIRTUAL_ENV &&
$VIRTUAL_ENV == "$VIRTUAL_ENV_HOME/$virtualenv" &&
$VIRTUAL_ENV_OVERRIDE -eq 1 ]]
then
echo -e "${red}venv: the project's virtualenv has been manually"\
"activated, unable to continue${no_color}"
return 1
fi
if [[ ! -z $VIRTUAL_ENV &&
"$VIRTUAL_ENV" == "$VIRTUAL_ENV_HOME/$virtualenv" ]]
then
deactivate
fi
rm -rf "${VIRTUAL_ENV_HOME:?}/$virtualenv"
rm -f "$virtualenv_dir/.virtualenv"
}
# Creates a new self-managed virtualenv with the given name
_venv_create()
{
if [[ -z $1 ]]
then
echo -e "${blue}Usage: venv create <name>${no_color}"
return 1
fi
local virtualenv=$1
shift
if [[ -d "$VIRTUAL_ENV_HOME/$virtualenv" ]]
then
echo -e "${red}venv: the virtualenv ${virtualenv} already exists,"\
"aborting${no_color}"
return 1
fi
virtualenv "$@" "$VIRTUAL_ENV_HOME/$virtualenv"
if [[ $? -eq 0 ]]
then
source "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate"
export VIRTUAL_ENV_OVERRIDE=1
fi
}
# Activates the nearest virtualenv or one provided
_venv_activate()
{
if [[ -z $1 ]]
then
echo -e "${blue}Usage: venv create <name>${no_color}"
return 1
fi
local virtualenv=$1
if [[ -f "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate" ]]
then
source "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate"
export VIRTUAL_ENV_OVERRIDE=1
else
echo -e "${red}venv: the virtualenv ${virtualenv} doesn't exist,"\
"unable to activate${no_color}"
return 1
fi
}
# Deactivates a self-managed virtualenv
_venv_deactivate()
{
if [[ ! -z $VIRTUAL_ENV ]]
then
local virtualenv
local virtualenv_dir
local override=0
virtualenv_dir=$(__venv_find_virtualenv_file "$(pwd)")
if [[ $VIRTUAL_ENV_OVERRIDE -eq 1 ]]
then
override=1
deactivate
export VIRTUAL_ENV_OVERRIDE=0
fi
# If the .virtualenv file was found, we ensure that environment stays
# activated
if [[ ! -z $virtualenv_dir ]]
then
virtualenv=$(cat "$virtualenv_dir/.virtualenv")
if [[ -f "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate" ]]
then
source "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate"
if [[ $override -eq 1 ]]
then
echo -e "${blue}venv: reverting to the virtualenv"\
"${virtualenv} as defined in the .virtualenv"\
"file${no_color}"
else
echo -e "${red}venv: a .virtualenv file was found; unable"\
"to deactivate${no_color}"
fi
else
echo -e "${red}venv: the virtualenv ${virtualenv} doesn't"\
"exist, unable to activate${no_color}"
return 1
fi
fi
else
echo -e "${red}venv: no virtualenv is curretly activated${no_color}"
return 1
fi
}
# Deletes a self-managed virtualenv
_venv_remove()
{
if [[ -z "$1" ]]
then
echo -e "${blue}Usage: venv remove <name>${no_color}"
return 1
fi
local return_code=0
local virtualenv
local virtualenv_dir
# Remove the virtualenv and all its related files
for virtualenv in "$@"
do
if [[ -f "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate" ]]
then
if [[ ! -z "$VIRTUAL_ENV" &&
$VIRTUAL_ENV == "$VIRTUAL_ENV_HOME/$virtualenv" ]]
then
if [[ $VIRTUAL_ENV_OVERRIDE -eq 1 ]]
then
export VIRTUAL_ENV_OVERRIDE=0
fi
deactivate
fi
virtualenv_dir=$(__venv_find_virtualenv_file "$(pwd)")
if [[ ! -z $virtualenv_dir ]]
then
echo -e "${blue}venv: removing virtualenv which was specified"\
"in a .virtualenv file, use 'venv init' to"\
"recreate${no_color}"
fi
rm -rf "${VIRTUAL_ENV_HOME:?}/$virtualenv"
else
echo -e "${red}venv: the virtualenv ${virtualenv} doesn't exist,"\
"unable to remove${no_color}"
return_code=1
fi
done
return $return_code
}
# Makes a copy of a virtualenv
_venv_copy()
{
hash virtualenv-clone 2> /dev/null
if [[ $? -ne 0 ]]
then
echo -e "${red}Error: virtualenv-clone is required for the copy"\
"command to work${no_color}"
return 1
fi
if [[ -z $1 || -z $2 ]]
then
echo -e "${blue}Usage: venv copy <source_name>"\
"<destination_name>${no_color}"
return 1
fi
local virtualenv=$1
local destination=$2
if [[ -d "$VIRTUAL_ENV_HOME/$destination" ]]
then
echo -e "${red}venv: he destination virtualenv $destination already"\
"exists, aborting${no_color}"
return 1
elif [[ -f "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate" ]]
then
virtualenv-clone \
"$VIRTUAL_ENV_HOME/$virtualenv" "$VIRTUAL_ENV_HOME/$destination"
else
echo -e "${red}venv: the virtualenv ${virtualenv} doesn't exist,"\
"unable to change directory${no_color}"
return 1
fi
}
# Provides a plain listing of virtualenvs
__venv_simple_list()
{
local virtualenv_name
while IFS= read -r -d '' dir
do
if [[ -f "$dir/bin/activate" ]]
then
virtualenv_name=$(basename "$dir")
echo "$virtualenv_name"
fi
done < <(find "$VIRTUAL_ENV_HOME" -mindepth 1 -maxdepth 1 -type d -print0)
}
# Lists all virtualenvs that are available
_venv_list()
{
local virtualenv_dir
virtualenvs=$(__venv_simple_list)
if [[ -z $virtualenvs ]]
then
echo -e "${blue}venv: no virtualenvs were found in"\
"$VIRTUAL_ENV_HOME${no_color}"
return 1
fi
echo -e "${cyan}virtualenvs found in $VIRTUAL_ENV_HOME${no_color}"
IFS=$'\n'
for virtualenv in $(__venv_simple_list)
do
if [[ ! -z $VIRTUAL_ENV &&
$VIRTUAL_ENV == "$VIRTUAL_ENV_HOME/$virtualenv" ]]
then
echo -e -n "${green}* ${virtualenv} "
if [[ $VIRTUAL_ENV_OVERRIDE -eq 1 ]]
then
echo -e -n "(manually managed)"
else
virtualenv_dir=$(__venv_find_virtualenv_file "$(pwd)")
virtualenv_dir=${virtualenv_dir/$HOME/\~}
echo -e -n "(as defined in ${virtualenv_dir}/.virtualenv)"
fi
echo -e "${no_color}"
else
echo -e " ${virtualenv}"
fi
done
unset IFS
}
# Changes into the base directory of a virtualenv
_venv_base()
{
local virtualenv
local virtualenv_dir
virtualenv_dir=$(__venv_find_virtualenv_file "$(pwd)")
if [[ ! -z $1 ]]
then
virtualenv=$1
shift
elif [[ ! -z $virtualenv_dir ]]
then
virtualenv=$(cat "$virtualenv_dir/.virtualenv")
else
echo -e "${red}venv: no virtualenv specified or found in a"\
".virtualenv file${no_color}"
return 1
fi
# Change into the virtualenv directory
if [[ -f "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate" ]]
then
cd "$VIRTUAL_ENV_HOME/$virtualenv"
else
echo -e "${red}venv: the virtualenv ${virtualenv} doesn't exist,"\
"unable to change directory${no_color}"
return 1
fi
}
# Changes into the site-packages directory of a virtualenv
_venv_site()
{
local virtualenv
local virtualenv_dir
virtualenv_dir=$(__venv_find_virtualenv_file "$(pwd)")
if [[ ! -z $1 ]]
then
virtualenv=$1
shift
elif [[ ! -z $virtualenv_dir ]]
then
virtualenv=$(cat "$virtualenv_dir/.virtualenv")
else
echo -e "${red}venv: no virtualenv specified or found in a"\
".virtualenv file${no_color}"
return 1
fi
# Change into the virtualenv directory
if [[ -f "$VIRTUAL_ENV_HOME/$virtualenv/bin/activate" ]]
then
site_packages_dir=$("$VIRTUAL_ENV_HOME/$virtualenv/bin/python" -c "import distutils; print(distutils.sysconfig.get_python_lib())")
cd "$site_packages_dir"
else
echo -e "${red}venv: the virtualenv ${virtualenv} doesn't exist,"\
"unable to change directory${no_color}"
return 1
fi
}
# Attempts to find the nearest .virtualenv file
__venv_find_virtualenv_file()
{
local test_directory=$1
while [[ $test_directory != "/" ]]
do
if [[ -f "$test_directory/.virtualenv" ]]
then
virtualenv=$(cat "$test_directory/.virtualenv")
break
else
test_directory=$(dirname "$test_directory")
fi
done
if [[ $test_directory != "/" ]]
then
echo "$test_directory"
fi
}
# Bash completion for venver
_venv_bash_completion()
{
local cur prev opts
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
command="${COMP_WORDS[1]}"
case $prev in
venv)
opts="init clean create activate deactivate remove copy list base site"
;;
activate|base|site|remove|init|copy)
opts=$(__venv_simple_list)
;;
*)
# Support deletion of multiple virtualenvs at the same time
if [[ $command == "remove" ]]
then
opts=$(__venv_simple_list)
else
opts=""
fi
;;
esac
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
}
# ZSH completion for venver
_venv_zsh_completion()
{
local words completions
read -cA words
local cur prev opts
cur="${words[-1]}"
prev="${words[-2]}"
command="${words[2]}"
case $prev in
venv)
opts="init
clean
create
activate
deactivate
remove
copy
list
base
site"
;;
activate|base|site|remove|init|copy)
opts=$(__venv_simple_list)
;;
*)
# Support deletion of multiple virtualenvs at the same time
if [[ $command == "remove" ]]
then
opts=$(__venv_simple_list)
else
opts=""
fi
;;
esac
reply=(${(ps:\n:)opts})
}
# Enable bash or ZSH completion for the venv command
command -v compctl > /dev/null 2>&1
if [[ $? -eq 0 ]]
then
compctl -K _venv_zsh_completion venv
else
complete -F _venv_bash_completion venv
fi