-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_and_upload.sh
executable file
·153 lines (130 loc) · 4.27 KB
/
build_and_upload.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
#!/usr/bin/env bash
# shellcheck disable=SC2206
save_version_to_file() {
local path="${1}"
local content="${2}"
echo "__version__ = '${content}'" > "${path}"
}
update_version() {
local version_file_name='version.py'
local data
data=$(date '+%y.%-m.%-d')
if [ ! -f "${version_file_name}" ]; then
save_version_to_file "${version_file_name}" "${data}"
poetry version "${data}"
return
fi
version=$(cat "${version_file_name}" | sed -e "s#'##g" | sed -e 's/__version__ = //g')
if [ "${version}" == "${data}" ]; then
save_version_to_file "${version_file_name}" "${version}-1"
poetry version "${version}-1"
elif [[ "${version}" == *"${data}"* ]]; then
# shellcheck disable=SC2206
# SC2206: Quote to prevent word splitting/globbing, or split robustly with mapfile or read -a.
# This solution is much simpler - We want to split variable value using space instead using IFS, read or mapfile
version_elements=(${version//-/ })
save_version_to_file "${version_file_name}" "${version_elements[0]}-$(( version_elements[1] + 1 ))"
poetry version "${version_elements[0]}-$(( version_elements[1] + 1 ))"
elif [[ "${version}" == *"-"* ]] && [ "${data}" != "${version}" ]; then
save_version_to_file "${version_file_name}" "${data}"
poetry version "${data}"
elif [ "${version}" == "" ]; then
save_version_to_file "${version_file_name}" "${data}"
poetry version "${data}"
else
echo "Strange condition with version:"
echo " Date: \"${data}\""
echo "Version: \"${version}\""
exit 3
fi
}
status_begin(){
echo -en "$(printf '\033[47;30m \033[0m%.0s' $(seq 1 $(tput cols)))\r"
echo -e "\033[44;97m (..) \033[0m\033[47;30m ${1} \033[0m"
}
status_end(){
if [ "${1}" -gt 0 ]; then
color="\033[41;37m"
message="(--)"
else
color="\033[42;97m"
message="(^^)"
fi
echo -en "$(printf '\033[47;30m \033[0m%.0s' $(seq 1 $(tput cols)))\r"
echo -e "${color} ${message} \033[0m\033[47;30m ${2} \033[0m"
}
usage() {
echo -e "
\r Usage:
\r ${0} build (to build only)
\r ${0} build test (to publish in TEST PYPI index)
\r ${0} build prod (to publish in PRODUCTION PYPI index)
"
exit 1
}
BUILD="${1}"
UPLOAD="${2}"
if [ -z "${BUILD}" ] || [ "${BUILD}" != "build" ]; then
usage
fi
if [ ! -z "${UPLOAD}" ]; then
PIP_REPOSITORY_URL="${PIP_TEST_REPOSITORY_URL}"
case ${UPLOAD} in
test)
server_info="Publishing to test PYPI Index"
PIP_REPOSITORY_URL="${PIP_TEST_REPOSITORY_URL}"
POETRY_PYPI="test-pypi"
;;
prod)
server_info="PUBLISHING TO PRODUCTION PYPI INDEX"
PIP_REPOSITORY_URL="${PIP_PRODUCTION_REPOSITORY_URL}"
POETRY_PYPI="pypi"
;;
*)
echo -e "\n ERROR: Second parameter unknown: \"${UPLOAD}\""
usage
;;
esac
fi
status_begin "Updating project version"
update_version
status_end $? "Updating project version"
: ' Instead poetry we can also use twine and build:
echo; echo
status_begin "Installing twine and build"
pip install twine build
status_end $? "Installing twine and build"
'
echo; echo
status_begin "Poetry - lock"
poetry lock
status_end $? "Poetry - lock"
echo; echo
status_begin "Building package"
poetry build
: ' Instead poetry we can also use build:
python -m build -sw .
'
status_end $? "Building package"
if [ ! -z "${UPLOAD}" ]; then
echo; echo
status_begin "${server_info}"
poetry publish -r ${POETRY_PYPI} -u ${PIP_USER_NAME} -p ${PIP_USER_PASSWORD} -vvv
: ' Instead poetry we can also use twine:
twine \
upload \
--verbose \
--repository-url "${PIP_REPOSITORY_URL}" \
-u "${PIP_USER_NAME}" \
-p "${PIP_USER_PASSWORD}" \
dist/*
'
status_end $? "${server_info}"
fi
echo; echo
status_begin "Cleaning..."
rm -rf dist/
rm -rf build/
rm -rf src/*.egg-info
rm -rf __pycache__
status_end $? "Cleaning..."