-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·191 lines (154 loc) · 4.63 KB
/
build.sh
File metadata and controls
executable file
·191 lines (154 loc) · 4.63 KB
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
#!/bin/bash
function loginfo() {
echo "INFO:$(date '+%F %T'): ************************* ${1} ***************************"
}
function logerror() {
echo "ERROR:$(date '+%F %T'): ************************* ${1} ***************************"
}
function setup() {
set -e
echo "Timezone = $(date '+%Z %z')"
kill_local_pypi_server_if_exists
cleanup_directories
}
# cleanup is required mainly for testing in local Dev machine
# below function can be used in setup() & teardown() functions
function cleanup_directories() {
# ./venv_dir - do not remove venv directory
# removing & creating venv dir for each iteration of testing is inefficient
# set +e, as below commands may return non-zero exit code.
# If no directory existsa, then script will exit, since we set -e
# in setup() function
set +e
[[ -d ./pypi_packages ]] && rm -Rf ./pypi_packages
[[ -d build ]] && rm -Rf build
[[ -d dist ]] && rm -Rf dist
[[ -d src/singly_linkedlist.egg-info ]] && rm -Rf src/*.egg-info
[[ -f log_pypi_server.log ]] && rm -f log_pypi_server.log
set -e
}
function create_build_env() {
if [[ ! -e ./venv_dir ]];then
mkdir ./venv_dir
loginfo "Created venv directory $PWD/venv_dir"
fi
# set-up vnev
loginfo "Activating venv"
python3 -m venv ./venv_dir
source ./venv_dir/bin/activate
pip install -r requirements.txt
loginfo "Activated venv"
}
function kill_local_pypi_server_if_exists() {
set +e
existing_pypi_pid=$(pgrep pypi-server)
set -e
if [[ -z $existing_pypi_pid ]];then
echo "No running Pypi server process detected"
else
kill -9 $existing_pypi_pid
echo "Killed Pypi server process with PID $existing_pypi_pid"
fi
}
function start_local_pypi_server() {
# setup local pypi server
# for package upload & download
mkdir pypi_packages
pypi-server -P . -a . -p 8085 pypi_packages &> log_pypi_server.log &
}
# lint
function lint() {
loginfo "Lint - start"
loginfo "flake8 - start"
flake8 src/singly_linkedlist/singly_linkedlist.py || exit
flake8 tests || exit
loginfo "flake8 - end"
loginfo "pylint - start"
pylint --ignore=__pycache__ src/ || exit
loginfo "pylint - end"
loginfo "Lint - end"
}
# test
function test_code(){
loginfo "Running Units Tests"
python3 -m pytest -v --ignore=lib --ignore=venv_dir tests/
loginfo "Unit Tests - Finished"
}
# build
function build() {
loginfo "Build - start"
python -m build
# https://packaging.python.org/glossary/#term-Built-Distribution
echo "Contents of wheel file(built distribution):"
unzip -l dist/*whl
# https://packaging.python.org/glossary/#term-Source-Archive
echo "Contents of tar file(source archive):"
tar --list -f dist/*tar.gz
loginfo "Build - end"
}
# Release/Upload artifacts/packages generated
function release() {
loginfo "Release - start"
# connection attempts to local Pypi server
max_attempts=5
readonly max_attempts
attempt=1
wait_seconds=5
readonly wait_seconds
while [[ $attempt -le $max_attempts ]];do
if nc -zv localhost 8085;then
echo "Connection to local Pypi server succeeded, proceeding to upload artifacts"
break
else
echo "Attempt $attempt : Connection to local Pypi server failed"
echo "Waiting $wait_seconds seconds"
let attempt++
sleep $wait_seconds
fi
if [[ $attempt -eq $max_attempts ]];then
echo "Exceeded maximum connection attempts. Aborting!"
exit 1
fi
done
# end of while loop
# Upload artifacts to local pypi server
# Pypi server is started in non-auth mode,
# but it expects some username & password, although not used
export TWINE_USERNAME='dummy'
export TWINE_PASSWORD='dummy'
twine upload --non-interactive --repository-url http://localhost:8085 dist/*
loginfo "Release - end"
}
# end of release() function
function install_generated_package() {
pip install -v --index-url http://localhost:8085 singly_linkedlist
}
function import_installed_package() {
if python -c 'import singly_linkedlist';then
echo "Package import successfull"
else
echo "Package import failed"
exit 1
fi
}
function teardown() {
pip uninstall -y singly_linkedlist
cleanup_directories
kill_local_pypi_server_if_exists
}
function main() {
# start
setup
create_build_env
start_local_pypi_server
lint
test_code
build
release
install_generated_package
import_installed_package
teardown
# end
}
# script execution starts here
main "$@"