File: requestsync

package info (click to toggle)
ubuntu-dev-tools 0.206
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: python: 9,120; sh: 1,304; perl: 135; makefile: 10
file content (405 lines) | stat: -rwxr-xr-x 13,470 bytes parent folder | download | duplicates (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
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
#!/usr/bin/python3
# -*- 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 argparse
import os
import sys

from distro_info import UbuntuDistroInfo

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

Logger = getLogger()

#
# entry point
#


def main():
    # Our usage options.
    usage = "%(prog)s [options] <source package> [<target release> [base version]]"
    parser = argparse.ArgumentParser(usage=usage)

    parser.add_argument(
        "-d", dest="dist", default="unstable", help="Debian distribution to sync from."
    )
    parser.add_argument(
        "-k",
        dest="keyid",
        default=None,
        help="GnuPG key ID to use for signing report "
        "(only used when emailing the sync request).",
    )
    parser.add_argument(
        "-n",
        action="store_true",
        dest="newpkg",
        default=False,
        help="Whether package to sync is a new package in Ubuntu.",
    )
    parser.add_argument(
        "--email",
        action="store_true",
        default=False,
        help="Use a PGP-signed email for filing the sync request, rather than the LP API.",
    )
    parser.add_argument(
        "--lp",
        dest="deprecated_lp_flag",
        action="store_true",
        default=False,
        help=argparse.SUPPRESS,
    )
    parser.add_argument(
        "-l",
        "--lpinstance",
        metavar="INSTANCE",
        dest="lpinstance",
        default=None,
        help="Launchpad instance to connect to (default: production).",
    )
    parser.add_argument(
        "-s", action="store_true", dest="sponsorship", default=False, help="Force sponsorship"
    )
    parser.add_argument(
        "-C",
        action="store_true",
        dest="missing_changelog_ok",
        default=False,
        help="Allow changelog to be manually filled in when missing",
    )
    parser.add_argument(
        "-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_argument(
        "--no-conf",
        action="store_true",
        dest="no_conf",
        default=False,
        help="Don't read config files or environment variables",
    )
    parser.add_argument("source_package", help=argparse.SUPPRESS)
    parser.add_argument("release", nargs="?", help=argparse.SUPPRESS)
    parser.add_argument("base_version", nargs="?", type=Version, help=argparse.SUPPRESS)
    args = parser.parse_args()

    require_utf8()

    config = UDTConfig(args.no_conf)

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

    if args.keyid is None:
        args.keyid = config.get_value("KEYID")

    if not args.lpapi:
        if args.lpinstance == "production":
            bug_mail_domain = "bugs.launchpad.net"
        elif args.lpinstance == "staging":
            bug_mail_domain = "bugs.staging.launchpad.net"
        else:
            Logger.error("Error: Unknown launchpad instance: %s", args.lpinstance)
            sys.exit(1)

    mailserver_host = config.get_value(
        "SMTP_SERVER", default=None, compat_keys=["UBUSMTP", "DEBSMTP"]
    )
    if not args.lpapi and not mailserver_host:
        try:
            import DNS  # pylint: disable=import-outside-toplevel

            DNS.DiscoverNameServers()
            mxlist = DNS.mxlookup(bug_mail_domain)
            firstmx = mxlist[0]
            mailserver_host = firstmx[1]
        except ImportError:
            Logger.error("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
    # pylint: disable=import-outside-toplevel
    if args.lpapi:
        from ubuntutools.lp.lpapicache import Distribution, Launchpad
        from ubuntutools.requestsync.lp import (
            check_existing_reports,
            get_debian_srcpkg,
            get_ubuntu_delta_changelog,
            get_ubuntu_srcpkg,
            need_sponsorship,
            post_bug,
        )

        # See if we have LP credentials and exit if we don't -
        # cannot continue in this case

        try:
            # devel for changelogUrl()
            Launchpad.login(service=args.lpinstance, api_version="devel")
        except IOError:
            sys.exit(1)
    else:
        from ubuntutools.requestsync.mail import (
            check_existing_reports,
            get_debian_srcpkg,
            get_ubuntu_delta_changelog,
            get_ubuntu_srcpkg,
            mail_bug,
            need_sponsorship,
        )

        if not any(x in os.environ for x in ("UBUMAIL", "DEBEMAIL", "EMAIL")):
            Logger.error(
                "The environment variable UBUMAIL, DEBEMAIL or EMAIL needs "
                "to be set to let this script mail the sync request."
            )
            sys.exit(1)

    newsource = args.newpkg
    sponsorship = args.sponsorship
    distro = args.dist
    ffe = args.ffe
    lpapi = args.lpapi
    need_interaction = False
    srcpkg = args.source_package

    if not args.release:
        if lpapi:
            args.release = Distribution("ubuntu").getDevelopmentSeries().name
        else:
            ubu_info = UbuntuDistroInfo()
            args.release = ubu_info.devel()
        Logger.warning("Target release missing - assuming %s", args.release)

    # Get the current Ubuntu source package
    try:
        ubuntu_srcpkg = get_ubuntu_srcpkg(srcpkg, args.release, "Proposed")
        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:
            Logger.info("'%s' doesn't exist in 'Ubuntu %s'.", srcpkg, args.release)
            Logger.info("Do you want to sync a new package?")
            confirmation_prompt()
            newsource = True
    except udtexceptions.SeriesNotFoundException as error:
        Logger.error(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 as error:
        Logger.error(error)
        sys.exit(1)
    except udtexceptions.SeriesNotFoundException as error:
        Logger.error(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 args.lpapi:
        # try rmadison
        import ubuntutools.requestsync.mail  # pylint: disable=import-outside-toplevel

        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 as error:
            Logger.error(error)
            sys.exit(1)

    if ubuntu_version == debian_version:
        Logger.error(
            "The versions in Debian and Ubuntu are the same already (%s). Aborting.",
            ubuntu_version,
        )
        sys.exit(1)
    if ubuntu_version > debian_version:
        Logger.error(
            "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, args.release)

    if not sponsorship and not ffe:
        Logger.error(
            "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 = (
        f"{srcpkg} {debian_version} ({ubuntu_component})"
        f" from Debian {distro} ({debian_component})"
    )
    title = f"Sync {pkg_to_sync}"
    if ffe:
        title = "FFe: " + title
    report = f"Please sync {pkg_to_sync}\n\n"

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

        Logger.info("Changes have been made to the package in Ubuntu.")
        Logger.info("Please edit the report and give an explanation.")
        Logger.info("Not saving the report file will abort the request.")
        report += (
            f"Explanation of the Ubuntu delta and why it can be dropped:\n"
            f"{get_ubuntu_delta_changelog(ubuntu_srcpkg)}\n>>> ENTER_EXPLANATION_HERE <<<\n\n"
        )

    if ffe:
        need_interaction = True

        Logger.info("To approve FeatureFreeze exception, you need to state")
        Logger.info("the reason why you feel it is necessary.")
        Logger.info("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 = args.base_version or ubuntu_version

    if newsource:
        report += "All changelog entries:\n\n"
    else:
        report += f"Changelog entries since current {args.release} version {ubuntu_version}:\n\n"
    changelog = debian_srcpkg.getChangelog(since_version=base_version)
    if not changelog:
        if not args.missing_changelog_ok:
            Logger.error(
                "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:
        Logger.error(
            "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 = None if newsource else srcpkg
    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,
            args.keyid,
            email_from,
            mailserver_host,
            mailserver_port,
            mailserver_user,
            mailserver_pass,
        )


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        Logger.error("User abort.")
        sys.exit(2)