~xnox/ubuntu-archive-tools/sru-report-autopkgtest-vomit

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/python3

# Parse removals.txt file
# Copyright (C) 2004, 2005, 2009, 2010, 2011, 2012  Canonical Ltd.
# Authors: James Troup <james.troup@canonical.com>,
#          Colin Watson <cjwatson@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; either version 2 of the License, or
# (at your option) any later version.

# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

############################################################################

# What kind of genius logs 3.5 years of removal data in
# semi-human-parseable text and nothing else?  Hmm.  That would be me. :(

# MAAAAAAAAAAAAAADNESSSSSSSSSSSSSSSSS

############################################################################

from __future__ import print_function

import copy
import optparse
import os
import sys
import time


try:
    from urllib.request import urlopen, urlretrieve
except ImportError:
    from urllib import urlretrieve
    from urllib2 import urlopen
import re
import logging
import gzip
import datetime
import subprocess

import apt_pkg
from launchpadlib.launchpad import Launchpad

import lputils


CONSUMER_KEY = "process-removals"


Debian = None


def parse_removals_file(options, removals_file):
    if options.report_ubuntu_delta and options.no_action:
        me = None
    else:
        me = options.launchpad.me.name

    state = "first separator"
    removal = {}
    for line in removals_file:
        line = line.strip().decode('UTF-8')
        # skip over spurious empty lines
        if not line and state in ("first separtor", "next separator", "date"):
            continue
        if line == ("=" * 73):
            if state == "done":
                state = "next separator"
            elif state in ("first separator", "next separator"):
                state = "date"
            else:
                raise RuntimeError("found separator but state is %s." % state)
        # NB: The 'Reason' is an abbreviation, check any referenced bugs for
        # more
        elif state == "next separator" and line.startswith("NB:"):
            state = "first separator"
        # [Date: Tue,  9 Jan 2001 20:46:15 -0500] [ftpmaster: James Troup]
        elif state in ("date", "next separator"):
            try:
                (date, ftpmaster, unused) = line.split("]")
                date = date.replace("[Date: ", "")
                state = "removed from"
            except:
                state = "broken date"
        # Sat, 06 May 2017 08:45:30 +0000] [ftpmaster: Chris Lamb]
        elif state == "broken date":
            (date, ftpmaster, unused2) = line.split("]")
            state = "removed from"
        # Removed the following packages from unstable:
        elif state == "removed from":
            # complete processing of the date from the preceding line
            removal["date"] = time.mktime(time.strptime(
                date, "%a, %d %b %Y %H:%M:%S %z"))
            removal["ftpmaster"] = ftpmaster.replace("] [ftpmaster: ", "")

            prefix = "Removed the following packages from "
            if not line.startswith(prefix):
                raise RuntimeError(
                    "state = %s, expected '%s', got '%s'" %
                    (state, prefix, line))
            line = line.replace(prefix, "")[:-1]
            line = line.replace(" and", "")
            line = line.replace(",", "")
            removal["suites"] = line.split()
            state = "before packages"
        elif state == "before packages" or state == "after packages":
            if line:
                raise RuntimeError(
                    "state = %s, expected '', got '%s'" % (state, line))
            if state == "before packages":
                state = "packages"
            elif state == "after packages":
                state = "reason prefix"
        # xroach |      4.0-8 | source, alpha, arm, hppa, i386, ia64, m68k, \
        #                       mips, mipsel, powerpc, s390, sparc
        # Closed bugs: 158188
        elif state == "packages":
            if line.find("|") != -1:
                package, version, architectures = [
                    word.strip() for word in line.split("|")]
                architectures = [
                    arch.strip() for arch in architectures.split(",")]
                removal.setdefault("packages", [])
                removal["packages"].append([package, version, architectures])
            elif line.startswith("Closed bugs: "):
                line = line.replace("Closed bugs: ", "")
                removal["closed bugs"] = line.split()
                state = "after packages"
            elif not line:
                state = "reason prefix"
            else:
                raise RuntimeError(
                    "state = %s, expected package list or 'Closed bugs:', "
                    "got '%s'" % (state, line))
        # ------------------- Reason -------------------
        elif state == "reason prefix":
            expected = "------------------- Reason -------------------"
            if not line == expected:
                raise RuntimeError(
                    "state = %s, expected '%s', got '%s'" %
                    (state, expected, line))
            state = "reason"
        # RoSRM; license problems.
        # ----------------------------------------------
        elif state == "reason":
            if line == "----------------------------------------------":
                state = "done"
                do_removal(me, options, removal)
                removal = {}
            else:
                removal.setdefault("reason", "")
                removal["reason"] += "%s\n" % line

        # nothing should go here


def show_reverse_depends(options, package):
    """Show reverse dependencies.

    This is mostly done by calling reverse-depends, but with some tweaks to
    make the output easier to read in context.
    """
    series_name = options.series.name
    commands = (
        ["reverse-depends", "-r", series_name, "src:%s" % package],
        ["reverse-depends", "-r", series_name, "-b", "src:%s" % package],
    )
    for command in commands:
        subp = subprocess.Popen(command, stdout=subprocess.PIPE)
        line = None
        for line in subp.communicate()[0].splitlines():
            line = line.decode('UTF-8').rstrip("\n")
            if line == "No reverse dependencies found":
                line = None
            else:
                print(line)
        if line:
            print()


non_meta_re = re.compile(r'^[a-zA-Z0-9+,./:=@_-]+$')


def shell_escape(arg):
    if non_meta_re.match(arg):
        return arg
    else:
        return "'%s'" % arg.replace("'", "'\\''")


seen_sources = set()


def do_removal(me, options, removal):
    if options.removed_from and options.removed_from not in removal["suites"]:
        return
    if options.date and int(options.date) > int(removal["date"]):
        return
    if options.architecture:
        for architecture in re.split(r'[, ]+', options.architecture):
            package_list = copy.copy(removal["packages"])
            for entry in package_list:
                (package, version, architectures) = entry
                if architecture not in architectures:
                    removal["packages"].remove(entry)
        if not removal["packages"]:
            return

    for entry in removal.get("packages", []):
        (package, version, architectures) = entry
        if options.source and options.source != package:
            continue
        if package in seen_sources:
            continue
        seen_sources.add(package)
        if package in Debian and not (options.force and options.source):
            logging.info("%s (%s): back in sid - skipping.", package, version)
            continue

        spphs = []
        for pocket in ("Release", "Proposed"):
            options.pocket = pocket
            if pocket == "Release":
                options.suite = options.series.name
            else:
                options.suite = "%s-proposed" % options.series.name
            try:
                spphs.append(
                    lputils.find_latest_published_source(options, package))
            except lputils.PackageMissing:
                pass

        if not spphs:
            logging.debug("%s (%s): not found", package, version)
            continue

        if options.report_ubuntu_delta:
            if 'ubuntu' in version:
                print("%s %s" % (package, version))
                continue
            if options.no_action:
                continue

        for spph in spphs:
            logging.info(
                "%s (%s/%s), removed from Debian on %s",
                package, version, spph.source_package_version,
                time.strftime("%Y-%m-%d", time.localtime(removal["date"])))

        removal["reason"] = removal["reason"].rstrip('.\n')
        try:
            bugs = ';' + ','.join(
                [" Debian bug #%s" % item for item in removal["closed bugs"]])
        except KeyError:
            bugs = ''
        reason = "(From Debian) " + removal["reason"] + bugs

        subprocess.call(['seeded-in-ubuntu', package])
        show_reverse_depends(options, package)
        for spph in spphs:
            if options.no_action:
                print("remove-package -s %s%s -e %s -m %s %s" % (
                    options.series.name,
                    "-proposed" if spph.pocket == "Proposed" else "",
                    spph.source_package_version, shell_escape(reason),
                    package))
            else:
                from ubuntutools.question import YesNoQuestion
                print("Removing packages:")
                print("\t%s" % spph.display_name)
                for binary in spph.getPublishedBinaries():
                    print("\t\t%s" % binary.display_name)
                print("Comment: %s" % reason)
                if YesNoQuestion().ask("Remove", "no") == "yes":
                    spph.requestDeletion(removal_comment=reason)


def fetch_removals_file(options):
    removals = "removals-full.txt"
    if options.date:
        thisyear = datetime.datetime.today().year
        if options.date >= time.mktime(time.strptime(str(thisyear), "%Y")):
            removals = "removals.txt"
    logging.debug("Fetching %s" % removals)
    return urlopen("http://ftp-master.debian.org/%s" % removals)


def read_sources():
    global Debian
    Debian = {}
    base = "http://ftp.debian.org/debian"

    logging.debug("Reading Debian sources")
    for component in "main", "contrib", "non-free":
        filename = "Debian_unstable_%s_Sources" % component
        if not os.path.exists(filename):
            url = "%s/dists/unstable/%s/source/Sources.gz" % (base, component)
            logging.info("Fetching %s" % url)
            fetched, headers = urlretrieve(url)
            out = open(filename, 'wb')
            gzip_handle = gzip.GzipFile(fetched)
            while True:
                block = gzip_handle.read(65536)
                if not block:
                    break
                out.write(block)
            out.close()
            gzip_handle.close()
        sources_filehandle = open(filename)
        Sources = apt_pkg.TagFile(sources_filehandle)
        while Sources.step():
            pkg = Sources.section.find("Package")
            version = Sources.section.find("Version")

            if (pkg in Debian and
                apt_pkg.version_compare(Debian[pkg]["version"],
                                        version) > 0):
                continue

            Debian[pkg] = {}
            Debian[pkg]["version"] = version
        sources_filehandle.close()


def parse_options():
    parser = optparse.OptionParser()

    parser.add_option("-l", "--launchpad", dest="launchpad_instance",
                      default="production")

    parser.add_option("-a", "--architecture", metavar="ARCH",
                      help="remove from ARCH")
    parser.add_option("-d", "--date", metavar="DATE",
                      help="only those removed since DATE (Unix epoch or %Y-%m-%d)")

    parser.add_option("-r", "--removed-from", metavar="SUITE",
                      help="only those removed from SUITE (aka distroseries)")

    parser.add_option("-s", "--source", metavar="NAME",
                      help="only source package NAME")
    parser.add_option("--force", action="store_true", default=False,
                      help="force removal even for packages back in unstable "
                           "(only with -s)")

    parser.add_option("-f", "--filename",
                      help="parse FILENAME")

    parser.add_option("-n", "--no-action", action="store_true",
                      help="don't remove packages; just print remove-package "
                           "commands")
    parser.add_option("-v", "--verbose", action="store_true",
                      help="emit verbose debugging messages")

    parser.add_option("--report-ubuntu-delta", action="store_true",
                      help="skip and report packages with Ubuntu deltas")

    options, args = parser.parse_args()
    if len(args):
        parser.error("no arguments expected")

    if options.date:
        if len(options.date) == 8:
            print("The format of date should be %Y-%m-%d.")
            sys.exit(1)
        try:
            int(options.date)
        except ValueError:
            options.date = time.mktime(time.strptime(options.date, "%Y-%m-%d"))
    else:
        options.date = time.mktime(time.strptime("2009-02-01", "%Y-%m-%d"))
    if not options.removed_from:
        options.removed_from = "unstable"
    if not options.architecture:
        options.architecture = "source"

    if options.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    return options, args


def main():
    apt_pkg.init()

    """Initialization, including parsing of options."""
    options, args = parse_options()

    if options.report_ubuntu_delta and options.no_action:
        options.launchpad = Launchpad.login_anonymously(
            CONSUMER_KEY, options.launchpad_instance)
    else:
        options.launchpad = Launchpad.login_with(
            CONSUMER_KEY, options.launchpad_instance)

    options.distribution = options.launchpad.distributions["ubuntu"]
    options.series = options.distribution.current_series
    options.archive = options.distribution.main_archive
    options.version = None

    read_sources()

    if options.filename:
        removals_file = open(options.filename, "rb")
    else:
        removals_file = fetch_removals_file(options)
    parse_removals_file(options, removals_file)
    removals_file.close()


if __name__ == '__main__':
    main()