2019-10-31 13:15:39 +03:00
#!/bin/sh
# script based on initial implementation
# by Vitaly Gusach (http://gusach.org.ua)
#
# purpose: warn on those entries in given packagelists(s)
# which are definitely absent; the build might still bail out
# but at least 80% of failures can be predicted early now
#
2021-08-20 12:38:13 +03:00
# usage: check-pkg-list [-n pkgnames] [--aptbox $PATH/to/aptbox] pkglist ...
2019-10-31 13:15:39 +03:00
# (pkgnames file should contain `apt-cache pkgnames`)
# NB: -n pkgnames MUST go first, if given
2021-08-20 17:56:10 +03:00
RET_ERROR=
2022-04-09 20:44:39 +03:00
error() {
echo `basename $0`: $* >&2
exit 1
}
2019-10-31 13:15:39 +03:00
exit_handler() {
local rc=$?
trap - EXIT
2021-08-20 17:18:51 +03:00
rm -f -- "$ftemp" "$fpkgnames" "$fpkgwildcards" "$favaillist" "$fpkgerrors"
2019-10-31 13:15:39 +03:00
exit $rc
}
# figure out apt.conf from recent aptbox or fallback to system one
dump_pkgnames() {
2021-08-20 12:38:13 +03:00
${APTBOX:+$APTBOX/}apt-cache pkgnames | sort -u > "$favaillist"
2019-10-31 13:15:39 +03:00
}
check_pkglist() {
fprofilelist="$1"
[ -f "$fprofilelist" ] || error "invalid packagelist filename: $fprofilelist"
# cleaning pkg list from comments, empty lines,
# splitting several pkgnames on the same line
sed -e '/^#/d' -e '/^[ ]*$/d' -e 's/ \+$//' -e 's/[ ]\+/\n/g' \
< "$fprofilelist" \
| sed 's/-$//' \
| sort -u \
> "$ftemp" # got list of pkgnames we need
# split pkgnames without wildcards and with wildcards
2022-05-19 05:15:00 +03:00
grep -F -v '*' "$ftemp" > "$fpkgnames"
2022-05-24 02:07:32 +03:00
grep -F '*' "$ftemp" > "$fpkgwildcards"
2019-10-31 13:15:39 +03:00
# return unavailable packages
2021-08-20 17:18:51 +03:00
comm -23 "$fpkgnames" "$favaillist" > "$fpkgerrors"
2019-10-31 13:15:39 +03:00
# return unavailable wildcards
while read i; do
# replacing * with regexp's \.+
pattern="^`echo ${i#^} | sed -e 's/\*/.\\\\+/'`$"
2021-08-20 17:18:51 +03:00
grep -q "$pattern" "$favaillist" || echo "$i" >> "$fpkgerrors"
2019-10-31 13:15:39 +03:00
done < "$fpkgwildcards"
2021-08-20 17:18:51 +03:00
if [ -s "$fpkgerrors" ]; then
2022-04-09 20:44:39 +03:00
echo "** error: Packages are not available in $fprofilelist:" >&2
2021-08-20 17:56:10 +03:00
RET_ERROR=1
2021-08-20 17:18:51 +03:00
cat $fpkgerrors >&2
fi
2019-10-31 13:15:39 +03:00
}
[ "$#" -gt 0 ] || error "need at least one argument, a packagelist to check"
# reusable temporary files with self-cleanup at exit
TEMP="${TMP:-/tmp}"
trap exit_handler HUP INT QUIT TERM EXIT
favaillist="`mktemp $TEMP/pkgchecker.avail.XXXXX`"
fpkgnames="`mktemp $TEMP/pkgchecker.names.XXXXX`"
fpkgwildcards="`mktemp $TEMP/pkgchecker.wildcards.XXXXX`"
2021-08-20 17:18:51 +03:00
fpkgerrors="`mktemp $TEMP/pkgchecker.error.XXXXX`"
2019-10-31 13:15:39 +03:00
ftemp="`mktemp $TEMP/pkgchecker.XXXXX`"
2021-08-20 12:38:13 +03:00
# check args
while :; do
case "$1" in
# make sure pkgnames dump is handy
"-n"|"--pkgnames")
[ -f "$2" ] && {
sort "$2" > "$favaillist"
shift; shift
} || error "-n needs valid pkgnames filename"
;;
# PATH to aptbox
"--aptbox")
[ -d "$2" ] && {
APTBOX="$2"
shift; shift
} || error "--aptbox needs valid directory"
;;
*)
break
;;
esac
done
2019-10-31 13:15:39 +03:00
[ -s "$favaillist" ] || dump_pkgnames
for list in "$@"; do
check_pkglist "$list"
done
2021-08-20 17:56:10 +03:00
[ -z "$RET_ERROR" ] || error "Some lists contain unavailable packages"