Rename tactic -> predicate.
[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. "master".
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-source-packages:
38 # List of source packages that will be rebuilt.
39 #
40 # %fedora-ignored:
41 # List of source packages which will be ignored.  These are treated
42 # as if they have been rebuilt.
43 #
44 # %fedora-blocked:
45 # List of packages which are blocked.  Any dependent packages are
46 # also blocked. XXX NOT IMPLEMENTED
47 #
48 # %koji:            Name of koji binary, usually "koji".
49 # %fedpkg:          Name of fedpkg binary, usually "fedpkg".
50
51 # Check if the source package has been built in Koji.
52
53 predicate is-koji-built (pkg) = {
54     cd %fedora-dir/%pkg/%fedora-branch
55     koji=%koji
56     specfile=%pkg.spec
57
58     # Packages which are ignored are treated as if they were rebuilt already.
59     for p in %fedora-ignored; do
60         if [ %pkg = "$p" ]; then exit 0; fi
61     done
62
63     # If the specfile doesn't have the magic string then the
64     # package definitely needs to be rebuilt.
65     grep -sq %fedora-rebuild-name $specfile || exit 99
66
67     # Else we must check Koji itself.
68     # Koji sends some messages to stderr.
69     nvr=$(fedpkg verrel)
70     buildinfo=$($koji buildinfo $nvr 2>&1 ||:)
71
72     # No build at all, needs rebuild.
73     echo "$buildinfo" | grep -sq "No such build" && exit 99
74
75     # Existing build, find out what state it is in.
76     state=$(echo "$buildinfo" | grep ^State: | awk '{print $2}')
77     taskid=$(echo "$buildinfo" | grep ^Task: | awk '{print $2}')
78
79     case "$state" in
80     COMPLETE)
81         # Complete so we don't need to rebuild.
82         exit 0 ;;
83     FAILED)
84         # Failed builds must be examined and fixed manually.
85         exit 1 ;;
86     BUILDING)
87         # Cancel the build, we will resubmit it.
88         $koji cancel $taskid ||:
89         exit 99 ;;
90     CANCELED|DELETED)
91         # Do a rebuild.
92         exit 99 ;;
93     esac
94     # Don't know what happened there, so fail.
95     exit 1
96 }
97
98 # Rebuild a Fedora package.  This rebuilds any dependencies first.
99
100 goal fedora-rebuild (pkg) =
101 is-koji-built ("%pkg") : wrap ("is-koji-built",
102                                fedora-source-dependencies (pkg)) {
103     cd %fedora-dir/%pkg/%fedora-branch
104     fedpkg=%fedpkg
105     koji=%koji
106     specfile=%pkg.spec
107
108     # We have to wait for the dependencies to become available
109     # before we can start the new build.
110     for p in %<; do
111         nvr=$($koji --quiet latest-build %fedora-tag $p | awk '{print $1}')
112         $koji wait-repo %fedora-tag --build=$nvr
113     done
114
115     # Make sure the local directory is up to date.
116     # This should also fail if there are local changes, which
117     # would need to be corrected/integrated by hand.
118     git pull
119
120     # If the specfile doesn't have the magic string then add
121     # that now.
122     if ! grep -sq %fedora-rebuild-name $specfile; then
123         rpmdev-bumpspec -c "- "%fedora-rebuild-name *.spec
124     else
125         rpmdev-bumpspec -c "- Bump release and rebuild." *.spec
126     fi
127     $fedpkg commit -c
128     $fedpkg push
129     $fedpkg build --target %fedora-tag
130 }
131
132 # Get the source package names for a particular package.
133 # Note this is not merely the BuildRequires, since those are
134 # the binary packages.  Also this will only find packages
135 # which are in the list of fedora-source-packages.
136 pure function fedora-source-dependencies (pkg) returning strings = @{
137     echo Calculating dependencies of %pkg >&2
138
139     specfile=%fedora-dir/%pkg/%fedora-branch/%pkg.spec
140
141     # We will require the mapping of all source packages to the
142     # list of binary packages they build, so work this out in advance.
143     declare -A binpkgs
144     for p in %fedora-source-packages; do
145         binpkgs[$p]="$(
146             rpmspec -q --provides %fedora-dir/$p/%fedora-branch/$p.spec 2>/dev/null |
147             awk '{print $1}'
148         )"
149     done
150
151     for r in $(rpmspec -q --buildrequires $specfile 2>/dev/null |
152                awk '{print $1}'); do
153         # Now we examine each *other* source package to see
154         # if any will create this dependency when they build.
155         for p in %fedora-source-packages; do
156             if [ "$p" != %pkg ]; then
157                 for f in ${binpkgs[$p]}; do
158                     if [ "$f" = "$r" ]; then
159                         echo "$p"
160                     fi
161                 done
162             fi
163         done
164     done | sort -u
165 }