summaryrefslogtreecommitdiff
path: root/src/abslibre-tools/libredbdiff
blob: 8aa6ce105359ded4423d9bb7a9a894b6230ae88d (plain)
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
326
327
328
329
330
#!/usr/bin/env bash
# libredbdiff
#
# Copyright (C) 2014 Esteban Carnevale <alfplayer@mailoo.org>
# Copyright (C) 2014, 2017 Luke Shumaker <lukeshu@sbcglobal.net>
#
# License: GNU GPLv3+
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -euE -o pipefail
. "$(librelib messages)"
. "$(librelib conf)"
load_files libredbdiff || exit
setup_traps

declare -r name="Libredbdiff"
declare -r cmd="${0##*/}"

declare -r conffile_prbl="$statedir/pacman.conf.parabola"
declare -r conffile_arch="$statedir/pacman.conf.archlinux"

declare -r dbpath_prbl="$statedir/pacman.parabola"
declare -r dbpath_arch="$statedir/pacman.archlinux"

declare -r mirrorlist_prbl="$statedir/mirrorlist.parabola"
declare -r mirrorlist_arch="$statedir/mirrorlist.archlinux"

declare -r field_pkgname_prbl=30
declare -r field_pkgname_arch=30
declare -r field_pkgname_total="$((field_pkgname_prbl + field_pkgname_arch))"

declare -r printf_format="%s  %-${field_pkgname_prbl}s%-${field_pkgname_arch}s   %s | %s\n"
declare -r printf_format_noarch="%s  %-${field_pkgname_total}s   %s\n"

enablerepo() {
	local repo="$1"
	local conffile_arg="$2"

	msg2 "Enabling repo %q in %q" "$repo" "$conffile_arg"
	sed -i "s/\#\[$repo\]/[$repo]/" "$conffile_arg"
	sed -i "\/\[$repo\]/,+1 s/#Include/Include/" "$conffile_arg"
}

createdir() {
	local dir=$1

	if ! [[ -d "$dir" ]] ; then
		msg "Creating directory %q" "$dir"
		mkdir -- "$dir" || die "Failed to create directory %q.  Exiting." "$dir"
	fi
}

# {{ A -lt B }} is like [ A -lt B ], but for pacman version numbers
{{() {
	  [[ $# = 4 ]] || panic
	  [[ $4 = '}}' ]] || panic
	  local a=$1
	  local op=$2
	  local b=$3

	  local cmp
	  cmp="$(vercmp "$a" "$b")" || die "vercmp failed"
	  [ "$cmp" "$op" 0 ]
}

# Globals:
#  - pkgname  : the Parabola pkgname
#  - ver_prbl : a map of pkgname->arch_pkgver
#  - ver_arch : a map of pkgname->prbl_pkgver
#  - provides : a map of pkgname->provides
compare_pkgs() {
	if [[ -n "${ver_arch[$pkgname]:-}" ]]; then
		if {{ "${ver_prbl[$pkgname]}" -lt "${ver_arch[$pkgname]}" }}; then
			printf "$printf_format" \
				'=' \
				"$pkgname" \
				"" \
				"${ver_prbl[$pkgname]}" \
				"${ver_arch[$pkgname]}"
		fi
	elif [[ -n "${provides[$pkgname]:-}" ]]; then
		local _provides provide
		read -r -a _provides <<<"${provides[$pkgname]}"
		for provide in "${_provides[@]}"; do
			if [[ -n "${ver_arch[$provide]:-}" ]]; then
				if {{ "${ver_prbl[$pkgname]}" -lt "${ver_arch[$provide]}" }}; then
					printf "$printf_format" \
						'p' \
						"$pkgname" \
						"$provide" \
						"${ver_prbl[$pkgname]}" \
						"${ver_arch[$provide]}"
				fi
			fi
		done
	else
		printf "$printf_format_noarch" \
			'o' \
			"$pkgname" \
			"${ver_prbl[$pkgname]}"
	fi
}

# Globals:
#  - prbl_packages_tmp
#  - ver_prbl
#  - ver_arch (transitively through compare_pkgs)
#  - provides
print_cmp() {
	local repo="$1"

	< "$prbl_packages_tmp" \
		awk -F/ -v repo="$repo" '$1 == repo {print $2}' |
		while read -a line ; do
			ver_prbl["${line[0]}"]="${line[1]}"
			provides["${line[0]}"]="${line[*]:2}"
			pkgname="${line[0]}"
			compare_pkgs
		done
}

usage() {
	print "Usage: sudo %q" "$cmd"
	print "   or: %q -n [REPO]" "$cmd"
	print "   or: %q -h" "$cmd"
	print 'Show packages that need to be updated from Arch repositories.'
	echo
	prose "Compares packages in Parabola repositories.  Packages from
	       all configured Parabola repositories are compared.  A Parabola
	       repository name can be specified as argument to compare only
	       packages in that repository."
	echo
	prose "The default mode of operation is to download/update all necessary
	       files for comparison, but not compare them.  Specify the \`-n\`
	       flag to not download anything, but to compare already downloaded
	       files."
	echo
	prose "In the compare mode (if the \`-n\` flag is given), by default it
	       will iterate over each of the configured repositories, prefixing
	       each section with a line in the format \`[REPO]\`.  However, if a
	       REPO is specified on the command line, it scan for packages only
	       in that repository, and no \`[REPO]\` line will be printed."
	echo
	print 'Options:'
	flag '-n' "Don't update anything, just compare already downloaded files."
	flag '-h' 'Show this message'
	echo
	print "Output format:"
	print "type_character  parabola_pkgname  (arch_pkgname)   parabola_pkgver - (arch_pkgver)"
	echo
	print "Type characters:"
	flag '=' "Arch package with the same pkgname and greater pkgver was found"
	flag 'p' "Arch package with pkgname equal to a provide and greater
	          pkgver was found In this case arch_pkgname is a provide of
	          parabola_pkgname"
	flag 'o' "No Arch package with the same pkgname or with pkgname equal to
	          a provide was found"
}

main() {
	local args arg mode=update badargs=false
	args="$(getopt -n "$cmd" -o nh -l noupdate,help -- "$@")" || badargs=true
	eval "set -- $args"
	while [[ $# -gt 0 ]]; do
		arg=$1
		shift
		case "$arg" in
			-n|--noupdate)
				mode=compare
				;;
			-h|--help)
				usage
				return 0
				;;
			--)
				break
				;;
		esac
	done
	if $badargs; then
		usage >&2
		return 2
	fi

	case $mode in
		update)
			if [[ $# -gt 0 ]]; then
				print "%s: found non-flag arguments; a repo list may only be specified with the -n flag: %s" "$cmd" "$*" >&2
				usage >&2
				return 2
			fi
			main_update
			;;
		compare)
			if [[ $# -gt 1 ]]; then
				print "%s: found too many non-flag arguments; only one repo may be specified: %s" "$cmd" "$*" >&2
				usage >&2
				return 2
			fi
			if [[ $# = 1 ]]; then
				if ! in_array "$1" "${repos[@]}"; then
					die "The specified Parabola repo \"%s\" cannot be compared.  It's not in the list of repos in the configuration variable \"repos\"." "$1"
				fi
			fi
			main_compare "$@"
			;;
	esac
}

main_update() {
	if [[ $EUID != 0 ]]; then
		die "To initialize %s or update %s pacman databases, %s must be run as root (without arguments).  Nothing done." \
		    "$name" \
		    "$name" \
		    "$cmd"
	fi
	check_vars libredbdiff statedir mirror_prbl mirror_arch || exit 1

	createdir "$statedir"
	{
		msg "Generating %s %q" Parabola pacman.conf
		cp -T /usr/share/pacman/defaults/pacman.conf.x86_64 "$conffile_prbl"
		sed -r \
		    -e "s|/etc/pacman\.d/mirrorlist$|$mirrorlist_prbl|" \
		    -e "s|^#?DBPath\s*=.*|DBPath = $dbpath_prbl|" \
		    -e "s|^#?Architecture\s*=.*|Architecture = x86_64|" \
		    -i "$conffile_prbl"
		local repo
		for repo in multilib {libre,pcr,nonprism}{,-multilib}; do
			enablerepo "$repo" "$conffile_prbl"
		done

		msg "Generating %s %q" Arch pacman.conf
		wget -q \
		     -O "$conffile_arch" \
		     "https://git.archlinux.org/svntogit/packages.git/plain/pacman/repos/core-x86_64/pacman.conf.x86_64" ||
			die "Failed to download %q.  Exiting." "$conffile_arch"
		sed -r \
		    -e "s|/etc/pacman\.d/mirrorlist$|$mirrorlist_arch|" \
		    -e "s|^#?DBPath\s*=.*|DBPath = $dbpath_arch|" \
		    -e "s|^#?Architecture\s*=.*|Architecture = x86_64|" \
		    -i "$conffile_arch"
		enablerepo multilib "$conffile_arch"

		printf 'Server = %s\n' "$mirror_prbl" > "$mirrorlist_prbl"

		printf 'Server = %s\n' "$mirror_arch" > "$mirrorlist_arch"
	}

	createdir "$dbpath_prbl"
	{
		msg "Synchronizing %s pacman databases for %s" "$name" "Parabola"
		pacman --config "$conffile_prbl" -Sy ||
			die "Failed to synchronize pacman database for %s.  Exiting." Parabola
	}

	createdir "$dbpath_arch"
	{
		msg "Synchronizing %s pacman databases for %s" "$name" "Arch"
		pacman --config "$conffile_arch" -Sy ||
			die "Failed to synchronize pacman database for %s.  Exiting." Arch
	}

	msg "%s pacman databases are updated.  %s is ready.  Run %q -n to print results." \
	    "$name" "$name" "$cmd"
}

main_compare() {
	check_vars libredbdiff statedir repos || exit 1

	local conffiles=(
		"$dbpath_prbl/sync/libre.db"
		"$dbpath_arch/sync/core.db"
		"$conffile_prbl"
		"$conffile_arch"
	)
	local file
	for file in "${conffiles[@]}"; do
		if ! [[ -f "$file" && -r "$file" ]]; then
			error "Could not read %q." "$file"
			die "Nothing done.  It may be necessary to run %q without \
arguments as root to initialize %s." "$cmd" "$name"
		fi
	done

	local tmpdir
	tmpdir="$(mktemp --tmpdir -d "$cmd.XXXXXXXXXX")" || die "Could not create temporary working directory"
	trap "rm -rf -- $(printf %q "$tmpdir")" RETURN
	local arch_packages_tmp="$tmpdir/arch-packages"
	local prbl_packages_tmp="$tmpdir/parabola-packages"

	unset provides ver_prbl ver_arch
	declare -gA provides ver_prbl ver_arch

	expac --config "$conffile_arch" -S '%n %v' \
		> "$arch_packages_tmp" || \
		die "expac command to get %s package data has failed.  Exiting." Arch

	local pkgname pkgver
	while read -r pkgname pkgver; do
		ver_arch["$pkgname"]="$pkgver"
	done < "$arch_packages_tmp"

	expac --config "$conffile_prbl" -S '%r/%n %v %S' \
		> "$prbl_packages_tmp" || \
		die "expac command to get %s package data has failed.  Exiting." Parabola

	if [[ $# == 1 ]]; then
		print_cmp "$1"
	else
		for repo in "${repos[@]}" ; do
			echo "[$repo]"
			print_cmp "$repo"
		done
	fi
}

main "$@"