-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_support.sh
206 lines (178 loc) · 4.2 KB
/
python_support.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
#!/bin/bash
# source logger
LOGGER="logger"
. ${LOGGER}.sh
# open .gitignore and check whether venv is already in it
# if not, add it
add_venv() {
if ! grep -q "venv" .gitignore; then
echo "venv" >> .gitignore
e_success "Added venv to .gitignore"
fi
}
# check whether .git directory exists
check_git_dir() {
if [ ! -d .git ]; then
e_heading "This is not a git repository"
return 0
else
e_success "This is a git repository"
return 1
fi
}
# make virtualenv venv
make_venv() {
if [ ! -d venv ]; then
sudo virtualenv venv
e_success "virtualenv venv created successfully"
fi
}
# activate venv
activate_venv() {
PWD=`pwd`
echo 'hello from venv'
. venv/bin/activate
e_success "Activated virtualenv venv"
}
# install requirements
install_requirements() {
# make sure requirements.txt exists
if [ ! -f requirements.txt ]; then
e_error "requirements.txt does not exist"
return 0
fi
pip3 install -r requirements.txt
}
# check python version
check_python_version() {
# check whether python3 is installed
if ! which python3 > /dev/null; then
e_warning "Python3 is not installed"
e_arrow "Installing python3"
sudo apt-get install python3
sudo apt-get install python3-pip
e_success "Python3 installed successfully"
fi
}
check_virtualenv(){
# check whether virtualenv is installed
if ! which virtualenv > /dev/null; then
e_warning "Virtualenv is not installed"
e_arrow "Installing virtualenv"
sudo pip3 install virtualenv
e_success "Virtualenv installed successfully"
fi
}
# Start django server
start_django_server(){
python3 manage.py runserver
}
# Start Flask server
start_flask_server(){
flask run
}
# Setup up environment for django project
start_django(){
if check_git_dir; then
add_venv
fi
make_venv
activate_venv
install_requirements
start_django_server
}
# Setup up django project
startproject_django(){
e_header "Setting up your Engine"
PWD=`pwd`
if check_git_dir; then
add_venv
fi
# check whether virtualenv is installed
e_header "Setting up your virtual environment"
check_virtualenv
make_venv
activate_venv
pip3 install django &> /dev/null
django-admin startproject $1
deactivate
cd $PWD/$1
# move venv to project
mv ../venv .
activate_venv
pip3 freeze > requirements.txt
e_heading "installing django dependencies...."
python3 manage.py startapp $2
e_header "You are good to go"
}
# deactivate venv
stop_django(){
echo "Switching from venv .."
deactivate
}
# start django server
run_django(){
# check whether venv is activated
if [ -z "$VIRTUAL_ENV" ]; then
e_warning "venv is not activated"
activate_venv
fi
start_django_server
}
# Setup up environment for flask project
start_flask(){
if check_git_dir; then
add_venv
fi
make_venv
activate_venv
install_requirements
start_flask_server
}
stop_flask(){
echo "Switching from venv .."
deactivate
}
# start flask server
run_flask(){
# check whether venv is activated
if [ -z "$VIRTUAL_ENV" ]; then
e_warning "venv is not activated"
activate_venv
fi
start_flask_server
}
startproject_fastapi(){
if check_git_dir; then
add_venv
fi
make_venv
activate_venv
pip3 install fastapi
pip3 install uvicorn
echo "from fastapi import FastAPI" > main.py
echo "app = FastAPI()" >> main.py
echo "app.add_route('/', lambda: 'Hello World')" >> main.py
pip3 freeze > requirements.txt
uvicorn main:app --reload
}
start_fastapi(){
if check_git_dir; then
add_venv
fi
make_venv
activate_venv
install_requirements
uvicorn main:app --reload
}
fastapi_run(){
if [ -z "$VIRTUAL_ENV" ]; then
e_warning "venv is not activated"
activate_venv
fi
uvicorn main:app --reload
}
fastapi_stop(){
echo "Switching from venv .."
deactivate
}