~bkerensa/ubuntu-dev-tools/fix-for-1068049

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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (C) 2007 Canonical Ltd., Steve Kowalik
# Authors:
#  Martin Pitt <martin.pitt@ubuntu.com>
#  Steve Kowalik <stevenk@ubuntu.com>
#  Michael Bienia <geser@ubuntu.com>
#  Daniel Hahler <ubuntu@thequod.de>
#  Iain Lane <laney@ubuntu.com>
#  Jonathan Davies <jpds@ubuntu.com>
#  Markus Korn <thekorn@gmx.de> (python-launchpadlib support)
#
# ##################################################################
#
# 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 2.
#
# 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.
#
# See file /usr/share/common-licenses/GPL-2 for more details.
#
# ##################################################################

import optparse
import os
import sys

from debian.changelog import Version
from distro_info import UbuntuDistroInfo, DistroDataOutdated

from ubuntutools.config import UDTConfig, ubu_email
from ubuntutools.lp import udtexceptions
from ubuntutools.misc import require_utf8
from ubuntutools.question import confirmation_prompt, EditBugReport

#
# entry point
#

def main():
    ubu_info = UbuntuDistroInfo()
    DEFAULT_SOURCE = 'unstable'
    try:
        if ubu_info.is_lts(ubu_info.devel()):
            DEFAULT_SOURCE = 'testing'
    except DistroDataOutdated, e:
        print >> sys.stderr, str(e)

    # Our usage options.
    usage = ('Usage: %prog [options] '
             '<source package> [<target release> [base version]]')
    parser = optparse.OptionParser(usage)

    parser.add_option('-d', type='string',
                      dest='dist', default=DEFAULT_SOURCE,
                      help='Debian distribution to sync from.')
    parser.add_option('-k', type='string',
                      dest='keyid', default=None,
                      help='GnuPG key ID to use for signing report '
                           '(only used when emailing the sync request).')
    parser.add_option('-n', action='store_true',
                      dest='newpkg', default=False,
                      help='Whether package to sync is a new package in '
                           'Ubuntu.')
    parser.add_option('--email', action='store_true', default=False,
                      help='Use a PGP-signed email for filing the sync '
                           'request, rather than the LP API.')
    parser.add_option('--lp', dest='deprecated_lp_flag',
                      action='store_true', default=False,
                      help=optparse.SUPPRESS_HELP)
    parser.add_option('-l', '--lpinstance', metavar='INSTANCE',
                      dest='lpinstance', default=None,
                      help='Launchpad instance to connect to '
                           '(default: production).')
    parser.add_option('-s', action='store_true',
                      dest='sponsorship', default=False,
                      help='Force sponsorship')
    parser.add_option('-C', action='store_true',
                      dest='missing_changelog_ok', default=False,
                      help='Allow changelog to be manually filled in '
                           'when missing')
    parser.add_option('-e', action='store_true',
                      dest='ffe', default=False,
                      help='Use this after FeatureFreeze for non-bug fix '
                           'syncs, changes default subscription to the '
                           'appropriate release team.')
    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 not len(args):
        parser.print_help()
        sys.exit(1)

    require_utf8()

    config = UDTConfig(options.no_conf)

    if options.deprecated_lp_flag:
        print "The --lp flag is now default, ignored."
    if options.email:
        options.lpapi = False
    else:
        options.lpapi = config.get_value('USE_LPAPI', default=True,
                                         boolean=True)
    if options.lpinstance is None:
        options.lpinstance = config.get_value('LPINSTANCE')

    if options.keyid is None:
        options.keyid = config.get_value('KEYID')

    if not options.lpapi:
        if options.lpinstance == 'production':
            bug_mail_domain = 'bugs.launchpad.net'
        elif options.lpinstance == 'staging':
            bug_mail_domain = 'bugs.staging.launchpad.net'
        else:
            print >> sys.stderr, ('Error: Unknown launchpad instance: %s'
                                  % options.lpinstance)
            sys.exit(1)

    mailserver_host = config.get_value('SMTP_SERVER',
                                       default=None,
                                       compat_keys=['UBUSMTP', 'DEBSMTP'])
    if not options.lpapi and not mailserver_host:
        try:
            import DNS
            DNS.DiscoverNameServers()
            mxlist = DNS.mxlookup(bug_mail_domain)
            firstmx = mxlist[0]
            mailserver_host = firstmx[1]
        except ImportError:
            print >> sys.stderr, ('Please install python-dns to support '
                                  'Launchpad mail server lookup.')
            sys.exit(1)

    mailserver_port = config.get_value('SMTP_PORT', default=25,
                                       compat_keys=['UBUSMTP_PORT',
                                                    'DEBSMTP_PORT'])
    mailserver_user = config.get_value('SMTP_USER',
                                       compat_keys=['UBUSMTP_USER',
                                                    'DEBSMTP_USER'])
    mailserver_pass = config.get_value('SMTP_PASS',
                                       compat_keys=['UBUSMTP_PASS',
                                                    'DEBSMTP_PASS'])

    # import the needed requestsync module
    if options.lpapi:
        from ubuntutools.requestsync.lp import (check_existing_reports,
                                                get_debian_srcpkg,
                                                get_ubuntu_srcpkg,
                                                get_ubuntu_delta_changelog,
                                                need_sponsorship, post_bug)
        from ubuntutools.lp.lpapicache import Distribution, Launchpad
        # See if we have LP credentials and exit if we don't -
        # cannot continue in this case

        try:
            # devel for changelogUrl()
            Launchpad.login(service=options.lpinstance, api_version='devel')
        except IOError:
            sys.exit(1)
    else:
        from ubuntutools.requestsync.mail import (check_existing_reports,
                                                  get_debian_srcpkg,
                                                  get_ubuntu_srcpkg,
                                                  get_ubuntu_delta_changelog,
                                                  mail_bug, need_sponsorship)
        if not any(x in os.environ for x in ('UBUMAIL', 'DEBEMAIL', 'EMAIL')):
            print >> sys.stderr, (
                'E: The environment variable UBUMAIL, DEBEMAIL or EMAIL needs '
                'to be set to let this script mail the sync request.')
            sys.exit(1)

    newsource = options.newpkg
    sponsorship = options.sponsorship
    distro = options.dist
    ffe = options.ffe
    lpapi = options.lpapi
    need_interaction = False
    force_base_version = None
    srcpkg = args[0]

    if len(args) == 1:
        if lpapi:
            release = Distribution('ubuntu').getDevelopmentSeries().name
        else:
            release = ubu_info.devel()
        print >> sys.stderr, 'W: Target release missing - assuming %s' % release
    elif len(args) == 2:
        release = args[1]
    elif len(args) == 3:
        release = args[1]
        force_base_version = Version(args[2])
    else:
        print >> sys.stderr, 'E: Too many arguments.'
        parser.print_help()
        sys.exit(1)

    # Get the current Ubuntu source package
    try:
        ubuntu_srcpkg = get_ubuntu_srcpkg(srcpkg, release)
        ubuntu_version = Version(ubuntu_srcpkg.getVersion())
        ubuntu_component = ubuntu_srcpkg.getComponent()
        newsource = False  # override the -n flag
    except udtexceptions.PackageNotFoundException:
        ubuntu_srcpkg = None
        ubuntu_version = Version('~')
        ubuntu_component = None  # Set after getting the Debian info
        if not newsource:
            print ("'%s' doesn't exist in 'Ubuntu %s'.\n"
                   "Do you want to sync a new package?"
                   % (srcpkg, release))
            confirmation_prompt()
            newsource = True
    except udtexceptions.SeriesNotFoundException, error:
        print >> sys.stderr, "E: %s" % error
        sys.exit(1)

    # Get the requested Debian source package
    try:
        debian_srcpkg = get_debian_srcpkg(srcpkg, distro)
        debian_version = Version(debian_srcpkg.getVersion())
        debian_component = debian_srcpkg.getComponent()
    except udtexceptions.PackageNotFoundException, error:
        print >> sys.stderr, "E: %s" % error
        sys.exit(1)
    except udtexceptions.SeriesNotFoundException, error:
        print >> sys.stderr, "E: %s" % error
        sys.exit(1)

    if ubuntu_component is None:
        if debian_component == 'main':
            ubuntu_component = 'universe'
        else:
            ubuntu_component = 'multiverse'

    # Stop if Ubuntu has already the version from Debian or a newer version
    if (ubuntu_version >= debian_version) and options.lpapi:
        # try rmadison
        import ubuntutools.requestsync.mail
        try:
            debian_srcpkg = ubuntutools.requestsync.mail.get_debian_srcpkg(
                    srcpkg, distro)
            debian_version = Version(debian_srcpkg.getVersion())
            debian_component = debian_srcpkg.getComponent()
        except udtexceptions.PackageNotFoundException, error:
            print >> sys.stderr, "E: %s" % error
            sys.exit(1)

    if ubuntu_version == debian_version:
        print  >> sys.stderr, ('E: The versions in Debian and Ubuntu are the '
                               'same already (%s). Aborting.'
                               % ubuntu_version)
        sys.exit(1)
    if ubuntu_version > debian_version:
        print >> sys.stderr, ('E: The version in Ubuntu (%s) is newer than '
                              'the version in Debian (%s). Aborting.'
                              % (ubuntu_version, debian_version))
        sys.exit(1)

    # -s flag not specified - check if we do need sponsorship
    if not sponsorship:
        sponsorship = need_sponsorship(srcpkg, ubuntu_component, release)

    if not sponsorship and not ffe:
        print >> sys.stderr, ('Consider using syncpackage(1) for syncs that '
                              'do not require feature freeze exceptions.')

    # Check for existing package reports
    if not newsource:
        check_existing_reports(srcpkg)

    # Generate bug report
    pkg_to_sync = ('%s %s (%s) from Debian %s (%s)'
                   % (srcpkg, debian_version, ubuntu_component,
                      distro, debian_component))
    title = "Sync %s" % pkg_to_sync
    if ffe:
        title = "FFe: " + title
    report = "Please sync %s\n\n" % pkg_to_sync

    if 'ubuntu' in str(ubuntu_version):
        need_interaction = True

        print ('Changes have been made to the package in Ubuntu.\n'
               'Please edit the report and give an explanation.\n'
               'Not saving the report file will abort the request.')
        report += (u'Explanation of the Ubuntu delta and why it can be '
                   u'dropped:\n%s\n>>> ENTER_EXPLANATION_HERE <<<\n\n'
                   % get_ubuntu_delta_changelog(ubuntu_srcpkg))

    if ffe:
        need_interaction = True

        print ('To approve FeatureFreeze exception, you need to state\n'
               'the reason why you feel it is necessary.\n'
               'Not saving the report file will abort the request.')
        report += ('Explanation of FeatureFreeze exception:\n'
                   '>>> ENTER_EXPLANATION_HERE <<<\n\n')

    if need_interaction:
        confirmation_prompt()

    base_version = force_base_version or ubuntu_version

    if newsource:
        report += 'All changelog entries:\n\n'
    else:
        report += ('Changelog entries since current %s version %s:\n\n'
                   % (release, ubuntu_version))
    changelog = debian_srcpkg.getChangelog(since_version=base_version)
    if not changelog:
        if not options.missing_changelog_ok:
            print >> sys.stderr, ("E: Did not retrieve any changelog entries. "
                                  "Do you need to specify '-C'? "
                                  "Was the package recently uploaded? (check "
                                  "http://packages.debian.org/changelogs/)")
            sys.exit(1)
        else:
            need_interaction = True
            changelog = "XXX FIXME: add changelog here XXX"
    report += changelog

    editor = EditBugReport(title, report)
    editor.edit(optional=not need_interaction)
    title, report = editor.get_report()

    if 'XXX FIXME' in report:
        print >> sys.stderr, ("E: changelog boilerplate found in report, "
                              "please manually add changelog when using '-C'")
        sys.exit(1)

    # bug status and bug subscriber
    status = 'confirmed'
    subscribe = 'ubuntu-archive'
    if sponsorship:
        status = 'new'
        subscribe = 'ubuntu-sponsors'
    if ffe:
        status = 'new'
        subscribe = 'ubuntu-release'

    srcpkg = not newsource and srcpkg or None
    if lpapi:
        # Map status to the values expected by LP API
        mapping = {'new': 'New', 'confirmed': 'Confirmed'}
        # Post sync request using LP API
        post_bug(srcpkg, subscribe, mapping[status], title, report)
    else:
        email_from = ubu_email(export=False)[1]
        # Mail sync request
        mail_bug(srcpkg, subscribe, status, title, report, bug_mail_domain,
                 options.keyid, email_from, mailserver_host, mailserver_port,
                 mailserver_user, mailserver_pass)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print "\nUser abort."
        sys.exit(2)