-
Notifications
You must be signed in to change notification settings - Fork 0
/
snnrb
executable file
·283 lines (251 loc) · 8.06 KB
/
snnrb
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
#!/bin/bash
# export PATH="$PWD:$PATH"
## declare the list of github repositories in an array.
declare -a arr=("snnadaptation" "snnalgorithms" "snnbackends" "snncompare" "snnradiation") #"simsnn")
# Specify global variables.
# DIR_WITH_REPOS="/home/$(echo $(whoami))/git/snn/"
DIR_WITH_REPOS="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
DIR_WITH_REPOS="$DIR_WITH_REPOS/../"
CONDA_ENVIRONMENT_NAME="snncompare"
while [[ "$#" -gt 0 ]]; do
case $1 in
-b|--branch)
CHECKOUT_BRANCH=1;
BRANCH_NAME="$2"
shift # past argument
shift # past value
;;
-nb|--new-branch)
CHECKOUT_NEW_BRANCH=1;
BRANCH_NAME="$2"
shift # past argument
shift # past value
;;
-r|--rebuild) rebuild=1; ;;
-p|--pull) pull=1 ;;
-pc|--precommit) precommit=1 ;;
-pu|--precommit-update) precommit_update=1 ;;
-pi|--precommit-install) precommit_install=1 ;;
-pip|--publish-to-pip) publish_to_pip=1 ;;
-c|--commitpush)
commitpush=1
COMMIT_MESSAGE="$2"
shift # past argument
shift # past value
;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
#######################################
# Verifies a directory exists, throws error otherwise.
# Local variables:
# dirpath
# Globals:
# None.
# Arguments:
# Relative folderpath of folder whose existence is verified.
# Returns:
# 0 If folder was found.
# 31 If the folder was not found.
# Outputs:
# Nothing
#######################################
manual_assert_dir_exists() {
local dirpath="$1"
if [ ! -d "$dirpath" ]; then
echo "The dir: $dirpath does not exist, even though one would expect it does."
exit 31
fi
}
#######################################
#
# Local variables:
#
# Globals:
#
# Arguments:
#
# Returns:
# 0 If function was evaluated successful.
# Outputs:
#
# TODO(a-t-0):
#######################################
# Source: https://stackoverflow.com/questions/70597896/check-if-conda-env-exists-and-create-if-not-in-bash
find_in_conda_env(){
conda env list | grep "${@}" >/dev/null 2>/dev/null
}
#######################################
#
# Local variables:
#
# Globals:
#
# Arguments:
#
# Returns:
# 0 If function was evaluated successful.
# Outputs:
#
# TODO(a-t-0):
#######################################
conda_env_exists() {
local some_envirionment="$1"
if find_in_conda_env ".*$some_envirionment.*" ; then
echo "FOUND"
else
echo "NOTFOUND"
fi
}
run_precommit() {
local git_path="$1"
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
eval "$(conda shell.bash hook)"
cd "$git_path" && conda deactivate && conda activate snncompare && git add *
cd "$git_path" && conda deactivate && conda activate snncompare && git add .* && pre-commit run --all-files
cd "$git_path" && conda deactivate && conda activate snncompare && pre-commit install
cd "$git_path" && conda deactivate && conda activate snncompare && pre-commit run --all-files
else
echo "Error, conda environment name:$CONDA_ENVIRONMENT_NAME not found."
exit 5
fi
}
run_precommit_install() {
local git_path="$1"
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
eval "$(conda shell.bash hook)"
cd "$git_path" && conda deactivate && conda activate snncompare && pre-commit install
else
echo "Error, conda environment name:$CONDA_ENVIRONMENT_NAME not found."
exit 5
fi
}
run_precommit_update() {
local git_path="$1"
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
eval "$(conda shell.bash hook)"
cd "$git_path" && conda deactivate && conda activate snncompare && pre-commit autoupdate
else
echo "Error, conda environment name:$CONDA_ENVIRONMENT_NAME not found."
exit 5
fi
}
git_pull() {
local git_path="$1"
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
eval "$(conda shell.bash hook)"
cd "$git_path" && conda deactivate && conda activate snncompare && git pull
else
echo "Error, conda environment name:$CONDA_ENVIRONMENT_NAME not found."
exit 5
fi
}
checkout_new_branch() {
local git_path="$1"
local branch_name="$2"
local new_branch="$3"
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
eval "$(conda shell.bash hook)"
if [ "$new_branch" == 1 ]; then
cd "$git_path" && conda deactivate && conda activate snncompare && git checkout -b "$branch_name"
cd "$git_path" && conda deactivate && conda activate snncompare && git add -A
cd "$git_path" && conda deactivate && conda activate snncompare && git commit -m "$message"
cd "$git_path" && conda deactivate && conda activate snncompare && git push --set-upstream origin "$branch_name"
else
cd "$git_path" && conda deactivate && conda activate snncompare && git checkout "$branch_name"
cd "$git_path" && conda deactivate && conda activate snncompare && git pull
fi
else
echo "Error, conda environment name:$CONDA_ENVIRONMENT_NAME not found."
exit 5
fi
}
commit_and_push() {
local git_path="$1"
local message="$2"
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
eval "$(conda shell.bash hook)"
cd "$git_path" && conda deactivate && conda activate snncompare && git add *
cd "$git_path" && conda deactivate && conda activate snncompare && git add -A
cd "$git_path" && conda deactivate && conda activate snncompare && git commit -m "$message"
cd "$git_path" && conda deactivate && conda activate snncompare && git push
else
echo "Error, conda environment name:$CONDA_ENVIRONMENT_NAME not found."
exit 5
fi
}
build_pip_package() {
local git_path="$1"
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
eval "$(conda shell.bash hook)"
cd "$git_path" && conda deactivate && conda activate snncompare && python3 setup.py sdist bdist_wheel
else
echo "Error, conda environment name:$CONDA_ENVIRONMENT_NAME not found."
exit 5
fi
}
publish_to_pip() {
local git_path="$1"
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
eval "$(conda shell.bash hook)"
cd "$git_path" && conda deactivate && conda activate snncompare && python3 setup.py sdist bdist_wheel
cd "$git_path" && conda deactivate && conda activate snncompare && python -m twine upload dist/\*
else
echo "Error, conda environment name:$CONDA_ENVIRONMENT_NAME not found."
exit 5
fi
}
install_pip_package() {
local git_path="$1"
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
eval "$(conda shell.bash hook)"
cd "$git_path" && conda deactivate && conda activate snncompare && pip install -e .
else
echo "Error, conda environment name:$CONDA_ENVIRONMENT_NAME not found."
exit 5
fi
}
for reponame in "${arr[@]}"
do
echo ""
echo ""
echo "$reponame"
# Assert the required folders exist.
manual_assert_dir_exists "$DIR_WITH_REPOS$reponame"
if [ "$rebuild" == 1 ]; then
# Build the pip package
build_pip_package "$DIR_WITH_REPOS$reponame"
# Install package locally
install_pip_package "$DIR_WITH_REPOS$reponame"
fi
if [ "$CHECKOUT_BRANCH" == 1 ] || [ "$CHECKOUT_NEW_BRANCH" == 1 ]; then
checkout_new_branch "$DIR_WITH_REPOS$reponame" "$BRANCH_NAME" "$CHECKOUT_NEW_BRANCH"
fi
if [ "$precommit" == 1 ]; then
run_precommit "$DIR_WITH_REPOS$reponame"
fi
if [ "$precommit_install" == 1 ]; then
run_precommit_install "$DIR_WITH_REPOS$reponame"
fi
if [ "$precommit_update" == 1 ]; then
run_precommit_update "$DIR_WITH_REPOS$reponame"
if [ "$reponame" == "snncompare" ]; then
if [ "$(conda_env_exists $CONDA_ENVIRONMENT_NAME)" == "FOUND" ]; then
cd "$DIR_WITH_REPOS$reponame" && conda deactivate
conda env update --file environment.yml
cd "$DIR_WITH_REPOS$reponame" && conda deactivate && conda activate snncompare
fi
fi
fi
if [ "$publish_to_pip" == 1 ]; then
publish_to_pip "$DIR_WITH_REPOS$reponame"
fi
if [ "$pull" == 1 ]; then
git_pull "$DIR_WITH_REPOS$reponame"
fi
if [ "$commitpush" == 1 ]; then
commit_and_push "$DIR_WITH_REPOS$reponame" "$COMMIT_MESSAGE"
fi
# TODO: if CLI arg is passed, upload the pip packages with upgraded version.
done