-
Notifications
You must be signed in to change notification settings - Fork 3
/
.bash_invenio
419 lines (335 loc) · 9.05 KB
/
.bash_invenio
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Copyright (C) 2021 Graz University of Technology.
export PIPENV_VERBOSITY=-1
export FLASK_ENV=development
export NPM_CONFIG_PREFIX=~/.npm/global
export NPM_PACKAGE_FILE="package.json"
export PATH_TO_LOCAL_GITHUB_CODE=
export GIT_URL_TO_INSTITUTIONAL_REPOSITORY_CODE=
export USER=
# the directory structure that this would work is to have something like
# opensource
# github
# inveniosoftware
# tu-graz-library
# PERSONAL
# test
# repos
# v3
# repo
# env-dev-invenio-theme-tugraz
# v4
# repo
# env-dev-invenio-theme-tugraz
function clone() {
# this function simplifies the local cloning of a inveniosoftware
# package over the own github account
repo=$1
if [[ -d "$repo" ]]
then
echo "repo already cloned"
return
fi
git clone git@github.com:$USER/$repo
cd $repo
git remote add upstream https://github.com/inveniosoftware/$repo
cd ..
}
function release() {
# this function simplifies the releasing process by providing last
# release tags, a prompt to choose the new tag, some checks that the
# format of the release tag is correct, further it adds the changes
# from the last tag to current to CHANGES.rst and last but not least
# it creates a release on github and so on pypi if the actions are
# set up correctly
if [[ ! -d .git ]]
then
echo "this is only possible in git directories"
return
fi
if [[ "$(isSSHActive)" == " " ]]
then
echo "no active ssh session"
return
fi
if [[ -f $NPM_PACKAGE_FILE ]]
then
is_npm_package=true
else
is_npm_package=false
fi
git remote update
# show last tag name on the command line
git tag | sort -V | tail -n 10
# calculate latest tag
latest_tag=$(git describe --abbrev=0 --tags $(git rev-list --tags --max-count=1))
# choose new tag name
while true
do
read -p "enter tag name: " tag_name
# check about tag name style
# TODO check about tag is semantically greater
if [[ "$tag_name" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] && [[ "$latest_tag" < "$tag_name" ]]
then
break
else
if [[ ! "$tag_name" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]
then
echo "the style v[0-9]+\.[0-9]+\.[0-9] of $tag_name is not correct please redo."
return
fi
if [[ "$tag_name" < "$latest_tag" ]]
then
echo "the tag has the right format, but it is lower then the latest existing tag"
return
fi
fi
done
# check sed syntax to understand that completly
upstream_default="upstream/$(git remote show upstream | sed -n '/HEAD branch/s/.*: //p')"
# create branch with new tag name
# NOTE: doing this before of calculating the CHANGES ensures that HEAD is on the offical newest state
release_branch_name="rel-${tag_name//./-}"
git checkout -b $release_branch_name $upstream_default
# check if npm packages
if [[ $is_npm_package = true ]]
then
init_file=$NPM_PACKAGE_FILE
else
# calculate init filename
local_pwd=$(pwd)
source_dir=${local_pwd##*/}
target_dir=${source_dir//-/_}
init_file="$target_dir/__init__.py"
fi
if [[ ! -f $init_file ]]
then
echo "neither __init__.py nor package.json exists. Add according to npm or python."
return
fi
# get changes from last tag until now
# https://gist.github.com/gzagatti/3bf037d397e6e47e47e81b7fc8293fd4
text=$(git log --pretty=format:%s $latest_tag..HEAD | # get logs from last tag
sed '/Merge/d' | # remove commit messages that indicate a merge commit
sed -n '$!N; /^\(.*\)\n\1$/!P; D' | # remove duplicate lines
sed 's/.*/- &/' # add prefix dash to indicate that the result is a list
)
current_date=$(printf '%(%Y-%m-%d)T\n' -1)
# add release notes after =======
if [[ $is_npm_package = true ]]
then
CHANGES_FILE="CHANGES.md"
else
CHANGES_FILE="CHANGES.rst"
fi
TMP_RELEASE_NOTES="/tmp/release-notes.txt"
if [[ ! -f $CHANGES_FILE ]]
then
cat > $TMP_RELEASE_NOTES <<EOF
Changes
=======
EOF
fi
# intentionally empy line before Version. necessary to have a empty line after =======
cat >> $TMP_RELEASE_NOTES <<EOF
Version $tag_name (release ${current_date})
$text
EOF
# add it to CHANGES.*
sed -i "/=======/r $TMP_RELEASE_NOTES" $CHANGES_FILE
rm $TMP_RELEASE_NOTES
# update __version__ with the new tag
if [[ $is_npm_package = true ]]
then
sed_pattern='s/^.*"version".*/\t"version": "'${tag_name:1}'",/'
else
sed_pattern='s/^__version__.*/__version__ = "'${tag_name:1}'"/'
fi
sed -i "$sed_pattern" $init_file
# commit
git commit -am "release $tag_name"
# push branch
git push -u origin $release_branch_name
# create pr
gh pr create --fill
if [[ $(echo "$(gh pr checks)" | grep -c "fail") -ne 0 ]]
then
echo "there are failed tests"
return
fi
# rebase pr
gh pr merge --admin --rebase --delete-branch
# create release
gh release create $tag_name --generate-notes --title "Release $tag_name"
}
# example venv
# example venv env
# example venv env:dev:invenio-theme-tugraz
# example venv env:test:invenio-records-marc21:utnapischtim
function venv() {
sshStatus=$(isSSHActive)
in=${1:-env}
arrIn=(${in//:/ })
env=${in//:/-}
state=${arrIn[1]}
module=${arrIn[2]}
organisation=${arrIn[3]:-tu-graz-library}
repo="repo"
if [[ -d ${env} ]]
then
echo "the environment with this name does already exists"
return
fi
if [[ ! -d ${repo} && ! ${sshStatus} ]]
then
echo "to install the repository base from gitlab it is necessary to have a running ssh session"
echo "start a ssh session with"
echo " eval ssh-agent bash"
echo " ssh-add PATH_TO_SSH_KEY"
echo "then rerun this command"
return
fi
createEnvironment ${env}
activate ${env}
installNecessaryPythonPackages
if [[ ! -d ${repo} && ${sshStatus} ]]
then
installRepo ${repo}
echo "installed repository"
fi
if [[ ${state} == "test" ]]
then
setUpInvenioTestEnv ${module} ${organisation} ${repo}
fi
if [[ ${state} == "dev" ]]
then
setUpInvenioDevEnv ${module} ${organisation} ${repo}
fi
}
function createEnvironment() {
env=${1}
python3.8 -m venv ${env} > /dev/null
}
function installNecessaryPythonPackages() {
pip install --upgrade pip > /dev/null
echo "upgraded pip"
pip install invenio-cli > /dev/null
echo "installed invenio-cli"
pip install Flask-DebugToolbar > /dev/null
echo "installed flask-debugtoolbar"
pip install wheel > /dev/null
echo "installed wheel"
}
function installRepo() {
repo=${1}
if [[ ${GIT_URL_TO_INSTITUTIONAL_REPOSITORY_CODE} ]]
then
git clone ${GIT_URL_TO_INSTITUTIONAL_REPOSITORY_CODE} ${repo} &> /dev/null
echo "
[cli]
project_dir = $(pwd)/${repo}
instance_path =
services_setup = False" > ${repo}/.invenio.private
else
# TODO:
# it would be nice to have the <<EOF part in a variable, but i didn't manage
# to store it in a variable the way that i could use it here as i do it now
invenio-cli init <<EOF
yes
${repo}
${repo}
${repo}.org
${repo}/${repo}
${repo} instance
INSTITUTIONAL
${repo}@${repo}.com
2021
1
1
1
1
EOF
fi
}
function checkPrerequisitesToInstallLocalPackage() {
module=${1}
repo=${2}
pathToModule=${3}
if [[ ! ${module} ]]
then
echo "module has to be set"
return
fi
cd $repo
if [[ ! -d ${pathToModule} ]]
then
echo "module is not local accessible $pathToModule"
return
fi
}
function setUpInvenioTestEnv() {
module=${1}
organisation=${2}
repo=${3}
pathToModule="${PATH_TO_LOCAL_GITHUB_CODE}/github/${organisation}/${module}"
checkPrerequisitesToInstallLocalPackage ${module} ${repo} ${pathToModule}
pip install -e "${pathToModule}[all,postgresql,elasticsearch7]"
}
function setUpInvenioDevEnv() {
module=${1}
organisation=${2}
repo=${3}
pathToModule="${PATH_TO_LOCAL_GITHUB_CODE}/github/${organisation}/${module}"
checkPrerequisitesToInstallLocalPackage ${module} ${repo} ${pathToModule}
if [[ ! -f "Pipfile.lock" ]]
then
invenio-cli packages lock --dev --pre
fi
invenio-cli install --pre --development
invenio-cli packages install ${pathToModule}
}
function activate() {
base=${PWD}
target=${1:-env}
pathVirtualEnvFile="/tmp/invenio_virtual_env.txt"
if [[ -f $pathVirtualEnvFile ]]
then
target=$(cat $pathVirtualEnvFile)
fi
if [[ ${VIRTUAL_ENV} ]]
then
echo "VIRTUAL_ENV has the following $VIRTUAL_ENV value. It could be possible that another virtualenv is not deactivated beforehand"
return
fi
if [ -d ${target} ]
then
source ${target}/bin/activate
echo ${target} > $pathVirtualEnvFile
elif [[ ${PWD} =~ ${target} ]]
then
while [[ ! -d ${target} ]]
do
cd ..
done
source ${target}/bin/activate
echo ${target} > $pathVirtualEnvFile
cd ${base}
else
echo "wrong directory, no env directory and no env in the parent directories"
fi
}
function run() {
base=${PWD}
target=${1:-env}
repo="repo"
activate ${target}
if [[ -d ${repo} ]]
then
cd ${repo}
invenio-cli run
elif [[ ${PWD##*/} == "repo" ]]
then
invenio-cli run
else
echo "wrong directory"
fi
}