~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
3147.5.35 by Celso Providelo
Applying review comments [r=bjornt]
5
6
7
"""Tool for 'mass-retrying' build records.
8
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
9
It supports build collections based distroseries and/or distroarchseries.
3147.5.29 by Celso Providelo
add experimental script for mass-retry builds
10
"""
11
3147.5.35 by Celso Providelo
Applying review comments [r=bjornt]
12
__metaclass__ = type
13
3147.5.29 by Celso Providelo
add experimental script for mass-retry builds
14
import _pythonpath
3147.5.35 by Celso Providelo
Applying review comments [r=bjornt]
15
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
16
import transaction
3147.5.29 by Celso Providelo
add experimental script for mass-retry builds
17
from zope.component import getUtility
18
11270.1.3 by Tim Penhey
Changed NotFoundError imports - gee there were a lot of them.
19
from lp.app.errors import NotFoundError
11458.1.1 by Jelmer Vernooij
Move enums of buildmaster.
20
from lp.buildmaster.enums import BuildStatus
7675.575.1 by William Grant
Move lp.soyuz.interfaces.build.BuildStatus to lp.buildmaster.interfaces.buildbase. Sort various imports along the way.
21
from lp.registry.interfaces.distribution import IDistributionSet
22
from lp.registry.interfaces.pocket import PackagePublishingPocket
13970.7.8 by William Grant
And make it a bit more native.
23
from lp.services.scripts.base import LaunchpadScript, LaunchpadScriptFailure
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
24
25
26
class BuilddMassRetryScript(LaunchpadScript):
27
28
    dbuser = "fiera"
29
30
    def add_my_options(self):
31
        self.parser.add_option(
32
            "-d", "--distribution", dest="distribution",
33
            metavar="DISTRIBUTION", default="ubuntu",
34
            help="distribution name")
35
36
        self.parser.add_option(
37
            "-s", "--suite", dest="suite", metavar="SUITE", help="suite name")
38
39
        self.parser.add_option(
40
            "-a", "--architecture", dest="architecture", metavar="ARCH",
41
            help="architecture tag")
42
43
        self.parser.add_option(
44
            "-N", "--dry-run", action="store_true", dest="dryrun",
45
            metavar="DRY_RUN", default=False,
46
            help="Whether to treat this as a dry-run or not.")
47
48
        self.parser.add_option(
49
            "-F", "--failed", action="store_true", dest="failed",
50
            default=False, help="Reset builds in FAILED state.")
51
52
        self.parser.add_option(
53
            "-D", "--dep-wait", action="store_true", dest="depwait",
54
            default=False, help="Reset builds in DEPWAIT state.")
55
56
        self.parser.add_option(
57
            "-C", "--chroot-wait", action="store_true", dest="chrootwait",
58
            default=False, help="Reset builds in CHROOTWAIT state.")
59
60
    def main(self):
61
        try:
13970.7.8 by William Grant
And make it a bit more native.
62
            distribution = getUtility(IDistributionSet)[
63
                self.options.distribution]
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
64
        except NotFoundError, info:
13970.7.8 by William Grant
And make it a bit more native.
65
            raise LaunchpadScriptFailure("Distribution not found: %s" % info)
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
66
67
        try:
13970.7.8 by William Grant
And make it a bit more native.
68
            if self.options.suite is not None:
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
69
                series, pocket = distribution.getDistroSeriesAndPocket(
13970.7.8 by William Grant
And make it a bit more native.
70
                    self.options.suite)
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
71
            else:
72
                series = distribution.currentseries
73
                pocket = PackagePublishingPocket.RELEASE
74
        except NotFoundError, info:
13970.7.8 by William Grant
And make it a bit more native.
75
            raise LaunchpadScriptFailure("Suite not found: %s" % info)
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
76
77
        # store distroseries as the current IHasBuildRecord provider
78
        build_provider = series
79
13970.7.8 by William Grant
And make it a bit more native.
80
        if self.options.architecture:
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
81
            try:
13970.7.8 by William Grant
And make it a bit more native.
82
                dar = series[self.options.architecture]
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
83
            except NotFoundError, info:
13970.7.8 by William Grant
And make it a bit more native.
84
                raise LaunchpadScriptFailure(info)
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
85
86
            # store distroarchseries as the current IHasBuildRecord provider
87
            build_provider = dar
88
13970.7.8 by William Grant
And make it a bit more native.
89
        self.logger.info(
90
            "Initializing Build Mass-Retry for '%s/%s'"
91
            % (build_provider.title, pocket.name))
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
92
93
        requested_states_map = {
13970.7.8 by William Grant
And make it a bit more native.
94
            BuildStatus.FAILEDTOBUILD: self.options.failed,
95
            BuildStatus.MANUALDEPWAIT: self.options.depwait,
96
            BuildStatus.CHROOTWAIT: self.options.chrootwait,
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
97
            }
98
99
        # XXX cprov 2006-08-31: one query per requested state
100
        # could organise it in a single one nicely if I have
101
        # an empty SQLResult instance, than only iteration + union()
102
        # would work.
103
        for target_state, requested in requested_states_map.items():
104
            if not requested:
105
                continue
106
13970.7.8 by William Grant
And make it a bit more native.
107
            self.logger.info("Processing builds in '%s'" % target_state.title)
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
108
            target_builds = build_provider.getBuildRecords(
109
                build_state=target_state, pocket=pocket)
110
111
            for build in target_builds:
112
                # Skip builds for superseded sources; they won't ever
113
                # actually build.
114
                if not build.current_source_publication:
13970.7.8 by William Grant
And make it a bit more native.
115
                    self.logger.debug(
116
                        'Skipping superseded %s (%s)'
117
                        % (build.title, build.id))
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
118
                    continue
119
120
                if not build.can_be_retried:
13970.7.8 by William Grant
And make it a bit more native.
121
                    self.logger.warn(
122
                        'Can not retry %s (%s)' % (build.title, build.id))
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
123
                    continue
124
13970.7.8 by William Grant
And make it a bit more native.
125
                self.logger.info('Retrying %s (%s)' % (build.title, build.id))
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
126
                build.retry()
127
13970.7.8 by William Grant
And make it a bit more native.
128
        self.logger.info("Success.")
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
129
13970.7.8 by William Grant
And make it a bit more native.
130
        if self.options.dryrun:
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
131
            transaction.abort()
13970.7.8 by William Grant
And make it a bit more native.
132
            self.logger.info('Dry-run.')
3147.5.29 by Celso Providelo
add experimental script for mass-retry builds
133
        else:
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
134
            transaction.commit()
13970.7.8 by William Grant
And make it a bit more native.
135
            self.logger.info("Committed")
3147.5.29 by Celso Providelo
add experimental script for mass-retry builds
136
137
138
if __name__ == '__main__':
13970.7.7 by William Grant
Quick port of buildd-mass-retry to LaunchpadScript.
139
    BuilddMassRetryScript('buildd-mass-retry', 'fiera').lock_and_run()