~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/ftpmaster-tools/buildd-mass-retry.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-09-19 04:01:37 UTC
  • mfrom: (13970.7.23 port-to-LaunchpadScript)
  • Revision ID: launchpad@pqm.canonical.com-20110919040137-sr8154o9tfptnqir
[r=sinzui][no-qa] Port a dozen scripts to LaunchpadScript,
        removing their direct initZopeless dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
import _pythonpath
15
15
 
16
 
from optparse import OptionParser
17
 
import sys
18
 
 
 
16
import transaction
19
17
from zope.component import getUtility
20
18
 
21
 
# Still needed fake import to stop circular imports.
22
 
import canonical.launchpad.interfaces
23
 
 
24
 
from canonical.database.sqlbase import ISOLATION_LEVEL_READ_COMMITTED
25
19
from lp.app.errors import NotFoundError
26
 
from canonical.launchpad.scripts import (
27
 
    execute_zcml_for_scripts, logger_options, logger)
28
 
from canonical.lp import initZopeless
29
20
from lp.buildmaster.enums import BuildStatus
30
21
from lp.registry.interfaces.distribution import IDistributionSet
31
22
from lp.registry.interfaces.pocket import PackagePublishingPocket
32
 
 
33
 
 
34
 
def main():
35
 
    parser = OptionParser()
36
 
    logger_options(parser)
37
 
 
38
 
    parser.add_option("-d", "--distribution",
39
 
                      dest="distribution", metavar="DISTRIBUTION",
40
 
                      default="ubuntu", help="distribution name")
41
 
 
42
 
    parser.add_option("-s", "--suite",
43
 
                      dest="suite", metavar="SUITE", default=None,
44
 
                      help="suite name")
45
 
 
46
 
    parser.add_option("-a", "--architecture",
47
 
                      dest="architecture", metavar="ARCH", default=None,
48
 
                      help="architecture tag")
49
 
 
50
 
    parser.add_option("-N", "--dry-run", action="store_true",
51
 
                      dest="dryrun", metavar="DRY_RUN", default=False,
52
 
                      help="Whether to treat this as a dry-run or not.")
53
 
 
54
 
    parser.add_option("-F", "--failed", action="store_true",
55
 
                      dest="failed", default=False,
56
 
                      help="Reset builds in FAILED state.")
57
 
 
58
 
    parser.add_option("-D", "--dep-wait", action="store_true",
59
 
                      dest="depwait", default=False,
60
 
                      help="Reset builds in DEPWAIT state.")
61
 
 
62
 
    parser.add_option("-C", "--chroot-wait", action="store_true",
63
 
                      dest="chrootwait", default=False,
64
 
                      help="Reset builds in CHROOTWAIT state.")
65
 
 
66
 
    (options, args) = parser.parse_args()
67
 
 
68
 
    log = logger(options, "build-mass-retry")
69
 
 
70
 
    log.debug("Intitialising connection.")
71
 
    execute_zcml_for_scripts()
72
 
    ztm = initZopeless(dbuser="fiera",
73
 
                       isolation=ISOLATION_LEVEL_READ_COMMITTED)
74
 
 
75
 
    try:
76
 
        distribution = getUtility(IDistributionSet)[options.distribution]
77
 
    except NotFoundError, info:
78
 
        log.error("Distribution not found: %s" % info)
79
 
        return 1
80
 
 
81
 
    try:
82
 
        if options.suite is not None:
83
 
            series, pocket = distribution.getDistroSeriesAndPocket(
84
 
                options.suite)
 
23
from lp.services.scripts.base import LaunchpadScript, LaunchpadScriptFailure
 
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:
 
62
            distribution = getUtility(IDistributionSet)[
 
63
                self.options.distribution]
 
64
        except NotFoundError, info:
 
65
            raise LaunchpadScriptFailure("Distribution not found: %s" % info)
 
66
 
 
67
        try:
 
68
            if self.options.suite is not None:
 
69
                series, pocket = distribution.getDistroSeriesAndPocket(
 
70
                    self.options.suite)
 
71
            else:
 
72
                series = distribution.currentseries
 
73
                pocket = PackagePublishingPocket.RELEASE
 
74
        except NotFoundError, info:
 
75
            raise LaunchpadScriptFailure("Suite not found: %s" % info)
 
76
 
 
77
        # store distroseries as the current IHasBuildRecord provider
 
78
        build_provider = series
 
79
 
 
80
        if self.options.architecture:
 
81
            try:
 
82
                dar = series[self.options.architecture]
 
83
            except NotFoundError, info:
 
84
                raise LaunchpadScriptFailure(info)
 
85
 
 
86
            # store distroarchseries as the current IHasBuildRecord provider
 
87
            build_provider = dar
 
88
 
 
89
        self.logger.info(
 
90
            "Initializing Build Mass-Retry for '%s/%s'"
 
91
            % (build_provider.title, pocket.name))
 
92
 
 
93
        requested_states_map = {
 
94
            BuildStatus.FAILEDTOBUILD: self.options.failed,
 
95
            BuildStatus.MANUALDEPWAIT: self.options.depwait,
 
96
            BuildStatus.CHROOTWAIT: self.options.chrootwait,
 
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
 
 
107
            self.logger.info("Processing builds in '%s'" % target_state.title)
 
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:
 
115
                    self.logger.debug(
 
116
                        'Skipping superseded %s (%s)'
 
117
                        % (build.title, build.id))
 
118
                    continue
 
119
 
 
120
                if not build.can_be_retried:
 
121
                    self.logger.warn(
 
122
                        'Can not retry %s (%s)' % (build.title, build.id))
 
123
                    continue
 
124
 
 
125
                self.logger.info('Retrying %s (%s)' % (build.title, build.id))
 
126
                build.retry()
 
127
 
 
128
        self.logger.info("Success.")
 
129
 
 
130
        if self.options.dryrun:
 
131
            transaction.abort()
 
132
            self.logger.info('Dry-run.')
85
133
        else:
86
 
            series = distribution.currentseries
87
 
            pocket = PackagePublishingPocket.RELEASE
88
 
    except NotFoundError, info:
89
 
        log.error("Suite not found: %s" % info)
90
 
        return 1
91
 
 
92
 
    # store distroseries as the current IHasBuildRecord provider
93
 
    build_provider = series
94
 
 
95
 
    if options.architecture:
96
 
        try:
97
 
            dar = series[options.architecture]
98
 
        except NotFoundError, info:
99
 
            log.error(info)
100
 
            return 1
101
 
 
102
 
        # store distroarchseries as the current IHasBuildRecord provider
103
 
        build_provider = dar
104
 
 
105
 
    log.info("Initializing Build Mass-Retry for '%s/%s'"
106
 
              % (build_provider.title, pocket.name))
107
 
 
108
 
    requested_states_map = {
109
 
        BuildStatus.FAILEDTOBUILD : options.failed,
110
 
        BuildStatus.MANUALDEPWAIT : options.depwait,
111
 
        BuildStatus.CHROOTWAIT : options.chrootwait,
112
 
        }
113
 
 
114
 
    # XXX cprov 2006-08-31: one query per requested state
115
 
    # could organise it in a single one nicely if I have
116
 
    # an empty SQLResult instance, than only iteration + union()
117
 
    # would work.
118
 
    for target_state, requested in requested_states_map.items():
119
 
        if not requested:
120
 
            continue
121
 
 
122
 
        log.info("Processing builds in '%s'" % target_state.title)
123
 
        target_builds = build_provider.getBuildRecords(
124
 
            build_state=target_state, pocket=pocket)
125
 
 
126
 
        for build in target_builds:
127
 
            # Skip builds for superseded sources; they won't ever
128
 
            # actually build.
129
 
            if not build.current_source_publication:
130
 
                log.debug(
131
 
                    'Skipping superseded %s (%s)' % (build.title, build.id))
132
 
                continue
133
 
 
134
 
            if not build.can_be_retried:
135
 
                log.warn('Can not retry %s (%s)' % (build.title, build.id))
136
 
                continue
137
 
 
138
 
            log.info('Retrying %s (%s)' % (build.title, build.id))
139
 
            build.retry()
140
 
 
141
 
    log.info("Success.")
142
 
 
143
 
    if options.dryrun:
144
 
        ztm.abort()
145
 
        log.info('Dry-run.')
146
 
    else:
147
 
        ztm.commit()
148
 
        log.info("Committed")
149
 
 
150
 
    return 0
 
134
            transaction.commit()
 
135
            self.logger.info("Committed")
151
136
 
152
137
 
153
138
if __name__ == '__main__':
154
 
    sys.exit(main())
 
139
    BuilddMassRetryScript('buildd-mass-retry', 'fiera').lock_and_run()