-
Notifications
You must be signed in to change notification settings - Fork 36
/
buildinfo
executable file
·225 lines (210 loc) · 5.51 KB
/
buildinfo
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
#!/usr/bin/env bash
# shellcheck disable=SC2155
# Copyright (c) 2018 Robin Broda <robin@broda.me>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
declare -i format=1 builddate
declare -a buildenv options installed
declare -A data
# Desc: Parses line into current global state
# 1: line
function parse_line () {
IFS=$' = ' read -r key val <<< "${1}"
case "${key}" in
"format")
if [[ ${format} -ne ${val} ]]; then
echo "incompatible format (want ${format}, got ${val})" >&2
exit 1
fi
data["${key}"]="${val}"
;;
"buildenv") buildenv+=("${val}");;
"options") options+=("${val}");;
"installed") installed+=("${val}");;
*)
data["${key}"]="${val}"
;;
esac
}
# Desc: Parses buildinfo from string into current global state
# STDIN: .BUILDINFO
function parse () {
while read -r line; do
parse_line "${line}"
done
}
# Desc: Parses buildinfo from file into current global state
# 1: .BUILDINFO path
function parse_buildinfo () {
parse < "${1}"
}
# Desc: Parses buildinfo from provided package into current global state
# 1: Package path
function parse_package () {
parse <<< "$(tar xOf "${1}" .BUILDINFO 2>/dev/null)"
}
readonly archive_url="https://archive.archlinux.org/packages"
# Desc: get ALA link for given package
# 1: Package
function get_archive_link () {
local pkg="$(rev <<< "${1}")"
local pkgname="$(cut -d'-' -f4- <<< "${pkg}" | rev)"
echo "${archive_url}/${1:0:1}/${pkgname}/${1}.pkg.tar.xz"
}
# Desc: check validity of archive URL
# 1: Package archive link
# Return: check result
function verify_archive_link () {
curl -IfLlso /dev/null "${1}"
return $?
}
# Desc: Download package to $2 if not present and check signature using pacman's keyring
# 1: Package archive link
# 2: Target directory
function download_archive_package () {
local filename="${1}.pkg.tar.xz"
local cachedir=$(readlink -e "${2}")
if [[ -f "${cachedir}/${filename}" ]]; then
echo "Hit cache for ${filename}" >&2
echo "${2}/${filename}"
else
local pwd="$(pwd)"
local workdir="$(mktemp -d)"
local target="$(get_archive_link "${1}")"
cd "${workdir}" || exit 1
if verify_archive_link "${target}"; then
echo "Downloading ${filename}" >&2
curl -L "${target}" -o "${filename}" 2>/dev/null
curl -L "${target}.sig" -o "${filename}.sig" 2>/dev/null
if gpg --keyring /etc/pacman.d/gnupg/pubring.gpg --verify "${filename}.sig" 2>/dev/null; then
mv "${filename}" "${cachedir}/"
echo "${2}/${filename}"
else
echo "${filename} doesn't pass signature verification" >&2
echo "check ${workdir}" >&2
exit 1
fi
fi
cd "${pwd}" || exit 1
rm -r "${workdir}"
fi
}
declare action
while [ "$#" -gt 1 ]; do
case "${1}" in
-i)
action="info"
shift 1
;;
-p)
action="packages"
shift 1
;;
-d)
action="download"
shift 1
;;
-f)
action="field"
shift 1
;;
-ff)
action="field_all"
shift 1
;;
-*)
echo "unknown option: ${1}" >&2
exit 1
;;
*)
break
;;
esac
done
if [[ "${action}" == "" ]]; then
echo "no option given" >&2
exit 1
fi
declare file="$(readlink -e "${@: -1}")"
set -- "${@:1:$(($#-1))}"
if [[ "${file}" =~ \.pkg ]]; then
parse_package "${file}"
else
parse_buildinfo "${file}"
fi
case "${action}" in
"info")
echo -e "Name : ${data[pkgname]}"
echo -e "Base Name : ${data[pkgbase]}"
echo -e "Version : ${data[pkgver]}"
echo -e "Checksum (sha256) : ${data[pkgbuild_sha256sum]}"
echo -e "Packager : ${data[packager]}"
echo -e "Build Date : ${data[builddate]}"
echo -e "Build Directory : ${data[builddir]}"
echo -e "Build Environment : ${buildenv[*]}"
echo -e "Options : ${options[*]}"
echo -e "Installed Packages : ${#installed[@]}"
;;
"packages")
for ipkg in "${installed[@]}"; do
select_archive_link "${ipkg}"
done
;;
"field")
case "${1}" in
"buildenv")
echo -e "${buildenv[*]}"
;;
"options")
echo -e "${options[*]}"
;;
"installed")
echo -e "${installed[*]}"
;;
*)
echo -e "${data[${1}]}"
;;
esac
;;
"field_all")
for field in pkgname pkgbase pkgver pkgbuild_sha256sum packager builddate builddir; do
echo -e "${field}=${data[${field}]}"
done
echo -e "buildenv=${buildenv[*]}"
echo -e "options=${options[*]}"
echo -e "installed=${installed[*]}"
;;
"download")
if [ "$#" -gt 0 ]; then
readonly target_dir="${1}"
shift 1
if [[ -d "${target_dir}" ]]; then
for ipkg in "${installed[@]}"; do
download_archive_package "${ipkg}" "${target_dir}"
done
else
echo "Target directory doesn't exist" >&2
exit 1
fi
else
echo "Target directory not specified" >&2
exit 1
fi
;;
esac