This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanme.sh
executable file
·257 lines (225 loc) · 6.14 KB
/
cleanme.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
#!/bin/bash
#
# @file cleanme
# @brief Cleanup the enviroment, from root folder run: ./cleanme.sh.
# Path to the project used as source, defaults to current path.
PROJECT_PATH=$(pwd)
# String indicating to clean only resources of specific types.
# The allowed values are:
# - a: Ansible resources.
# - p: Python resources.
# The full string is: ap.
TYPE=''
# Wheter to uninstall or not the following software:
# - ansible
# - python3
UNINSTALL=false
# @description Delete ansible auto-created files.
#
# @noargs.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function cleanup_ansible() {
# Remove installed roles.
rm -rf ~/.ansible/roles/* &>/dev/null
if [[ $UNINSTALL == true ]]; then
uninstall_ansible
[ $? -eq 1 ] && return 1
fi
return 0
}
# @description Delete general auto-created files.
#
# @arg $1 string Optional project path. Default to current path.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function cleanup_general() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
# Delete soft links on tests folder.
for link_item in $(find . -type l -printf '%p\n'); do
rm $link_item
done
# Delete docs/build folder if found.
[[ -d $project_path/docs/build ]] && rm -rf $project_path/docs/build
# Delete doc/build folder if found.
[[ -d $project_path/doc/build ]] && rm -rf $project_path/doc/build
# Delete temporary files.
rm -rf /tmp/* &>/dev/null
return 0
}
# @description Delete python auto-created files.
#
# @arg $1 string Optional project path. Default to current path.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function cleanup_python() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
local python_files_regex="(\.pytest_cache|__pycache__|\.pyc|\.pyo$)"
find $project_path | grep -E $python_files_regex &>/dev/null
if [ $? -eq 0 ]; then
rm -rf $(find $project_path | grep -E $python_files_regex)
fi
# Remove coverage report.
rm -rf $project_path/htmlcov &>/dev/null
rm -f $project_path/.coverage &>/dev/null
return 0
}
# @description Shows an error message.
#
# @arg $1 string Message to show.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function error_message() {
[[ -z $1 ]] && return 0
case $1 in
path)
echo "$1 is not an existent directory."
;;
permissions)
echo 'Administrative permissions are needed.'
;;
esac
return 0
}
# @description Get bash parameters.
#
# Accepts:
#
# - *h* (help).
# - *p* <path> (project_path).
# - *t* <types> (only clean type).
# - *u* (uninstall).
#
# @arg '$@' string Bash arguments.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function get_parameters() {
# Obtain parameters.
while getopts 'h;p:t:u;' opt; do
OPTARG=$(sanitize "$OPTARG")
case "$opt" in
h) help && exit 0;;
p) PROJECT_PATH="${OPTARG}";;
t) TYPE="${OPTARG}";;
u) UNINSTALL=true;;
esac
done
return 0
}
# @description Shows help message.
#
# @noargs
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function help() {
echo 'Cleanup the enviroment.'
echo 'Parameters:'
echo '-h (help): Show this help message.'
echo '-o <only type> (type string): Optional string containing any of the
following characters: a, p. Each one indicating to only
clean a specific type of resources, being a = ansible, p = python,
for example the value "-o p" will clean python tests only.'
echo '-p <file_path> (project path): Optional absolute file path to the
root directory of the project to clean. if this
parameter is not espeficied, the current path will be used.'
echo '-u (uninstall): Uninstall the following software: ansible.'
echo 'Example:'
echo "./cleanme.sh -o ap -p /home/username/project -u"
return 0
}
# @description Cleanup the enviroment.
#
# @arg $@ string Bash arguments.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function main() {
get_parameters "$@"
if ! [[ -d $PROJECT_PATH ]]; then
error_message 'path'
return 1
fi
if [[ -z $TYPE ]] || [[ $TYPE == *'a'* ]]; then
cleanup_ansible
fi
if [[ -z $TYPE ]] || [[ $TYPE == *'p'* ]]; then
cleanup_python $PROJECT_PATH
fi
cleanup_general
return 0
}
# @description Sanitize input.
#
# The applied operations are:
#
# - Trim.
# - Remove unnecesary slashes.
#
# @arg $1 string Text to sanitize.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout Sanitized input.
function sanitize() {
[[ -z $1 ]] && echo '' && return 0
local sanitized="$1"
# Trim.
sanitized=$(trim "$sanitized")
# Remove double and triple slashes.
# Extract the protocol URL part (http:// or https://) (if exists).
protocol="$(echo $sanitized | grep :// | sed -e's,^\(.*://\).*,\1,g')"
# Remove the protocol.
sanitized="${sanitized/${protocol}/}"
# Remove unnecesary slashes.
sanitized=$(echo "$sanitized" | tr -s /)
# Readd the protocol (if exists).
sanitized=${protocol}${sanitized}
echo "$sanitized" && return 0
}
# @description Trim whitespace at the beggining and end of a string.
#
# @arg $1 string Text where to apply trim.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout Trimmed input.
function trim() {
[[ -z $1 ]] && return 1
local trimmed="$1"
# Strip leading spaces.
while [[ $trimmed == ' '* ]]; do
trimmed="${trimmed## }"
done
# Strip trailing spaces.
while [[ $trimmed == *' ' ]]; do
trimmed="${trimmed%% }"
done
echo "$trimmed" && return 0
}
# @description Uninstall Ansible.
#
# @noargs.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function uninstall_ansible() {
python3 -m pip uninstall ansible -y &>/dev/null
python -m pip uninstall ansible -y &>/dev/null
sudo apt purge -y ansible
sudo apt update
sudo apt autoremove
return 0
}
# Avoid running the main function if we are sourcing this file.
return 0 2>/dev/null
main "$@"