stdlib/fedora: Use grep -F when matching %fedora-rebuild-name
[goals.git] / stdlib / fedora.gl
1 # Goals stdlib Fedora module.
2 # Copyright (C) 2020 Richard W.M. Jones
3 # Copyright (C) 2020 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 # Goals for dealing with Fedora builds.
20
21 # For these to work you must set up your environment in
22 # a particular way as described below:
23 #
24 # %fedora-dir:
25 # Points to a directory containing Fedora packages checked out
26 # of dist git.  You must use the fedpkg clone -B command to
27 # create separate branches.
28 #
29 # %fedora-branch:   Working branch, eg. "rawhide".
30 # %fedora-tag:      Build tag, eg. "f32-build".
31 #
32 # %fedora-rebuild-name:
33 # Magic string that identifies this rebuild.  This is used to
34 # detect if a rebuild has been attempted before in this run.
35 # eg. "Perl 5.99 rebuild"
36 #
37 # %fedora-retry-failed:
38 # If set to 1 then failed builds are retried automatically.
39 # If set to 0 then failed builds must be manually restarted.
40 # Normally you want this to be 0.
41 #
42 # %fedora-source-packages:
43 # List of source packages that will be rebuilt.
44 #
45 # %fedora-ignored:
46 # List of source packages which will be ignored.  These are treated
47 # as if they have been rebuilt.
48 #
49 # %fedora-blocked:
50 # List of packages which are blocked.  Any dependent packages are
51 # also blocked. XXX NOT IMPLEMENTED
52
53 # Check if the source package has been built in Koji.
54
55 predicate is-koji-built (pkg) = {
56     cd %fedora-dir/%pkg/%fedora-branch
57     specfile=%pkg.spec
58
59     # Packages which are ignored are treated as if they were rebuilt already.
60     for p in %fedora-ignored; do
61         if [ %pkg = "$p" ]; then exit 0; fi
62     done
63
64     if ! grep -sq "^%%autochangelog" $specfile; then
65         # If the specfile doesn't have the magic string then the
66         # package definitely needs to be rebuilt.
67         grep -sqF %fedora-rebuild-name $specfile || exit 99
68     else
69         # Using autochangelog.
70         git log | grep -sqF %fedora-rebuild-name || exit 99
71     fi
72
73     # Else we must check Koji itself.
74     # Koji sends some messages to stderr.
75     nvr=$(fedpkg verrel)
76     buildinfo=$(koji buildinfo $nvr 2>&1 ||:)
77
78     # No build at all, needs rebuild.
79     echo "$buildinfo" | grep -sq "No such build" && exit 99
80
81     # Existing build, find out what state it is in.
82     state=$(echo "$buildinfo" | grep ^State: | awk '{print $2}')
83     taskid=$(echo "$buildinfo" | grep ^Task: | awk '{print $2}')
84
85     case "$state" in
86     COMPLETE)
87         # Complete so we don't need to rebuild.
88         exit 0 ;;
89     FAILED)
90         if [ %fedora-retry-failed -ne 0 ]; then
91             # Resubmit it automatically.
92             exit 99
93         else
94             # Fail, must be examined and retried manually.
95             exit 1
96         fi ;;
97     BUILDING)
98         # Cancel the build, we will resubmit it.
99         koji cancel $taskid ||:
100         exit 99 ;;
101     CANCELED|DELETED)
102         # Do a rebuild.
103         exit 99 ;;
104     esac
105     # Don't know what happened there, so fail.
106     exit 1
107 }
108
109 # Rebuild a Fedora package.  This rebuilds any dependencies first.
110
111 goal fedora-rebuild (pkg) =
112 is-koji-built ("%pkg") :
113         wrap ("is-koji-built", fedora-source-dependencies (pkg)) {
114     cd %fedora-dir/%pkg/%fedora-branch
115     specfile=%pkg.spec
116
117     # We have to wait for the dependencies to become available
118     # before we can start the new build.
119     for p in %<; do
120         nvr=$(koji --quiet latest-build %fedora-tag $p | awk '{print $1}')
121         test -n "$nvr" || exit 1
122         while ! koji wait-repo --timeout=10000 %fedora-tag --build=$nvr; do sleep 1m; done
123     done
124
125     # Make sure the local directory is up to date.
126     # This should also fail if there are local changes, which
127     # would need to be corrected/integrated by hand.
128     git pull
129
130     # Add the new commit.
131     if ! grep -sq "^%%autochangelog" $specfile; then
132         # If we're not building for Rawhide then we must use the
133         # rpmdev-bumpspec rightmost (-r) flag so there's an upgrade
134         # path to Rawhide.
135         rightmost=
136         if test %fedora-branch != "rawhide"; then rightmost=-r; fi
137
138         # If the specfile doesn't have the magic string then add
139         # that now.
140         if ! grep -sqF %fedora-rebuild-name $specfile; then
141             rpmdev-bumpspec -c "- "%fedora-rebuild-name $rightmost *.spec
142         else
143             rpmdev-bumpspec -c "- Bump release and rebuild" $rightmost *.spec
144         fi
145
146         fedpkg commit -c
147     else
148         # Using autochangelog.
149         if ! git log | grep -sqF %fedora-rebuild-name; then
150             git commit --allow-empty -m %fedora-rebuild-name
151         else
152             git commit --allow-empty -m "Bump release and rebuild"
153         fi
154     fi
155
156     # Push the new commit.
157     fedpkg push
158
159     # Do the Koji build in the side tag.
160     fedpkg build --target %fedora-tag
161 }
162
163 # Get the source package names for a particular package.
164 # Note this is not merely the BuildRequires, since those are
165 # the binary packages.  Also this will only find packages
166 # which are in the list of fedora-source-packages.
167 pure function fedora-source-dependencies (pkg) returning strings = @{
168     echo Calculating dependencies of %pkg >&2
169
170     specfile=%fedora-dir/%pkg/%fedora-branch/%pkg.spec
171
172     # We will require the mapping of all source packages to the
173     # list of binary packages they build, so work this out in advance.
174     declare -A bin2src
175     for p in %fedora-source-packages; do
176         p_specfile=%fedora-dir/$p/%fedora-branch/$p.spec
177         p_depcache=%fedora-dir/$p/%fedora-branch/.depcache
178         if ! test -f "$p_depcache" || test "$p_specfile" -nt "$p_depcache";
179         then
180             rm -f $p_depcache
181             for b in $(rpmspec -q --provides $p_specfile 2>/dev/null | awk '{print $1}'); do
182                 echo "bin2src['$b']='$p'" >> $p_depcache
183             done
184         fi
185         source $p_depcache
186     done
187
188     for b in $(rpmspec -q --buildrequires $specfile 2>/dev/null |
189                awk '{print $1}'); do
190         # Find the source package that produces these binary requirements.
191         echo ${bin2src[$b]}
192     done | grep -v '^$' | sort -u
193 }