-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmkvenv.sh
66 lines (55 loc) · 1.72 KB
/
mkvenv.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
function mkvenv() {
if [[ -z "$1" ]]; then
echo "usage: mkvenv [--force] [--path VENV_PATH] [PYTHON_VERSION]"
return 1
fi
local cyan='\033[0;36m'
local red='\033[0;31m'
local nocolor='\033[0m'
local grey='\033[0;90m'
local force=""
local venvdir=".venv"
local version=""
if [[ -f .python-version ]]; then
version="$(cat .python-version)"
fi
while [[ -n "$1" ]]; do case $1 in
-p | --path )
shift; venvdir=$1
;;
-f | --force )
force=1
;;
*)
version="$1"
;;
esac; shift; done
hash pyenv 2>/dev/null || { echo -e "${red}pyenv not found; try 'brew install pyenv'${nocolor}"; return 1; }
hash direnv 2>/dev/null || { echo -e "${red}direnv not found; try 'brew install direnv'${nocolor}"; return 1; }
if [[ -f .python-version ]]; then
local existing="$(cat .python-version)"
if [[ -z "$force" && "$existing" != "$version" ]]; then
echo -e "${red}${version} does not match .python-version (${existing})${nocolor}"
return 1
fi
fi
pyenv local "$version" || return 1
if [[ -d "$venvdir" ]]; then
if [[ -z "$force" ]]; then
echo -e "${red}${venvdir} already exists! use -f to overwrite it${nocolor}"
return 1
elif [[ -n "$venvdir" && "$venvdir" != "/" && "$venvdir" != "" ]]; then
rm -r "$venvdir"
fi
fi
printf "$grey"
python -m venv "$venvdir" || return 1
printf "$nocolor"
touch .envrc
sed -i '' '/^source ".*bin\/activate"$/d' .envrc \
&& echo "source \"$venvdir/bin/activate\"" >> .envrc
sed -i '' '/^unset PS1$/d' .envrc \
&& echo "unset PS1" >> .envrc
direnv allow
echo -e "${cyan}Created Python virtual environment in $(cd "$venvdir" 2> /dev/null ; pwd) ${nocolor}"
}