-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvenvwrapper.sh
89 lines (78 loc) · 2.25 KB
/
venvwrapper.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
#!/usr/bin/env bash
# Summary: Setup the wrapper functions into the current shell
# Usage: . venvwrapper.sh
VENVWRAPPER_CALLER=$_
# Setup home directory
if [ -z $1 ]
then
VENVWRAPPERDIRECTORY="$HOME/.venvwrapper-environments"
echo "Using $VENVWRAPPERDIRECTORY as the location for virtual environments"
echo "(pass a parameter to the venvwrapper script to override)"
else
VENVWRAPPERDIRECTORY=$1
shift
fi
# Ensure directory creation priviliges
mkdir -p "$VENVWRAPPERDIRECTORY"
echo "venvwrapper is configured and running"
# Set up all functions
activate() {
if [ -z $1 ]
then
echo "activates a previously-created virtual environment"
echo "Usage: activate VIRTUALENVIRONMENTNAME"
else
. "$VENVWRAPPERDIRECTORY/$1/bin/activate"
fi
}
mkvenv() {
echo "Directory $VENVWRAPPERDIRECTORY"
if [ -z $1 ]
then
echo "Creates a virtual environment"
echo "Usage: mkvenv VIRTUALENVIRONMENTNAME"
else
if [ -d "$VENVWRAPPERDIRECTORY/$1" ]
then
echo "That venv already exists"
else
echo "Creating a venv named $1"
python3 -m venv "$VENVWRAPPERDIRECTORY/$1"
activate $1
fi
fi
}
rmvenv() {
if [ -z $1 ]
then
echo "Removes a virtual environment"
echo "Usage: rmvenv VIRTUALENVIRONMENTNAME"
else
if [ -d "$VENVWRAPPERDIRECTORY/$1" ]
then
rm -r "$VENVWRAPPERDIRECTORY/$1"
echo "Removed venv $1"
else
echo "That venv does not exist"
fi
fi
}
lsvenv() {
ls $VENVWRAPPERDIRECTORY $*
}
# The checks below try to predict whether the script is set up correctly.
# They work correctly on Ubuntu, feel free to remove the checks if they're firing incorrectly.
# TODO: They don't work properly in OSX Catalina.
# Check for inclusion in .bashrc script
if [[ $VENVWRAPPER_CALLER != *"bash-completion"* ]]
then
echo ""
echo "You probably want this run in your '.bashrc' script."
echo "echo . $(realpath $BASH_SOURCE) $VENVWRAPPERDIRECTORY >> ~/.bashrc"
fi
# Check for whether it's been sourced
if [ $0 == $BASH_SOURCE ]
then
echo ""
echo "This should be run sourced. I don't think it was run sourced. (prepend 'source' or '.')"
fi