~laney/ubuntu-archive-tools/cm-show-fix-released

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
#!/usr/bin/python
# Manage the Launchpad build farm.
#
# Copyright 2012-2014 Canonical Ltd.
# Author: William Grant <wgrant@ubuntu.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/>.

import datetime
from optparse import OptionParser
import re
from textwrap import dedent

from launchpadlib.launchpad import Launchpad
import pytz


def format_timedelta(delta):
    value = None
    hours = delta.seconds // 3600
    minutes = (delta.seconds - (hours * 3600)) // 60
    if delta.days > 0:
        value = delta.days
        unit = 'day'
    elif hours > 0:
        value = hours
        unit = 'hour'
    elif minutes > 0:
        value = minutes
        unit = 'minute'
    if value is not None:
        return 'for %d %s%s' % (value, unit, 's' if value > 1 else '')
    return ''


parser = OptionParser(description=dedent("""\
    List and manage Launchpad builders.

    If no changes are specified (--auto, --manual, --enable, --disable, or
    --set-failnotes), a detailed listing of matching builders will be shown.
    """))
parser.add_option(
    "-l", "--lp-instance", dest="lp_instance", default="production",
    help="use the specified Launchpad instance (default: production)")

parser.add_option(
    "-v", "--verbose", dest="verbose", action="store_true", default=None,
    help="display more detail")

parser.add_option(
    "-a", "--arch", dest="arch", default=None,
    help="update only builders of this architecture (eg. i386)")
parser.add_option(
    "-b", "--builder", dest="builders", action="append", metavar="BUILDER",
    help="update only this builder (may be given multiple times)")
parser.add_option(
    "--failnotes", dest="failnotes", default=None,
    help="update only builders with failnotes matching this regexp")
parser.add_option(
    "-e", "--enabled", action="store_const", dest="ok_filter", const=True,
    help="update only enabled builders")
parser.add_option(
    "-d", "--disabled", action="store_const", dest="ok_filter", const=False,
    help="update only disabled builders")
parser.add_option(
    "--virtual", action="store_const", dest="virtual_filter", const=True,
    help="update only virtual builders")
parser.add_option(
    "--non-virtual", action="store_const", dest="virtual_filter", const=False,
    help="update only non-virtual builders")

parser.add_option(
    "--auto", dest="auto", action="store_true", default=None,
    help="enable automatic dispatching")
parser.add_option(
    "--manual", dest="manual", action="store_true", default=None,
    help="disable automatic dispatching")
parser.add_option(
    "--enable", dest="enable", action="store_true", default=None,
    help="mark the builder as OK")
parser.add_option(
    "--disable", dest="disable", action="store_true", default=None,
    help="mark the builder as not OK")
parser.add_option(
    "--set-failnotes", dest="set_failnotes", default=None,
    help="set the builder's failnotes")

(options, args) = parser.parse_args()

if options.manual and options.auto:
    parser.error("--manual and --auto are mutually exclusive")
if options.enable and options.disable:
    parser.error("--enable and --disable are mutually exclusive")

changes = {}
if options.manual:
    changes['manual'] = True
if options.auto:
    changes['manual'] = False
if options.enable:
    changes['builderok'] = True
if options.disable:
    changes['builderok'] = False
if options.set_failnotes is not None:
    changes['failnotes'] = options.set_failnotes or None

lp = Launchpad.login_with(
    'manage-builders', options.lp_instance, version='devel')

candidates = []
for builder in lp.builders:
    if not builder.active:
        continue
    if (options.ok_filter is not None
            and builder.builderok != options.ok_filter):
        continue
    if (options.virtual_filter is not None
            and builder.virtualized != options.virtual_filter):
        continue
    if options.builders and builder.name not in options.builders:
        continue
    if (options.arch
        and not any(p.rsplit('/', 1)[1] == options.arch
                    for p in builder.processors)):
        continue
    if (options.failnotes and (
            not builder.failnotes
            or not re.search(options.failnotes, builder.failnotes))):
        continue
    candidates.append(builder)

count_changed = count_unchanged = 0

if changes:
    print 'Updating %d builders.' % len(candidates)

for candidate in candidates:
    changed = False
    for change, value in changes.iteritems():
        if getattr(candidate, change) != value:
            setattr(candidate, change, value)
            changed = True
    if changed:
        count_changed += 1
        candidate.lp_save()
        print '* %s' % candidate.name
    elif changes:
        print '  %s' % candidate.name
        count_unchanged += 1
    else:
        archs = ' '.join(p.rsplit('/', 1)[1] for p in candidate.processors)
        if not candidate.builderok:
            if candidate.failnotes:
                failnote = candidate.failnotes.strip().splitlines()[0]
            else:
                failnote = 'no failnotes'
            status = 'DISABLED: %s' % failnote
        elif not options.verbose or candidate.clean_status == 'Clean':
            status = ''
        else:
            time = format_timedelta(
                datetime.datetime.now(pytz.UTC) -
                candidate.date_clean_status_changed)
            status = '%s %s' % (candidate.clean_status, time)
        if options.verbose:
            print (
                '  %-20s %25s %3s %-24s %s' % (
                    candidate.name,
                    candidate.vm_host if candidate.vm_host else '',
                    '(v)' if candidate.virtualized else '', archs, status))
        else:
            print (
                '  %-20s %3s %-24s %s' % (
                    candidate.name,
                    '(v)' if candidate.virtualized else '', archs, status))

if changes:
    print "Changed: %d. Unchanged: %d." % (count_changed, count_unchanged)