generated from PaulHBartley/BlankPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkvenv
executable file
·71 lines (57 loc) · 3.31 KB
/
mkvenv
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
function mkvenv() {
#################################################################################################
# OVERVIEW
# This function calls the $ python venv module to create a Python virtual environment with the
# option to activate the environment directly after the environment is created.
#
# OPERATION
# $ mkvenv -a <directory> installs a .venv folder inside <directory>. It then calls $ source to
# run a script named "activate" found in the newly installed .venv folder. Running the "activate"
# script sets <directory> as the active Python virtual environment for that shell session. The
# "activate" script places "(<directory>) $" to the left of the command line prompt (as shown),
# signaling that all Python calls made with <directory> as the active environment will be routed
# to the Python interpreter in /<path>/<directory>/.venv. To return the Python environment to the
# system-wide Python install, type $ deactivate.
#
# $ mkvenv <directory> simply installs a .venv folder inside <directory> without activating
# <directory> as the Python virtual environment.
#
# For the script to work, python3.8 (or later) and the venv module need to be installed.
#
# DIRECTORY BEHAVIOR
# $ mkvenv [-a] <directory> -- makes <directory> (new or existing) a Python project folder
# inside the current working directory.
# $ mkvenv [-a] <path>/<directory> -- makes <directory> (new or existing) a Python project
# folder inside the <path> destination directory.
# $ mkvenv [-a] -- makes the current working directory into a Python project folder.
#
#################################################################################################
i=0
for arg in $1
do let i=i+1
done
j=0
for arg in $2
do let j=j+1
done
if [[ $# == 1 ]] && [[ $1 != -a ]] && [[ $i == 1 ]]; then
python3.8 -m venv --prompt `basename $1` `dirname $1`/`basename $1`/.venv
elif [[ $# == 2 ]] && [[ $1 == -a ]] && [[ $j == 1 ]]; then
python3.8 -m venv --prompt `basename $2` `dirname $2`/`basename $2`/.venv && source `dirname $2`/`basename $2`/.venv/bin/activate
elif [[ $# == 1 ]] && [[ $1 != -a ]] && [[ $i > 1 ]]; then
python3.8 -m venv --prompt "$(echo "$*" | sed -e 's/ /-/g')" "$(echo "$*" | sed -e 's/ /-/g')"/.venv
elif [[ $# == 2 ]] && [[ $1 == -a ]] && [[ $j > 1 ]]; then
python3.8 -m venv --prompt "$(echo "${*:2:$j}" | sed -e 's/ /-/g')" "$(echo "${*:2:$j}" | sed -e 's/ /-/g')"/.venv && source "$(echo "${*:2:$j}" | sed -e 's/ /-/g')"/.venv/bin/activate
elif [[ $# > 1 ]] && [[ $1 != -a ]] && [[ $i == 1 ]]; then
python3.8 -m venv --prompt "$(echo "$*" | sed -e 's/ /-/g')" "$(echo "$*" | sed -e 's/ /-/g')"/.venv
elif [[ $# > 1 ]] && [[ $1 == -a ]] && [[ $j == 1 ]]; then
python3.8 -m venv --prompt "$(echo "${*:2:$#}" | sed -e 's/ /-/g')" "$(echo "${*:2:$#}" | sed -e 's/ /-/g')"/.venv && source "$(echo "${*:2:$#}" | sed -e 's/ /-/g')"/.venv/bin/activate
elif [[ $# == 0 ]] && [[ $i == 0 ]] && [[ $j == 0 ]]; then
python3.8 -m venv --prompt `basename $PWD` `dirname $PWD`/`basename $PWD`/.venv
elif [[ $# == 1 ]] && [[ $1 == -a ]] && [[ $i == 1 ]] && [[ $j == 0 ]]; then
python3.8 -m venv --prompt `basename $PWD` `dirname $PWD`/`basename $PWD`/.venv && source `dirname $PWD`/`basename $PWD`/.venv/bin/activate
else
echo "The command could not operate on the given directory name. Please try again."
exit 2
fi
}