~brian-murray/ubuntu-archive-tools/workaround-503

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

# Copyright (C) 2016  Canonical Ltd.
# Author: 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/>.

"""Show and approve changes in an unapproved kernel upload.

Generate a debdiff between current source package in a given release and the
version in the canonical-kernel ppa, and ask whether or not to approve the
upload.

The debdiff is filtered for noise (abi/* directories; mechanical changes of
ABI strings in debian/control et al.)

USAGE:
    kernel-sru-review [-s precise] linux
"""

import datetime
import os
import pytz
import re
import shutil
import subprocess
import sys
import time
from contextlib import ExitStack
from tempfile import mkdtemp
from optparse import OptionParser

from launchpadlib.launchpad import Launchpad
from kernel_workflow import *


def review_task_callback(lp, bugnum, task, context):
    if str(task.target) != \
       ('%skernel-sru-workflow/promote-to-proposed' % str(lp._root_uri)):
        return {}
    if task.status == 'Confirmed':
        return {'proposed': task}
    raise KernelWorkflowError(
              "Ignoring bug %s, not ready to promote-to-proposed"
              % bugnum)


def review_source_callback(lp, bugnum, tasks, full_packages, release, context):
    for source in full_packages:
        process_source_package(
            source, release, context['archive'], context['ppa'],
            context['ubuntu'])
    tasks['proposed'].status = 'Fix Committed'
    tasks['proposed'].assignee = lp.me
    tasks['proposed'].lp_save()


def process_source_package(source, release, archive, ppa, ubuntu):
    series = ubuntu.getSeries(name_or_version=release)

    ppa_src = ppa.getPublishedSources(order_by_date=True,
                                      status='Published', exact_match=True,
                                      distro_series=series,
                                      source_name=source)[0]
    ppa_ver = ppa_src.source_package_version
    ppa_dsc = list(filter(
                  lambda x: x.endswith('.dsc'), ppa_src.sourceFileUrls()))[0]

    archive_uploads = series.getPackageUploads(version=ppa_ver,
                                               name=source,
                                               archive=archive,
                                               exact_match=True)
    for upload in archive_uploads:
        if upload.status != 'Rejected':
            print("%s_%s already copied to Ubuntu archive (%s), skipping" %
                  (source, ppa_ver, upload.status))
            return

    srcpkg = archive.getPublishedSources(order_by_date=True,
                                         status='Published', exact_match=True,
                                         distro_series=series,
                                         source_name=source)[0]
    source_ver = srcpkg.source_package_version
    source_dsc = list(filter(
                         lambda x: x.endswith('.dsc'),
                         srcpkg.sourceFileUrls()))[0]

    new_fullabi = ppa_ver.split('~')[0]
    new_majorabi = re.sub(r"\.[^.]+$", '', new_fullabi)
    new_upstream = new_fullabi.split('-')[0]

    old_fullabi = source_ver.split('~')[0]
    old_majorabi = re.sub(r"\.[^.]+$", '', old_fullabi)
    old_upstream = old_fullabi.split('-')[0]

    # grab the old source first
    pull_cmd = ['pull-lp-source', source, source_ver]
    try:
        subprocess.check_call(pull_cmd)
    except subprocess.CalledProcessError as e:
        print("Failed to get archive source for %s at version %s" %
              (source, source_ver))
        raise e

    # update contents to match what we think the new ABI should be
    sed_cmd = ('grep -rl "{}" "{}-{}"/debian* | grep -v changelog '
               + '| xargs -r sed -i -e"s/{}/{}/g" -e"s/{}/{}/g"').format(
                  re.escape(old_majorabi), source, old_upstream,
                  re.escape(old_fullabi), re.escape(new_fullabi),
                  re.escape(old_majorabi), re.escape(new_majorabi))
    try:
        subprocess.check_call(sed_cmd, shell=True)
    except subprocess.CalledProcessError as e:
        print("Failed to postprocess archive source for %s at version %s" %
              (source, source_ver))
        raise e

    # move the source dir aside so that it doesn't clobber.
    os.rename(source + '-' + old_upstream, source + '-' + old_upstream + '.old')

    # grab the new source
    dget_cmd = ['dget', '-u', ppa_dsc]
    try:
        subprocess.check_call(dget_cmd)
    except subprocess.CalledProcessError as e:
        print("Failed to get ppa source for %s at version %s" %
              (source, ppa_ver))
        raise e

    # look at the diff
    diff_cmd = ('diff -uNr "{}-{}.old" "{}-{}" | filterdiff -x \'**/abi/**\''
                + ' | sensible-pager').format(
                   source, old_upstream, source, new_upstream)
    subprocess.call(diff_cmd, shell=True)

    print("Accept the package into -proposed? [yN] ", end="")
    sys.stdout.flush()
    response = sys.stdin.readline()
    if response.strip().lower().startswith('y'):
        copy_cmd = ['copy-proposed-kernel', release, source]
        copy_time = datetime.datetime.now(tz=pytz.utc)
        try:
            subprocess.check_call(copy_cmd)
        except subprocess.CalledProcessError as e:
            print("Failed to copy source for %s at version %s" %
                  (source, ppa_ver))
            raise e
        print("Accepted")
        # we know this isn't a kernel package containing signed bits,
        # so don't subject ourselves to extra delays
        if '-meta' in source or '-signed' in source:
            return
        print("Checking for UEFI binaries")
        # Arbitrary 10 second delay, maybe enough to let uefi binaries hit
        # the unapproved queue.
        time.sleep(10)
        # accept any related uefi binaries.  We filter as closely as possible
        # on name without hard-coding architecture, and we also filter to
        # only include uefi binaries that have appeared since we started the
        # copy to avoid accepting something that might have been improperly
        # copied into the queue by an "attacker" with upload rights.
        uefis = series.getPackageUploads(archive=archive,
                                         pocket='Proposed',
                                         status='Unapproved',
                                         custom_type='uefi',
                                         name='{}_{}_'.format(source,ppa_ver),
                                         created_since_date=copy_time)
        for uefi in uefis:
            print("Accepting {}".format(uefi))
            uefi.acceptFromQueue()

if __name__ == '__main__':

    default_release = 'yakkety'

    parser = OptionParser(
        usage="Usage: %prog [options] bug [bug ...]")

    parser.add_option(
        "-l", "--launchpad", dest="launchpad_instance", default="production")
    parser.add_option(
        "-k", "--keep-files", dest="keep_files", action="store_true")

    opts, bugs = parser.parse_args()

    if len(bugs) < 1:
        parser.error('Need to specify at least one bug number')

    launchpad = Launchpad.login_with(
        'ubuntu-archive-tools', opts.launchpad_instance, version='devel')

    ubuntu = launchpad.distributions['ubuntu']
    archive = ubuntu.main_archive
    ppa = launchpad.people['canonical-kernel-team'].getPPAByName(
                distribution=ubuntu, name='ppa')

    context = {'archive': archive, 'ppa': ppa, 'ubuntu': ubuntu}
    main_dir = os.getcwd()
    for bugnum in bugs:
        with ExitStack() as resources:
            cwd = mkdtemp(prefix='kernel-sru-{}-'.format(bugnum), dir=main_dir)
            if not opts.keep_files:
                resources.callback(shutil.rmtree, cwd)
            os.chdir(cwd)
            process_sru_bug(
                launchpad, bugnum, review_task_callback,
                review_source_callback, context)
            os.chdir(main_dir)