~dricottone/huttese-apk

ref: 56ea7f0ef754051e1bf3bd6b76a0e075d3086aa4 huttese-apk/pkgkit -rwxr-xr-x 5.9 KiB
56ea7f0e — Drew DeVault pkgkit: generate new abuild keys if necessary 4 years ago
                                                                                
dc230e0c Drew DeVault
1511e47d Drew DeVault
dc230e0c Drew DeVault
1511e47d Drew DeVault
dc230e0c Drew DeVault
8c3c3e9e Drew DeVault
dc230e0c Drew DeVault
204a142c Drew DeVault
b360456b Drew DeVault
dc230e0c Drew DeVault
204a142c Drew DeVault
dc230e0c Drew DeVault
56ea7f0e Drew DeVault
dc230e0c Drew DeVault
fbeb3da7 Drew DeVault
dc230e0c Drew DeVault
5f98174f Drew DeVault
44a5af69 Drew DeVault
5f98174f Drew DeVault
44a5af69 Drew DeVault
5f98174f Drew DeVault
d8b33e75 Drew DeVault
dc230e0c Drew DeVault
fbeb3da7 Drew DeVault
dc230e0c Drew DeVault
ef86d517 Drew DeVault
a4ee27dc Drew DeVault
fbeb3da7 Drew DeVault
dc230e0c Drew DeVault
8c3c3e9e Drew DeVault
e22186f7 Drew DeVault
8c3c3e9e Drew DeVault
44a0d199 Drew DeVault
bc082d37 Drew DeVault
44a0d199 Drew DeVault
e22186f7 Drew DeVault
78b06a80 Drew DeVault
e22186f7 Drew DeVault
8c3c3e9e Drew DeVault
9713d0de Drew DeVault
dc230e0c Drew DeVault
d56be4a1 Drew DeVault
dc230e0c Drew DeVault
68e2b4a1 Drew DeVault
d56be4a1 Drew DeVault
dc230e0c Drew DeVault
9713d0de Drew DeVault
68e2b4a1 Drew DeVault
dc230e0c Drew DeVault
204a142c Drew DeVault
dc230e0c Drew DeVault
180c3dcb Drew DeVault
dc230e0c Drew DeVault
d56be4a1 Drew DeVault
dc230e0c Drew DeVault
180c3dcb Drew DeVault
dc230e0c Drew DeVault
180c3dcb Drew DeVault
dc230e0c Drew DeVault
8c3c3e9e Drew DeVault
dc230e0c Drew DeVault
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
#!/bin/sh -eu
usage() {
	printf "%s\n\n" "Usage: $0 [command...] [flags...] [args...]"
	printf "%s\n" "$0 add-repo [-s] <name> <key>"
	printf "\t%s\n" "Trusts <key> and adds the local repo to /etc/apk/repositories"
	printf "\t%s\n\n" "-s: set this key as default in abuild.conf"
	printf "%s\n" "$0 build [-cu] [-v <pkgver>] <packages...>"
	printf "\t%s\n" "Builds packages"
	printf "\t%s\n" "-c: update checksums"
	printf "\t%s\n" "-u: update pkgrel"
	printf "\t%s\n\n" "-v <pkgver>: update pkgver"
	printf "%s\n" "$0 pkgver"
	printf "\t%s\n\n" "Prints the suggested package ver for this directory"
	printf "%s\n" "$0 upload <ssh server> <remote path> <packages...>"
	printf "\t%s\n" "Uploads packages via rsync and updates remote APKINDEX"
}

get_pkgvar() (
	var="$1"
	set +u
	. APKBUILD
	eval 'printf "%s\n" "$'"$var"'"'
)

find_repos() (
	find . -maxdepth 1 -type d -not -name . -not -name .git
)

find_pkg_repo() (
	pkg=$1
	for repo in $(find_repos)
	do
		if [ -d "$repo"/"$pkg" ]
		then
			echo "$repo"
			return 0
		fi
	done
	return 1
)

update_pkgrel() (
	pkg="$1"
	pkgver="$(get_pkgvar pkgver)"
	prev_ver=$(apk search -x "$pkg" | cut -c$((${#pkg}+2))-)
	prev_pkgver=$(printf "%s" "$prev_ver" | cut -d'-' -f1)
	prev_pkgrel=$(printf "%s" "$prev_ver" | cut -d'-' -f2 | cut -d'r' -f2-)
	if [ "$prev_pkgver" == "$pkgver" ]
	then
		pkgrel=$((prev_pkgrel + 1))
		sed -e "s/pkgrel=.*/pkgrel=$pkgrel/" -i APKBUILD
	fi
)

update_pkgver() {
	pkgver="$1"
	prev_pkgver=$(apk search -x ${project} | cut -d '-' -f2)
	sed -e "s/pkgver=.*/pkgver=$pkgver/" -i APKBUILD
	if [ "$prev_pkgver" != "$pkgver" ]
	then
		sed -e "s/pkgrel=.*/pkgrel=0/" -i APKBUILD
	fi
}

add_repo() {
	set_default=0
	while getopts s flag
	do
		case $flag in
			s) set_default=1 ;;
			*) usage && exit 1 ;;
		esac
	done
	shift $((OPTIND-1))
	echo "$@"
	if [ $# -ne 2 ]
	then
		usage
		exit 1
	fi
	repo="$1"
	key="$2"
	mkdir -p ~/packages/$repo
	echo ~/packages/$repo | cat - /etc/apk/repositories > /tmp/repositories
	sudo mv /tmp/repositories /etc/apk/repositories
	if ! [ -f "$key" ]
	then
		echo "Warning: generating new abuild key"
		(
			umask 0007
			openssl genrsa -out "$key" 2048
		)
	fi
	sudo openssl rsa -in $key -pubout \
		-out /etc/apk/keys/$(basename "$key").pub
	if [ $set_default -eq 1 ]
	then
		echo "PACKAGER_PRIVKEY="'"'$key'"' >> \
			~/.abuild/abuild.conf
	fi
}

build() (
	builddeps=0
	checksum=0
	updpkgrel=0
	pkgver=""
	# Disable progress on abuild-apk
	cat <<-"EOF" | sudo tee /usr/local/bin/abuild-apk
	#!/bin/sh
	exec /usr/bin/abuild-apk --no-progress $@
	EOF
	sudo chmod +x /usr/local/bin/abuild-apk
	export PATH=/usr/local/bin:$PATH
	while getopts Rcuv: flag
	do
		case $flag in
			R) builddeps=1 ;;
			c) checksum=1 ;;
			u) updpkgrel=1 ;;
			v) pkgver="$OPTARG" ;;
			*) usage && exit 1 ;;
		esac
	done
	shift $((OPTIND-1))
	if [ $# -eq 0 ]
	then
		echo "No packages specified."
		exit 1
	fi
	for pkg in $@
	do
		repo="$(find_pkg_repo "$pkg")"
		(
			cd "$repo"/"$pkg"
			if [ -n "$pkgver" ]
			then
				update_pkgver "$pkgver"
			fi
			if [ $updpkgrel -eq 1 ]
			then
				update_pkgrel "$pkg"
			fi
			if [ $checksum -eq 1 ]
			then
				abuild checksum
			fi
			if [ $builddeps -eq 1 ]
			then
				abuild -R
			else
				abuild -r
			fi
		)
	done
)

print_pkgver() {
	if [ $# -ne 0 ]
	then
		usage
		exit 1
	fi
	if [ -d .git ]
	then
		set +e
		pkgver=$(git describe --abbrev=0 2>/dev/null)
		if [ $? -ne 0 ]
		then
			pkgver=0
		fi
		set -e
		if ! git describe --exact-match HEAD >/dev/null 2>&1
		then
			cdate=$(git show -s -1 --format='%ci' HEAD)
			cdate=$(echo "$cdate" | cut -d' ' -f1 | sed -e 's/-//g')
			pkgver="${pkgver}_git${cdate}"
		fi
	elif [ -d .hg ]
	then
		pkgver=$(hg id -T '{latesttag}')
		if [ "$(hg id -T '{latesttagdistance}')" -ne 0 ]
		then
			pkgver="$(hg id -T '{latesttag}_hg{sub("-", "", date|shortdate)}')"
		fi
	else
		pkgver=unknown_0000
	fi
	echo "$pkgver"
}

mirror_ver() (
	mirror="$1"
	pkg="$2"
	apk policy "$pkg" | while read -r line
	do
		case "$line" in
			*:)
				ver="${line%:}"
				ver="${ver## }"
				;;
			*$mirror*)
				echo "$ver"
				;;
		esac
	done
)

upload_pkg() (
	repo="$1"
	pkg="$2"
	arch="$(uname -m)"
	. /etc/os-release
	aver="v$(printf "%s" "$VERSION_ID" | cut -d. -f1-2)"

	set -x
	path="$remote_path/$aver/$repo/$arch"
	ssh "$remote" mkdir -p "$path"

	url=$(echo "$remote" | cut -d@ -f2)
	prev_ver=$(mirror_ver "$url" "$pkg")

	for ver in $prev_ver
	do
		if [ "$prev_ver" != "none" ]
		then
			ssh "$remote" rm -f "$path/$pkg-$ver.apk"
		fi
	done
	pkgver="$(get_pkgvar pkgver)"
	pkgrel="$(get_pkgvar pkgrel)"

	rsync --blocking-io -rsP \
		~/packages/"$repo/$arch/$pkg-$pkgver-r$pkgrel.apk" "$remote":"$path/"
	set +x
)

update_index() (
	repo="$1"
	arch="$(uname -m)"

	. /etc/os-release
	aver="v$(printf "%s" "$VERSION_ID" | cut -d. -f1-2)"

	path="$remote_path/$aver/$repo/$arch"

	set -x
	ssh "$remote" \
		find "$path" -type f -name '*.apk' -print0 |
		ssh "$remote" xargs -0 apk index \
			--rewrite-arch "$(uname -m)" \
			-x "$path"/APKINDEX.tar.gz \
			-o "$path"/APKINDEX.unsigned.tar.gz
	rsync --blocking-io -rP \
		"$remote:$path/APKINDEX.unsigned.tar.gz" \
		/tmp/APKINDEX.tar.gz
	abuild-sign /tmp/APKINDEX.tar.gz
	rsync --blocking-io -rP /tmp/APKINDEX.tar.gz "$remote:$path/"
	set +x
)

upload() (
	if [ $# -lt 2 ]
	then
		usage
		exit 1
	fi
	remote="$1"
	remote_path="$2"
	shift 2
	if [ $# -eq 0 ]
	then
		echo "No packages specified"
		exit 1
	fi
	repos=""
	for pkg in $@
	do
		repo=$(find_pkg_repo "$pkg")
		if ! echo "$repos" | grep "$repo" >/dev/null
		then
			repos="$repo $repos"
		fi
		(
			cd "$repo"/"$pkg"
			arch="$(get_pkgvar arch)"
			subpkgs="$pkg:_:$arch $(get_pkgvar subpackages)"
			for subpkg in $subpkgs
			do
				name="$(echo "$subpkg" | cut -d':' -f1)"
				upload_pkg "$repo" "$name"
			done
		)
	done
	for repo in $repos
	do
		update_index $repo
	done
)

if [ $# -lt 1 ]
then
	usage
	exit 1
fi
cmd="$1"
shift

case "$cmd" in
	add-repo)
		add_repo "$@"
		;;
	build)
		build "$@"
		;;
	pkgver)
		print_pkgver "$@"
		;;
	upload)
		upload "$@"
		;;
	help|-h)
		usage
		;;
esac