-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyrel.sh
242 lines (190 loc) · 6.48 KB
/
pyrel.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
# SPDX-FileCopyrightText: (c) 2021 Art Galkin <ortemeo@gmail.com>
# SPDX-License-Identifier: BSD-3-Clause
# See https://github.com/rtmigo/pyrel#pyrel
set -e
TWINE_CHECK=true # should we call `twine check` from `pyrel_test_begin`?
TWINE_CHECK_STRICT=true # works only if TWINE_CHECK is true
####################################################################################################
# without the `realpath` we cannot get the real project directory, so we start with it
function realpath() {
# emulating realpath for MacOS (10.14 does not have it @2021)
# refactored code from https://stackoverflow.com/a/18443300
local original_pwd
original_pwd=$PWD
cd "$(dirname "$1")"
local current_link
current_link=$(readlink "$(basename "$1")")
while [ "$current_link" ]; do
cd "$(dirname "$current_link")"
current_link=$(readlink "$(basename "$1")")
done
local result
result="$PWD/$(basename "$1")"
cd "$original_pwd"
echo "$result"
}
####################################################################################################
if [ ! -f "./setup.py" ]; then
echo "./setup.py not found. Please run the script python project directory"
exit 1
fi
echo "OK, we're in the project root"
project_root_dir=$(realpath .)
####################################################################################################
TWINE_CHECK_STRICT=true=true # useful ?
log() { printf '%s\n' "$*"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$@"; exit 1; }
header() {
echo
echo "================================================================================"
echo " $1"
echo "================================================================================"
echo
}
####################################################################################################
DEFAULT_TRAP_COMMAND="header EXITING"
function trap_add() {
# slightly modified https://stackoverflow.com/a/30650385
#
# Sample:
# set -e
# trap_add 'echo "in trap A"' EXIT
# trap_add 'echo "in trap B"' EXIT
# echo "before error"
# run_bad_command
# echo "after error"
# Output:
# before error
# in trap A
# in trap B
[ -z "${DEFAULT_TRAP_COMMAND}" ] && DEFAULT_TRAP_COMMAND="echo Exiting @ $(date)"
local trap_add_cmd
local new_cmd
local trap_add_name
local existing_cmd
trap_add_cmd=$1; shift || fatal "${FUNCNAME[0]} usage error"
new_cmd=
for trap_add_name in "$@"; do
# Grab the currently defined trap commands for this trap
existing_cmd=$(trap -p "${trap_add_name}" | awk -F"'" '{print $2}')
# Define default command
[ -z "${existing_cmd}" ] && existing_cmd="$DEFAULT_TRAP_COMMAND"
# Generate the new command
new_cmd="${existing_cmd};${trap_add_cmd}"
# Assign the tests
# shellcheck disable=SC2064
trap "${new_cmd}" "${trap_add_name}" || \
fatal "unable to add to trap ${trap_add_name}"
done
}
####################################################################################################
function pyrel_venv_begin() {
log PYREL_VENV_BEGIN
if [ -n "$temp_venv_dir" ]; then
fatal "Nesting venvs is not supported"
fi
temp_venv_dir=$(mktemp -d -t venv-XXXXXXX)
log pyrel_venv_begin: creating "$temp_venv_dir"
python3 -m venv "$temp_venv_dir"
# shellcheck disable=SC1090
source "$temp_venv_dir/bin/activate"
# inserting trap before other cleanup to handle nested venvs
trap_add "set +e && pyrel_venv_end && set -e" EXIT
}
function pyrel_venv_end() {
if [ -z "$temp_venv_dir" ]; then
log pyrel_venv_end: ok: "temp_venv_dir is undefined"
return
fi
if [ -d "$temp_venv_dir" ]; then
log pyrel_venv_end: removing "$temp_venv_dir"
deactivate
python3 -m venv "$temp_venv_dir" --clear
rm -rf "$temp_venv_dir"
log pyrel_venv_end: removed "$temp_venv_dir"
else
log pyrel_venv_end: ok: temp venv dir "$temp_venv_dir" does not exist.
fi
temp_venv_dir=""
}
####################################################################################################
function build_package() {
cd "$project_root_dir" || return 1
python3 -m pip install --upgrade pip
pip3 install setuptools wheel twine --force-reinstall
python3 setup.py sdist bdist_wheel
}
function check_package() {
if [ $TWINE_CHECK = true ]; then
cd "$project_root_dir" || return 1
if [ $TWINE_CHECK_STRICT = true ]; then
twine check ./dist/* --strict
else
twine check ./dist/*
fi
fi
}
function find_latest_file() {
# usage: find_latest_file *.txt
local file
local latest_file
for file in $1; do
[[ $file -nt latest_file ]] && latest_file=$file
done
echo "$latest_file"
}
function install_package_from_latest_whl() {
local latest_whl_file
latest_whl_file=$(find_latest_file "$project_root_dir"/dist/*.whl)
pip3 install --upgrade pip
pip3 install "$latest_whl_file" --force-reinstall
}
function cd_project() {
cd "$project_root_dir" || fatal "Failed to CD to the project dir"
}
function rmdir_with_msg() {
log "Removing $1"
# shellcheck disable=SC2086
rm -rf "$1" #{1}
}
function if_dir_unexisting_remove_on_exit() {
if ! compgen -G "$1" > /dev/null ; then
# directory does not exist now, so if we create it, we'll remove it
log "WILL REMOVE $1 on exit"
trap_add "rmdir_with_msg $1" EXIT
else
log "WILL NOT REMOVE $1 on exit"
fi
}
function pyrel_test_begin() {
# we will optionally remove dist, build and egg_info from the project dir
# if there weren't there before the tests
if_dir_unexisting_remove_on_exit "$project_root_dir/dist"
if_dir_unexisting_remove_on_exit "$project_root_dir/build"
if_dir_unexisting_remove_on_exit "$project_root_dir/*.egg-info"
header "BUILDING PACKAGE"
pyrel_venv_begin
cd_project
python3 -m pip install --upgrade pip
build_package
check_package
pyrel_venv_end
header "INSTALLING PACKAGE"
pyrel_venv_begin
install_package_from_latest_whl
# move from the current directory to random one
nowhere_dir=$(mktemp -d -t ci-XXXXXXXXXX)
cd "$nowhere_dir" || return 1
trap_add "rmdir_with_msg $nowhere_dir" EXIT
header "START OF USER COMMANDS BLOCK (after pyrel_test_begin)"
}
function pyrel_test_end() {
header "END OF OF USER COMMANDS BLOCK (pyrel_test_end)"
# Python 3.7 sometimes fails when trying to --clear venv while the current
# directory is created by mktemp. That's why we move to the project before
# ending the venv
cd_project
pyrel_venv_end
echo "All done."
}