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