File: ubuntu-build

package info (click to toggle)
ubuntu-dev-tools 0.206
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,172 kB
  • sloc: python: 9,120; sh: 1,304; perl: 135; makefile: 10
file content (448 lines) | stat: -rwxr-xr-x 15,922 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/python3
#
#   ubuntu-build - command line interface for Launchpad buildd operations.
#
#   Copyright (C) 2007-2024 Canonical Ltd.
#   Authors:
#    - Martin Pitt <martin.pitt@canonical.com>
#    - Jonathan Davies <jpds@ubuntu.com>
#    - Michael Bienia <geser@ubuntu.com>
#    - Steve Langasek <steve.langasek@canonical.com>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, version 3 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# pylint: disable=invalid-name
# pylint: enable=invalid-name

import argparse
import sys

import lazr.restfulclient.errors
from launchpadlib.launchpad import Launchpad

from ubuntutools import getLogger
from ubuntutools.lp.udtexceptions import PocketDoesNotExistError
from ubuntutools.misc import split_release_pocket

Logger = getLogger()


def get_build_states(pkg, archs):
    res = []

    for build in pkg.getBuilds():
        if build.arch_tag in archs:
            res.append(f"  {build.arch_tag}: {build.buildstate}")
    msg = "\n".join(res)
    return f"Build state(s) for '{pkg.source_package_name}':\n{msg}"


def rescore_builds(pkg, archs, score):
    res = []

    for build in pkg.getBuilds():
        arch = build.arch_tag
        if arch in archs:
            if not build.can_be_rescored:
                continue
            try:
                build.rescore(score=score)
                res.append(f"  {arch}: done")
            except lazr.restfulclient.errors.Unauthorized:
                Logger.error(
                    "You don't have the permissions to rescore builds."
                    " Ignoring your rescore request."
                )
                return None
            except lazr.restfulclient.errors.BadRequest:
                Logger.info("Cannot rescore build of %s on %s.", build.source_package_name, arch)
                res.append(f"  {arch}: failed")

    msg = "\n".join(res)
    return f"Rescoring builds of '{pkg.source_package_name}' to {score}:\n{msg}"


def retry_builds(pkg, archs):
    res = []
    for build in pkg.getBuilds():
        arch = build.arch_tag
        if arch in archs:
            try:
                build.retry()
                res.append(f"  {arch}: done")
            except lazr.restfulclient.errors.BadRequest:
                res.append(f"  {arch}: failed")
    msg = "\n".join(res)
    return f"Retrying builds of '{pkg.source_package_name}':\n{msg}"


def main():
    # Usage.
    usage = "%(prog)s <srcpackage> <release> <operation>\n\n"
    usage += "Where operation may be one of: rescore, retry, or status.\n"
    usage += "Only Launchpad Buildd Admins may rescore package builds."

    # Valid architectures.
    valid_archs = set(
        ["armhf", "arm64", "amd64", "i386", "powerpc", "ppc64el", "riscv64", "s390x"]
    )

    # Prepare our option parser.
    parser = argparse.ArgumentParser(usage=usage)

    parser.add_argument(
        "-a",
        "--arch",
        action="append",
        dest="architecture",
        help=f"Rebuild or rescore a specific architecture. Valid architectures "
        f"include: {', '.join(valid_archs)}.",
    )

    parser.add_argument("-A", "--archive", help="operate on ARCHIVE", default="ubuntu")

    # Batch processing options
    batch_options = parser.add_argument_group(
        "Batch processing",
        "These options and parameter ordering is only "
        "available in --batch mode.\nUsage: "
        "ubuntu-build --batch [options] <package>...",
    )
    batch_options.add_argument(
        "--batch", action="store_true", dest="batch", help="Enable batch mode"
    )
    batch_options.add_argument(
        "--series",
        action="store",
        dest="series",
        help="Selects the Ubuntu series to operate on (default: current development series)",
    )
    batch_options.add_argument(
        "--retry", action="store_true", dest="retry", help="Retry builds (give-back)."
    )
    batch_options.add_argument(
        "--rescore",
        action="store",
        dest="priority",
        type=int,
        help="Rescore builds to <priority>.",
    )
    batch_options.add_argument(
        "--state",
        action="store",
        dest="state",
        help="Act on builds that are in the specified state",
    )

    parser.add_argument("packages", metavar="package", nargs="*", help=argparse.SUPPRESS)

    # Parse our options.
    args = parser.parse_args()

    launchpad = Launchpad.login_with("ubuntu-dev-tools", "production", version="devel")
    ubuntu = launchpad.distributions["ubuntu"]

    if args.batch:
        release = args.series
        if not release:
            # ppas don't have a proposed pocket so just use the release pocket;
            # but for the main archive we default to -proposed
            release = ubuntu.getDevelopmentSeries()[0].name
            if args.archive == "ubuntu":
                release = f"{release}-proposed"
        try:
            (release, pocket) = split_release_pocket(release)
        except PocketDoesNotExistError as error:
            Logger.error(error)
            sys.exit(1)
    else:
        # Check we have the correct number of arguments.
        if len(args.packages) < 3:
            parser.error("Incorrect number of arguments.")

        try:
            package = str(args.packages[0]).lower()
            release = str(args.packages[1]).lower()
            operation = str(args.packages[2]).lower()
        except IndexError:
            parser.print_help()
            sys.exit(1)

    archive = launchpad.archives.getByReference(reference=args.archive)
    try:
        distroseries = ubuntu.getSeries(name_or_version=release)
    except lazr.restfulclient.errors.NotFound as error:
        Logger.error(error)
        sys.exit(1)

    if not args.batch:
        # Check our operation.
        if operation not in ("rescore", "retry", "status"):
            Logger.error("Invalid operation: %s.", operation)
            sys.exit(1)

        # If the user has specified an architecture to build, we only wish to
        # rebuild it and nothing else.
        if args.architecture:
            if args.architecture[0] not in valid_archs:
                Logger.error("Invalid architecture specified: %s.", args.architecture[0])
                sys.exit(1)
            else:
                one_arch = True
        else:
            one_arch = False

        # split release and pocket
        try:
            (release, pocket) = split_release_pocket(release)
        except PocketDoesNotExistError as error:
            Logger.error(error)
            sys.exit(1)

        # Get list of published sources for package in question.
        try:
            sources = archive.getPublishedSources(
                distro_series=distroseries,
                exact_match=True,
                pocket=pocket,
                source_name=package,
                status="Published",
            )[0]
        except IndexError:
            Logger.error("No publication found for package %s", package)
            sys.exit(1)
        # Get list of builds for that package.
        builds = sources.getBuilds()

        # Find out the version and component in given release.
        version = sources.source_package_version
        component = sources.component_name

        # Operations that are remaining may only be done by Ubuntu developers
        # (retry) or buildd admins (rescore). Check if the proper permissions
        # are in place.
        if operation == "retry":
            necessary_privs = archive.checkUpload(
                component=sources.getComponent(),
                distroseries=distroseries,
                person=launchpad.me,
                pocket=pocket,
                sourcepackagename=sources.getPackageName(),
            )
            if not necessary_privs:
                Logger.error(
                    "You cannot perform the %s operation on a %s package as you"
                    " do not have the permissions to do this action.",
                    operation,
                    component,
                )
                sys.exit(1)

        # Output details.
        Logger.info(
            "The source version for '%s' in %s (%s) is at %s.",
            package,
            release.capitalize(),
            component,
            version,
        )

        Logger.info("Current build status for this package:")

        # Output list of arches for package and their status.
        done = False
        for build in builds:
            if one_arch and build.arch_tag != args.architecture[0]:
                # Skip this architecture.
                continue

            done = True
            Logger.info("%s: %s.", build.arch_tag, build.buildstate)
            if operation == "rescore":
                if build.can_be_rescored:
                    # FIXME: make priority an option
                    priority = 5000
                    Logger.info("Rescoring build %s to %d...", build.arch_tag, priority)
                    try:
                        build.rescore(score=priority)
                    except lazr.restfulclient.errors.Unauthorized:
                        Logger.error(
                            "You don't have the permissions to rescore builds."
                            " Ignoring your rescore request."
                        )
                        break
                else:
                    Logger.info("Cannot rescore build on %s.", build.arch_tag)
            if operation == "retry":
                if build.can_be_retried:
                    Logger.info("Retrying build on %s...", build.arch_tag)
                    build.retry()
                else:
                    Logger.info("Cannot retry build on %s.", build.arch_tag)

        # We are done
        if done:
            sys.exit(0)

        Logger.info("No builds for '%s' found in the %s release", package, release.capitalize())
        Logger.info("It may have been built in a former release.")
        sys.exit(0)

    # Batch mode

    if not args.architecture:
        # no specific architectures specified, assume all valid ones
        archs = valid_archs
    else:
        archs = set(args.architecture)

    # filter out duplicate and invalid architectures
    archs.intersection_update(valid_archs)

    if not args.packages:
        retry_count = 0
        can_rescore = True

        if not args.state:
            if args.retry:
                args.state = "Failed to build"
            elif args.priority:
                args.state = "Needs building"
        # there is no equivalent to series.getBuildRecords() for a ppa.
        # however, we don't want to have to traverse all build records for
        # all series when working on the main archive, so we use
        # series.getBuildRecords() for ubuntu and handle ppas separately
        series = ubuntu.getSeries(name_or_version=release)
        if args.archive == "ubuntu":
            builds = series.getBuildRecords(build_state=args.state, pocket=pocket)
        else:
            builds = []
            for build in archive.getBuildRecords(build_state=args.state, pocket=pocket):
                if not build.current_source_publication:
                    continue
                if build.current_source_publication.distro_series == series:
                    builds.append(build)
        for build in builds:
            if build.arch_tag not in archs:
                continue
            if not build.current_source_publication:
                continue
            # fixme: refactor
            # Check permissions (part 2): check upload permissions for the
            # source package
            can_retry = args.retry and archive.checkUpload(
                component=build.current_source_publication.component_name,
                distroseries=series,
                person=launchpad.me,
                pocket=pocket,
                sourcepackagename=build.source_package_name,
            )
            if args.retry and not can_retry:
                Logger.error(
                    "You don't have the permissions to retry the build of '%s', skipping.",
                    build.source_package_name,
                )
                continue
            Logger.info(
                "The source version for '%s' in '%s' (%s) is: %s",
                build.source_package_name,
                release,
                pocket,
                build.source_package_version,
            )

            if args.retry and build.can_be_retried:
                Logger.info(
                    "Retrying build of %s on %s...", build.source_package_name, build.arch_tag
                )
                try:
                    build.retry()
                    retry_count += 1
                except lazr.restfulclient.errors.BadRequest:
                    Logger.info(
                        "Failed to retry build of %s on %s",
                        build.source_package_name,
                        build.arch_tag,
                    )

            if args.priority and can_rescore:
                if build.can_be_rescored:
                    try:
                        build.rescore(score=args.priority)
                    except lazr.restfulclient.errors.Unauthorized:
                        Logger.error(
                            "You don't have the permissions to rescore builds."
                            " Ignoring your rescore request."
                        )
                        can_rescore = False
                    except lazr.restfulclient.errors.BadRequest:
                        Logger.info(
                            "Cannot rescore build of %s on %s.",
                            build.source_package_name,
                            build.arch_tag,
                        )

            Logger.info("")
        if args.retry:
            Logger.info("%d package builds retried", retry_count)
        sys.exit(0)

    for pkg in args.packages:
        try:
            pkg = archive.getPublishedSources(
                distro_series=distroseries,
                exact_match=True,
                pocket=pocket,
                source_name=pkg,
                status="Published",
            )[0]
        except IndexError:
            Logger.error("No publication found for package %s", pkg)
            continue

        # Check permissions (part 2): check upload permissions for the source
        # package
        can_retry = args.retry and archive.checkUpload(
            component=pkg.component_name,
            distroseries=distroseries,
            person=launchpad.me,
            pocket=pocket,
            sourcepackagename=pkg.source_package_name,
        )
        if args.retry and not can_retry:
            Logger.error(
                "You don't have the permissions to retry the "
                "build of '%s'. Ignoring your request.",
                pkg.source_package_name,
            )

        Logger.info(
            "The source version for '%s' in '%s' (%s) is: %s",
            pkg.source_package_name,
            release,
            pocket,
            pkg.source_package_version,
        )

        Logger.info(get_build_states(pkg, archs))
        if can_retry:
            Logger.info(retry_builds(pkg, archs))
        if args.priority:
            Logger.info(rescore_builds(pkg, archs, args.priority))

        Logger.info("")


if __name__ == "__main__":
    main()