Fix mistake in dnf-repoquery-by-srpm documentation.
[utils/utils.git] / dnf-repoquery-by-srpm
1 #!/bin/bash
2 # dnf-repoquery-by-srpm [dnf-options...] srpm-name...
3 #
4 # List every package built from one of the given source RPMs
5 # (given by full name as in the SOURCERPM tag, e.g., glibc-2.31-4.fc32.src.rpm).
6 #
7 # This script's option parser is crude and assumes that every argument that
8 # doesn't begin with a dash is a source RPM name, so you must use the
9 # "--opt=val" form of dnf options, not "--opt val".  Also, if you customize
10 # the query format, it must not contain an embedded newline or \x01.
11 #
12 # Ideally "dnf repoquery" would support arbitrary query tags like rpm
13 # (i.e., "rpm -qa SOURCERPM=...") and we wouldn't need this script.
14 #
15 # ~ Matt 2020-09-02
16
17 set -e
18 set -x
19 set -o pipefail
20
21 dnf_options=()
22 orig_queryformat="%{name}-%{evr}.%{arch}"
23 grep_options=()
24 for arg in "$@"; do
25   case "$arg" in
26     (--qf=*)
27       orig_queryformat="${arg#--qf=}";;
28     (--queryformat=*)
29       orig_queryformat="${arg#--queryformat=}";;
30     (-*)
31       dnf_options+=("$arg");;
32     (*)
33       # Hope . is the only character special in a basic regular expression that
34       # occurs in package filenames.
35       grep_options+=(-e $'\x01'"${arg//./\.}\$");;
36   esac
37 done
38
39 real_queryformat="$orig_queryformat"$'\x01'"%{sourcerpm}"
40
41 dnf repoquery --queryformat="$real_queryformat" "${dnf_options[@]}" \
42   | { grep "${grep_options[@]}" || true; } \
43   | sed -e $'s,\x01.*$,,'