This file is indexed.

/usr/bin/requestbackport is in ubuntu-dev-tools 0.155.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/python
#
# Copyright (C) 2011, Stefano Rivera <stefanor@ubuntu.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

from collections import defaultdict
import optparse
import re
import sys

import apt
from distro_info import UbuntuDistroInfo

from ubuntutools.config import UDTConfig
from ubuntutools.lp.lpapicache import Launchpad, Distribution
from ubuntutools.lp.udtexceptions import PackageNotFoundException
from ubuntutools.logger import Logger
from ubuntutools.question import (YesNoQuestion, EditBugReport,
                                  confirmation_prompt)
from ubuntutools.rdepends import query_rdepends, RDependsException


class DestinationException(Exception):
    pass


def determine_destinations(source, destination):
    ubuntu_info = UbuntuDistroInfo()
    if destination is None:
        destination = ubuntu_info.stable()

    if source not in ubuntu_info.all:
        raise DestinationException("Source release %s does not exist" % source)
    if destination not in ubuntu_info.all:
        raise DestinationException("Destination release %s does not exist"
                                   % destination)
    if destination not in ubuntu_info.supported():
        raise DestinationException("Destination release %s is not supported"
                                   % destination)

    found = False
    destinations = []
    support_gap = False
    for release in ubuntu_info.all:
        if release == destination:
            found = True
        if release == source:
            break
        if found:
            if support_gap:
                if ubuntu_info.is_lts(release):
                    support_gap = False
                else:
                    continue
            if release not in ubuntu_info.supported():
                support_gap = True
                continue
            destinations.append(release)

    assert found
    assert len(destinations) > 0

    return destinations


def disclaimer():
    print ("Ubuntu's backports are not for fixing bugs in stable releases, "
           "but for bringing new features to older, stable releases.")
    print ("See https://wiki.ubuntu.com/UbuntuBackports for the Ubuntu "
           "Backports policy and processes.")
    print ("See https://wiki.ubuntu.com/StableReleaseUpdates for the process "
           "for fixing bugs in stable releases.")
    confirmation_prompt()


def check_existing(package, destinations):
    """Search for possible existing bug reports"""
    # The LP bug search is indexed, not substring:
    query = re.findall(r'[a-z]+', package)
    bugs = []
    for release in destinations:
        project = Launchpad.projects[release + '-backports']
        bugs += project.searchTasks(omit_duplicates=True,
                                    search_text=query,
                                    status=["Incomplete", "New", "Confirmed",
                                            "Triaged", "In Progress",
                                            "Fix Committed"])
    if not bugs:
        return

    Logger.normal("There are existing bug reports that look similar to your "
                  "request. Please check before continuing:")

    for bug in sorted(set(bug_task.bug for bug_task in bugs)):
        Logger.normal(" * LP: #%-7i: %s  %s", bug.id, bug.title, bug.web_link)

    confirmation_prompt()


def find_rdepends(releases, published_binaries):
    intermediate = defaultdict(lambda: defaultdict(list))

    # We want to display every pubilshed binary, even if it has no rdepends
    for binpkg in published_binaries:
        intermediate[binpkg]

    for arch in ('any', 'source'):
        for release in releases:
            for binpkg in published_binaries:
                try:
                    raw_rdeps = query_rdepends(binpkg, release, arch)
                except RDependsException:
                    # Not published? TODO: Check
                    continue
                for relationship, rdeps in raw_rdeps.iteritems():
                    for rdep in rdeps:
                        # Ignore circular deps:
                        if rdep['Package'] in published_binaries:
                            continue
                        # arch==any queries return Reverse-Build-Deps:
                        if (arch == 'any' and rdep.get('Architectures', [])
                                              == ['source']):
                            continue
                        intermediate[binpkg][rdep['Package']] \
                                .append((release, relationship))

    output = []
    for binpkg, rdeps in intermediate.iteritems():
        output += ['', binpkg, '-' * len(binpkg)]
        for pkg, appearences in rdeps.iteritems():
            output += ['* %s' % pkg]
            for release, relationship in appearences:
                output += ['  [ ] %s (%s)' % (release, relationship)]

    found_any = sum(len(rdeps) for rdeps in intermediate.itervalues())
    if found_any:
        output = [
            "Reverse dependencies:",
            "=====================",
            "The following reverse-dependencies need to be tested against the "
            "new version of %(package)s. "
            "For reverse-build-dependencies (-Indep), please test that the "
            "package still builds against the new %(package)s. "
            "For reverse-dependencies, please test that the version of the "
            "package currently in the release still works with the new "
            "%(package)s installed. "
            "Reverse- Recommends, Suggests, and Enhances don't need to be "
            "tested, and are listed for completeness-sake."
           ] + output
    else:
        output = ["No reverse dependencies"]

    return output


def locate_package(package, distribution):
    archive = Distribution('ubuntu').getArchive()
    for pass_ in ('source', 'binary'):
        try:
            package_spph = archive.getSourcePackage(package, distribution)
            return package_spph
        except PackageNotFoundException, e:
            if pass_ == 'binary':
                Logger.error(str(e))
                sys.exit(1)

        try:
            apt_pkg = apt.Cache()[package]
        except KeyError:
            continue
        package = apt_pkg.candidate.source_name
        Logger.normal("Binary package specified, considering its source "
                      "package instead: %s", package)


def request_backport(package_spph, source, destinations):

    published_binaries = set()
    for bpph in package_spph.getBinaries():
        published_binaries.add(bpph.getPackageName())

    if not published_binaries:
        Logger.error("%s (%s) has no published binaries in %s. ",
                     package_spph.getPackageName(), package_spph.getVersion(),
                     source)
        Logger.normal("Is it stuck in bin-NEW? It can't be backported until "
                      "the binaries have been accepted.")
        sys.exit(1)

    testing = []
    testing += ["You can test-build the backport in your PPA with "
                "backportpackage:"]
    testing += ["$ backportpackage -u ppa:<lp username>/<ppa name> "
                "-s %s -d %s %s"
                % (source, dest, package_spph.getPackageName())
                for dest in destinations]
    testing += [""]
    for dest in destinations:
        testing += ['* %s:' % dest]
        testing += ["[ ] Package builds without modification"]
        testing += ["[ ] %s installs cleanly and runs" % binary
                    for binary in published_binaries]

    subst = {
        'package': package_spph.getPackageName(),
        'version': package_spph.getVersion(),
        'component': package_spph.getComponent(),
        'source': package_spph.getSeriesAndPocket(),
        'destinations': ', '.join(destinations),
    }
    subject = ("Please backport %(package)s %(version)s (%(component)s) "
               "from %(source)s" % subst)
    body = ('\n'.join(
            [
             "Please backport %(package)s %(version)s (%(component)s) "
             "from %(source)s to %(destinations)s.",
             "",
             "Reason for the backport:",
             "========================",
             ">>> Enter your reasoning here <<<",
             "",
             "Testing:",
             "========",
             "Mark off items in the checklist [X] as you test them, "
             "but please leave the checklist so that backporters can quickly "
             "evaluate the state of testing.",
             ""
            ]
            + testing
            + [""]
            + find_rdepends(destinations, published_binaries)
            + [""]
        ) % subst)

    editor = EditBugReport(subject, body)
    editor.edit()
    subject, body = editor.get_report()

    Logger.normal('The final report is:\nSummary: %s\nDescription:\n%s\n',
                  subject, body)
    if YesNoQuestion().ask("Request this backport", "yes") == "no":
        sys.exit(1)

    targets = [Launchpad.projects['%s-backports' % destination]
               for destination in destinations]
    bug = Launchpad.bugs.createBug(title=subject, description=body,
                                   target=targets[0])
    for target in targets[1:]:
        bug.addTask(target=target)

    Logger.normal("Backport request filed as %s", bug.web_link)


def main():
    parser = optparse.OptionParser('%prog [options] package')
    parser.add_option('-d', '--destination', metavar='DEST',
                      help='Backport to DEST release and necessary '
                           'intermediate releases '
                           '(default: current stable release)')
    parser.add_option('-s', '--source', metavar='SOURCE',
                      help='Backport from SOURCE release '
                           '(default: current devel release)')
    parser.add_option('-l', '--lpinstance', metavar='INSTANCE', default=None,
                      help='Launchpad instance to connect to '
                           '(default: production).')
    parser.add_option('--no-conf', action='store_true',
                      dest='no_conf', default=False,
                      help="Don't read config files or environment variables")
    options, args = parser.parse_args()

    if len(args) != 1:
        parser.error("One (and only one) package must be specified")
    package = args[0]

    config = UDTConfig(options.no_conf)

    if options.lpinstance is None:
        options.lpinstance = config.get_value('LPINSTANCE')
    Launchpad.login(options.lpinstance)

    if options.source is None:
        options.source = Distribution('ubuntu').getDevelopmentSeries().name

    try:
        destinations = determine_destinations(options.source,
                                              options.destination)
    except DestinationException, e:
        Logger.error(str(e))
        sys.exit(1)

    disclaimer()

    check_existing(package, destinations)

    package_spph = locate_package(package, options.source)
    request_backport(package_spph, options.source, destinations)


if __name__ == '__main__':
    main()