-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
build-portable.sh
executable file
·275 lines (232 loc) · 7.26 KB
/
build-portable.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
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
#!/usr/bin/env bash
set -euo pipefail
BUILDNAME="${1:-}"
GITREPO="${2:-}"
GITREF="${3:-}"
declare -A DEPS=(
[curl]=curl
[git]=git
[jq]=jq
[yq]=yq
[pip]=pip
[unzip]=unzip
)
PIP_ARGS=(
--isolated
--disable-pip-version-check
)
GIT_FETCHDEPTH=300
ROOT=$(git rev-parse --show-toplevel 2>/dev/null || dirname "$(readlink -f "${0}")")
CONFIG="${ROOT}/config.yml"
CONFIG_PORTABLE="${ROOT}/portable.yml"
DIR_CACHE="${ROOT}/cache"
DIR_DIST="${ROOT}/dist"
# ----
SELF=$(basename "$(readlink -f "${0}")")
log() {
echo "[${SELF}]" "${@}"
}
err() {
log >&2 "${@}"
exit 1
}
[[ "${CI:-}" ]] || [[ "${VIRTUAL_ENV:-}" ]] || err "Can only be built in a virtual environment"
for dep in "${!DEPS[@]}"; do
command -v "${dep}" >/dev/null 2>&1 || err "${DEPS["${dep}"]} is required to build the installer. Aborting."
done
[[ -f "${CONFIG}" ]] \
|| err "Missing config file: ${CONFIG}"
CONFIGJSON=$(cat "${CONFIG}")
if [[ -n "${BUILDNAME}" ]]; then
yq -e ".builds[\"${BUILDNAME}\"]" >/dev/null 2>&1 <<< "${CONFIGJSON}" \
|| err "Invalid build name"
else
BUILDNAME=$(yq -r '.builds | keys | first' <<< "${CONFIGJSON}")
fi
read -r appname apprel \
< <(yq -r '.app | "\(.name) \(.rel)"' <<< "${CONFIGJSON}")
read -r gitrepo gitref \
< <(yq -r '.git | "\(.repo) \(.ref)"' <<< "${CONFIGJSON}")
read -r implementation pythonversion platform \
< <(yq -r ".builds[\"${BUILDNAME}\"] | \"\(.implementation) \(.pythonversion) \(.platform)\"" <<< "${CONFIGJSON}")
read -r _pythonversionfull pythonfilename pythonurl pythonsha256 \
< <(yq -r ".builds[\"${BUILDNAME}\"].pythonembed | \"\(.version) \(.filename) \(.url) \(.sha256)\"" <<< "${CONFIGJSON}")
gitrepo="${GITREPO:-${gitrepo}}"
gitref="${GITREF:-${gitref}}"
# ----
# shellcheck disable=SC2064
TEMP=$(mktemp -d) && trap "rm -rf '${TEMP}'" EXIT || exit 255
DIR_REPO="${TEMP}/source.git"
DIR_BUILD="${TEMP}/build"
DIR_ASSETS="${TEMP}/assets"
DIR_BIN="${DIR_BUILD}/bin"
DIR_PKGS="${DIR_BUILD}/pkgs"
mkdir -p \
"${DIR_CACHE}" \
"${DIR_DIST}" \
"${DIR_BUILD}" \
"${DIR_ASSETS}" \
"${DIR_BIN}" \
"${DIR_PKGS}"
get_sources() {
log "Getting sources"
git \
-c advice.detachedHead=false \
clone \
--depth="${GIT_FETCHDEPTH}" \
-b "${gitref}" \
"${gitrepo}" \
"${DIR_REPO}"
log "Commit information"
git \
-c core.pager=cat \
-C "${DIR_REPO}" \
log \
-1 \
--pretty=full
}
get_python() {
local filepath="${DIR_CACHE}/${pythonfilename}"
if ! [[ -f "${filepath}" ]]; then
log "Downloading Python"
curl -SLo "${filepath}" "${pythonurl}"
fi
log "Checking Python"
sha256sum -c - <<< "${pythonsha256} ${filepath}"
}
get_assets() {
local assetname
while read -r assetname; do
local filename url sha256
read -r filename url sha256 \
< <(yq -r ".assets[\"${assetname}\"] | \"\(.filename) \(.url) \(.sha256)\"" <<< "${CONFIGJSON}")
if ! [[ -f "${DIR_CACHE}/${filename}" ]]; then
log "Downloading asset: ${assetname}"
curl -SLo "${DIR_CACHE}/${filename}" "${url}"
fi
log "Checking asset: ${assetname}"
sha256sum -c - <<< "${sha256} ${DIR_CACHE}/${filename}"
done < <(yq -r ".builds[\"${BUILDNAME}\"].assets[]" <<< "${CONFIGJSON}")
}
prepare_python() {
log "Preparing Python"
unzip -q "${DIR_CACHE}/${pythonfilename}" -d "${DIR_BUILD}/Python"
}
prepare_assets() {
log "Preparing assets"
local assetname
while read -r assetname; do
log "Preparing asset: ${assetname}"
local type filename sourcedir targetdir
read -r type filename sourcedir targetdir \
< <(yq -r ".assets[\"${assetname}\"] | \"\(.type) \(.filename) \(.sourcedir) \(.targetdir)\"" <<< "${CONFIGJSON}")
case "${type}" in
zip)
mkdir -p "${DIR_ASSETS}/${assetname}"
unzip "${DIR_CACHE}/${filename}" -d "${DIR_ASSETS}/${assetname}"
sourcedir="${DIR_ASSETS}/${assetname}/${sourcedir}"
;;
*)
sourcedir="${DIR_CACHE}"
;;
esac
while read -r from to; do
install -vDT "${sourcedir}/${from}" "${DIR_BUILD}/${targetdir}/${to}"
done < <(yq -r ".assets[\"${assetname}\"].files[] | \"\(.from) \(.to)\"" <<< "${CONFIGJSON}")
done < <(yq -r ".builds[\"${BUILDNAME}\"].assets[]" <<< "${CONFIGJSON}")
}
prepare_files() {
log "Copying license file with file extension"
install -v "${DIR_REPO}/LICENSE" "${DIR_BUILD}/LICENSE.txt"
}
prepare_executables() {
log "Preparing executables"
TZ=UTC python ./build-portable-commands.py --config="${CONFIG_PORTABLE}" --target="${DIR_BIN}"
}
install_pkgs() {
log "Installing wheels"
pip install \
"${PIP_ARGS[@]}" \
--require-hashes \
--only-binary=:all: \
--platform="${platform}" \
--python-version="${pythonversion}" \
--implementation="${implementation}" \
--no-deps \
--target="${DIR_PKGS}" \
--no-compile \
--requirement=/dev/stdin \
< <(yq -r ".builds[\"${BUILDNAME}\"].dependencies | to_entries[] | \"\(.key)==\(.value)\"" <<< "${CONFIGJSON}")
log "Installing app"
pip install \
"${PIP_ARGS[@]}" \
--no-cache-dir \
--platform="${platform}" \
--python-version="${pythonversion}" \
--implementation="${implementation}" \
--no-deps \
--target="${DIR_PKGS}" \
--no-compile \
--upgrade \
"${DIR_REPO}"
log "Removing unneeded dist files"
( set -x; rm -r "${DIR_PKGS:?}/bin" "${DIR_PKGS}"/*.dist-info/direct_url.json; )
sed -i -E \
-e '/^.+\.dist-info\/direct_url\.json,sha256=/d' \
-e '/^\.\.\/\.\.\//d' \
"${DIR_PKGS}"/*.dist-info/RECORD
}
build_portable() {
log "Reading version string"
local versionstring versionplain versionmeta version
versionstring="$(PYTHONPATH="${DIR_PKGS}" python -c "from importlib.metadata import version;print(version('${appname}'))")"
versionplain="${versionstring%%+*}"
versionmeta="${versionstring##*+}"
# Not a custom git reference (assume that only tagged releases are used as source)
# Use plain version string with app release number and no abbreviated commit ID
if [[ -z "${GITREF}" ]]; then
version="${versionplain}-${apprel}"
# Custom ref -> tagged release (no build metadata in version string)
# Add abbreviated commit ID to the plain version string to distinguish it from regular releases, set 0 as app release number
elif [[ "${versionstring}" != *+* ]]; then
version="${versionplain}-0-g$(git -c core.abbrev=7 -C "${DIR_REPO}" rev-parse --short HEAD)"
# Custom ref -> arbitrary untagged commit (version string includes build metadata)
# Translate into correct format
else
version="${versionplain}-${versionmeta/./-}"
fi
log "Updating modification times"
local mtime
[[ "${SOURCE_DATE_EPOCH:-}" ]] && mtime="@${SOURCE_DATE_EPOCH}" || mtime=now
find "${DIR_BUILD}" -exec touch --no-dereference "--date=${mtime}" '{}' '+'
log "Packaging portable build"
local dist="${appname}-${version}-${BUILDNAME}"
(
cd "${TEMP}"
mv "${DIR_BUILD}" "${dist}"
find "./${dist}" \
| LC_ALL=C sort \
| TZ=UTC zip \
--quiet \
--latest-time \
-9 \
-X \
-@ \
"${DIR_DIST}/${dist}.zip"
)
sha256sum "${DIR_DIST}/${dist}.zip"
}
build() {
log "Building ${BUILDNAME}, using git reference ${gitref}"
get_sources
get_python
get_assets
prepare_executables
prepare_python
prepare_assets
prepare_files
install_pkgs
build_portable
log "Success!"
}
build