-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
project_setup.sh
331 lines (288 loc) · 9.48 KB
/
project_setup.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
#!/bin/bash
#######################################################
# Script Name: project_setup.sh
# Description: This script sets up a Python project by
# creating a virtual environment,
# installing dependencies, and performing
# other project-specific actions.
# Author: Anuj Khandelwal
# Date: 30-05-2023
# Last Modified: 10-06-2023
# Contact: [email protected]
#######################################################
# Helper functions
# Function to create .env file from .env-template file
create_env_file() {
if [ -f ".env-template" ] && [ ! -f ".env" ]; then
echo
echo "🚀 Creating a .env file from .env-template file"
echo
cp .env-template .env || { echo "❌ Failed to create .env file from .env-template"; return 1; }
else
echo
echo "👉 .env file already present. Skipping this action"
echo
fi
}
# Function to create a virtual environment named .venv
create_virtual_environment() {
if [ ! -d ".venv" ]; then
echo
echo "🚀 Creating a virtual environment named .venv"
echo
python3 -m venv .venv || { echo "❌ Failed to create virtual environment .venv"; return 1; }
else
echo
echo "👉 Virtual environment .venv already present. Skipping this action"
echo
fi
}
# Function to activate the newly created virtual environment
activate_virtual_environment() {
echo
echo "🚀 Activating virtual environment .venv"
echo
source .venv/bin/activate || { echo "❌ Failed to activate virtual environment .venv"; return 1; }
}
# Function to unset proxy settings
unset_proxy() {
echo
echo "⛔ Unsetting Proxy"
echo
if [[ -n "$HTTPS_PROXY" ]] || [[ -n "$HTTP_PROXY" ]] ; then
echo "The environment variable HTTPS_PROXY is set for using proxy, unsetting.."
unset HTTPS_PROXY
unset HTTP_PROXY
fi
export PIPENV_DONT_LOAD_ENV=1
}
# Function to upgrade .venv python-pip
upgrade_pip() {
if [ "$use_proxy" = true ]; then
echo
echo "⛔ You have chosen to install packages behind proxy ⛔"
echo
echo
echo "🚀 Upgrading .venv python-pip"
echo
export $(grep -v '^#' .env | xargs)
python -m pip install --no-cache-dir --upgrade pip --proxy "$HTTP_PROXY" || { echo "❌ Failed to upgrade .venv python-pip"; return 1; }
echo
echo "🚀 Installing pipenv"
echo
pip install --no-cache-dir --upgrade pipenv --proxy "$HTTP_PROXY" || { echo "❌ Failed to install pipenv"; return 1; }
else
unset_proxy
echo
echo "🟢 You are now working without proxy 🟢"
echo
echo
echo "🚀 Upgrading .venv python-pip"
echo
python -m pip install --no-cache-dir --upgrade pip || { echo "❌ Failed to upgrade .venv python-pip"; return 1; }
echo
echo "🚀 Installing pipenv"
echo
pip install --no-cache-dir --upgrade pipenv || { echo "❌ Failed to install pipenv"; return 1; }
fi
}
# Function to install base packages using pipenv
install_packages() {
if [ "$use_proxy" = true ]; then
echo
echo "✅ 🔉 Installing base packages (using proxy)"
echo
pipenv install || { echo "❌ Failed to install base packages (using proxy)"; return 1; }
else
echo
echo "✅ 🔉 Installing base packages (without using proxy)"
echo
PIPENV_DONT_LOAD_ENV=1 pipenv install || { echo "❌ Failed to install base packages"; return 1; }
fi
}
# Function to install dev packages using pipenv
install_dev_packages() {
if [ "$use_proxy" = true ]; then
echo
echo "✅ 🔊 Installing development packages (using proxy)"
echo
pipenv install --dev || { echo "❌ Failed to install development packages (using proxy)"; return 1; }
else
echo
echo "✅ 🔊 Installing development packages"
echo
PIPENV_DONT_LOAD_ENV=1 pipenv install --dev || { echo "❌ Failed to install development packages"; return 1; }
fi
}
install_precommit() {
echo
echo "✨ Installing pre-commit"
echo
pre-commit install || { echo "❌ Failed to install pre-commit"; return 1; }
echo
}
clear_pipenv_cache() {
echo
echo "🧽 Attempting to clean pipenv cache"
echo
pipenv --clear || { echo "❌ Failed to clear cache"; return 1; }
echo
echo "🧹 Pip cache cleared"
}
# Function to clear README.md file
clear_readme_file() {
if [ "$clear_readme" = true ]; then
echo
echo "💣 You are about to clear the README.md file for this project"
echo "📛 This should only be run when you are setting up the project for the first time"
echo "❌ This action cannot be reversed."
echo -ne "🚦 Do you wish to proceed? [y/N]: "
echo
read -r choice
case "$choice" in
[Yy])
echo "# $(basename "$(pwd)")" > README.md || { echo "❌ Failed to clear README.md file"; return 1; }
echo
echo "🚧 README.md has been cleared"
echo
;;
[Nn])
echo "✅ Skipping README.md modification"
;;
*)
echo "❌ Wrong input! Please try again."
;;
esac
fi
}
# Function to clean the repository
clean_repo() {
echo
echo "🗑️ Cleaning the repository"
echo
# Remove .assets directory if it exists
if [ -d ".assets" ]; then
echo "📁 Removing .assets directory"
rm -r .assets || { echo "❌ Failed to remove .assets directory"; return 1; }
else
echo "❌ .assets directory not found. Skipping removal"
fi
# Remove CONTRIBUTING.md file if it exists
if [ -f "CONTRIBUTING.md" ]; then
echo "📄 Removing CONTRIBUTING.md file"
rm CONTRIBUTING.md || { echo "❌ Failed to remove CONTRIBUTING.md file"; return 1; }
else
echo "❌ CONTRIBUTING.md file not found. Skipping removal"
fi
# Remove CONTRIBUTING.md file if it exists
if [ -f "FUNDING.yml" ]; then
echo "📄 Removing FUNDING.yml file"
rm FUNDING.yml || { echo "❌ Failed to remove FUNDING.yml file"; return 1; }
else
echo "❌ FUNDING.yml file not found. Skipping removal"
fi
}
# Function to display help in red color
display_help() {
echo -e "\e[1m\e[31mUsage: source your_script.sh [OPTIONS]\e[0m"
echo -e "\e[1m\e[31mOptions:\e[0m"
echo -e " \e[1m\e[31m--install ⏳ Install base packages\e[0m"
echo -e " \e[1m\e[31m--install-dev ⌛ Install development packages along with base packages\e[0m"
echo -e " \e[1m\e[31m--use-proxy 🔐 Enable proxy for python package installation\e[0m"
echo -e " \e[1m\e[31m--unset-proxy 🔓 Disable proxy for python package installation\e[0m"
echo -e " \e[1m\e[31m--clear-readme 📜 Clear README.md file\e[0m"
echo -e " \e[1m\e[31m--remove-cache 💾 Remove pip and pipenv cache\e[0m"
echo -e " \e[1m\e[31m--help 🆘 Display help message\e[0m"
}
# Check command line arguments
use_proxy=false
install_dev=false
clear_readme=false
should_exit=false
install=false
remove_cache=false
no_proxy=false
while [ $# -gt 0 ]; do
case "$1" in
--unset-proxy)
no_proxy=true
;;
--remove-cache)
remove_cache=true
;;
--install)
install=true
;;
--use-proxy)
use_proxy=true
;;
--install-dev)
install_dev=true
;;
--clear-readme)
clear_readme=true
;;
--help)
display_help
should_exit=true
;;
*)
echo "Error: Unrecognized argument '$1'"
display_help >&2
should_exit=true
;;
esac
if [ "$should_exit" = true ]; then
break
fi
shift
done
# Check if the script should exit
if [ "$should_exit" = true ]; then
return 0
fi
# Main function
main() {
if [ "$clear_readme" = true ]; then
clear_readme_file || return 1
elif [ "$remove_cache" = true ]; then
clear_pipenv_cache || return 1
elif [ "$no_proxy" = true ]; then
unset_proxy || return 1
elif [ "$install" = true ]; then
echo
echo "---------------------------------------------"
echo "🐍 Setting up Python Virtual Environment 🌐"
echo "----------------------------------------------"
echo
# Call the functions
clean_repo || return 1
create_env_file || return 1
create_virtual_environment || return 1
activate_virtual_environment || return 1
upgrade_pip || return 1
install_packages || return 1
if [ "$install_dev" = true ]; then
install_dev_packages || return 1
install_precommit || return 1
else
echo
echo "❌ Not installing development packages"
echo
fi
clear_pipenv_cache || return 1
else
display_help
fi
}
main
# Check if the virtual environment is activated
if [[ "$VIRTUAL_ENV" != "" ]]; then
echo
echo -e "✅ Virtual environment activated: \e[1m\e[32m$VIRTUAL_ENV\e[0m"
echo
else
echo
echo -e "❌ \e[1m\e[31mFailed to activate the virtual environment.\e[0m"
echo
fi