~wgrant/ubuntu-archive-tools/ddebs-2

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
#!/usr/bin/python

# Copyright (C) 2008, 2009, 2010, 2011, 2012  Canonical Ltd.

# 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/>.

"""Adjust SRU bugs after accepting the corresponding update."""

from __future__ import print_function

from optparse import OptionParser
import re
import sys

import launchpadlib.errors
from launchpadlib.launchpad import Launchpad


CONSUMER_KEY = "sru-accept"

bug_target_re = re.compile(
    r'/ubuntu/(?:(?P<suite>[^/]+)/)?\+source/(?P<source>[^/]+)$')


def get_main_archive(launchpad):
    distros = launchpad.distributions
    ubuntu = distros['ubuntu']
    return ubuntu.main_archive


def join_english_list(words):
    num_words = len(words)
    assert num_words > 0
    if num_words > 2:
        # Yes, I use the Oxford comma.  Deal with it.
        return '%s, and %s' % (', '.join(words[:-1]), words[-1])
    elif num_words == 2:
        return '%s and %s' % (words[0], words[1])
    else:
        return words[0]


def process_bug(launchpad, options, num):
    bug = launchpad.bugs[num]

    for task in bug.bug_tasks:
        # Ugly; we have to do URL-parsing to figure this out.
        # /ubuntu/+source/foo can be fed to launchpad.load() to get a
        # distribution_source_package, but /ubuntu/hardy/+source/foo can't.
        match = bug_target_re.search(task.target.self_link)
        if (not match or
            (options.package and
             match.group('source') != options.package)):
            print("Ignoring task %s in bug %s" % (task.web_link, num))
            continue
        if (match.group('suite') in options.targets and
            task.status not in ("Invalid", "Won't Fix", "Fix Committed",
                                "Fix Released")):
            task.status = "Fix Committed"
            task.lp_save()
            print("Success: task %s in bug %s" % (task.web_link, num))

    bug.subscribe(person=launchpad.people['ubuntu-sru'])
    bug.subscribe(person=launchpad.people['sru-verification'])

    if not options.package or 'linux' not in options.package:
        for t in ('verification-failed', 'verification-done'):
            if t in bug.tags:
                # this dance is needed due to
                # https://bugs.launchpad.net/launchpadlib/+bug/254901
                tags = bug.tags
                tags.remove(t)
                bug.tags = tags
        bug.lp_save()

        if 'verification-needed' not in bug.tags:
            # this dance is needed due to
            # https://bugs.launchpad.net/launchpadlib/+bug/254901
            tags = bug.tags
            tags.append('verification-needed')
            bug.tags = tags
            bug.lp_save()

    text = ('Hello %s, or anyone else affected,\n\n' %
            re.split(r'[,\s]', bug.owner.display_name)[0])

    if options.package:
        text += 'Accepted %s into ' % options.package
    else:
        text += 'Accepted into '
    if options.package and options.version:
        text += ('%s. The package will build now and be available at '
                 'http://launchpad.net/ubuntu/+source/%s/%s in a few hours, '
                 'and then in the -proposed repository.\n\n' % (
                     join_english_list([
                         '%s-proposed' % target
                         for target in options.targets]),
                     options.package, options.version))
    else:
        text += ('%s. The package will build now and be available in a few '
                 'hours in the -proposed repository.\n\n' % (
                     join_english_list([
                         '%s-proposed' % target
                         for target in options.targets])))

    text += ('Please help us by testing this new package.  See '
             'https://wiki.ubuntu.com/Testing/EnableProposed for '
             'documentation how to enable and use -proposed.  Your feedback '
             'will aid us getting this update out to other Ubuntu users.\n\n'
             'If this package fixes the bug for you, please add a comment to '
             'this bug, mentioning the version of the package you tested, and '
             'change the tag from verification-needed to verification-done. '
             'If it does not fix the bug for you, please add a comment '
             'stating that, and change the tag to verification-failed.  In '
             'either case, details of your testing will help us make a better '
             'decision.\n\n'
             'Further information regarding the verification process can be '
             'found at '
             'https://wiki.ubuntu.com/QATeam/PerformingSRUVerification .  '
             'Thank you in advance!')
    bug.newMessage(content=text, subject='Please test proposed package')


def append_series(option, opt_str, value, parser):
    if value.endswith('-proposed'):
        value = value[:-9]
    parser.values.ensure_value(option.dest, []).append(value)


if __name__ == '__main__':
    parser = OptionParser(
        usage="Usage: %prog [options] -v version [options] bug [bug ...]")

    parser.add_option("-l", "--launchpad", dest="launchpad_instance",
                      default="production")
    parser.add_option('-s', action='callback', callback=append_series,
                      type='string', dest='targets',
                      help='accept for SUITE(-proposed) instead of current '
                           'stable release',
                      metavar='SUITE')
    parser.add_option('-p', dest='package',
                      help='only change tasks for a particular source package',
                      default=None,
                      metavar='SRCPACKAGE')
    parser.add_option('-v', dest='version',
                      help='the version of the package being accepted',
                      default=None,
                      metavar='VERSION')

    options, args = parser.parse_args()

    if not options.version:
        print('A package version (-v) was not provided.')
        sys.exit(1)

    launchpad = Launchpad.login_with(CONSUMER_KEY, options.launchpad_instance)
    if not options.targets:
        options.targets = [[
            series.name for series in launchpad.distributions["ubuntu"].series
            if series.status == "Current Stable Release"][0]]
    try:
        for num in args:
            process_bug(launchpad, options, num)
    except launchpadlib.errors.HTTPError as err:
        print("There was an error:")
        print(err.content)