~vorlon/ubuntu-archive-tools/what-next

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

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

"""Demote packages to proposed pocket.

This is useful in the case where a package fails to build or is otherwise
broken, but we don't want to remove it from the archive permanently and
would be happy to take a fix by way of a sync from Debian or similar.  In
cases where the package comes from Debian, make sure that any demotion to
proposed is accompanied by a Debian bug report.

This is analogous to removing a package from Debian testing.
"""

from __future__ import print_function

from optparse import OptionParser
import sys

from launchpadlib.launchpad import Launchpad
from ubuntutools.question import YesNoQuestion

import lputils


def demote(options, packages):
    print("Demoting packages to %s-proposed:" % options.suite)
    try:
        demotables = []
        for package in packages:
            source = lputils.find_latest_published_source(options, package)
            demotables.append(source)
            print("\t%s" % source.display_name)
    except lputils.PackageMissing as message:
        print(message, ".  Exiting.")
        sys.exit(1)
    print("Comment: %s" % options.comment)

    if options.dry_run:
        print("Dry run; no packages demoted.")
    else:
        if not options.confirm_all:
            if YesNoQuestion().ask("Demote", "no") == "no":
                return

        for source in demotables:
            options.archive.copyPackage(
                source_name=source.source_package_name,
                version=source.source_package_version,
                from_archive=options.archive,
                from_series=options.series.name, from_pocket="Release",
                to_series=options.series.name, to_pocket="Proposed",
                include_binaries=True, auto_approve=True)
            if not options.confirm_all:
                if YesNoQuestion().ask(
                        "Remove %s from release" % source.source_package_name,
                        "no") == "no":
                    continue
            source.requestDeletion(removal_comment=options.comment)

        print("%d %s successfully demoted." % (
            len(demotables),
            "package" if len(demotables) == 1 else "packages"))


def main():
    parser = OptionParser(
        usage='usage: %prog -m "comment" [options] package [...]')
    parser.add_option(
        "-l", "--launchpad", dest="launchpad_instance", default="production")
    parser.add_option(
        "-n", "--dry-run", default=False, action="store_true",
        help="only show demotions that would be performed")
    parser.add_option(
        "-y", "--confirm-all", default=False, action="store_true",
        help="do not ask for confirmation")
    parser.add_option(
        "-d", "--distribution", default="ubuntu",
        metavar="DISTRIBUTION", help="demote from DISTRIBUTION")
    parser.add_option(
        "-s", "--suite", metavar="SUITE", help="demote from SUITE")
    parser.add_option(
        "-e", "--version",
        metavar="VERSION", help="package version (default: current version)")
    parser.add_option("-m", "--comment", help="demotion comment")
    options, args = parser.parse_args()

    options.launchpad = Launchpad.login_with(
        "demote-to-proposed", options.launchpad_instance, version="devel")
    lputils.setup_location(options)

    if options.comment is None:
        parser.error("You must provide a comment/reason for all demotions.")

    demote(options, args)


if __name__ == '__main__':
    main()