~ubuntu-archive/ubuntu-archive-tools/trunk

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

# Copyright 2012 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/>.

"""Remove a package from the archive."""

from __future__ import print_function

from optparse import OptionParser, SUPPRESS_HELP
import sys

from launchpadlib.launchpad import Launchpad
try:
    from ubuntutools.question import YesNoQuestion
except ImportError:
    print("Could not find ubuntutools.question; run sudo apt-get install "
          "python-ubuntutools")
    sys.exit()

import lputils


def find_removables(options, package):
    if options.binaryonly:
        for binary in lputils.find_latest_published_binaries(options, package):
            if not binary.is_debug:
                yield binary, True
    else:
        source = lputils.find_latest_published_source(options, package)
        yield source, True
        for binary in source.getPublishedBinaries():
            if not binary.is_debug:
                yield binary, False


def find_all_removables(options, packages):
    for package in packages:
        try:
            for removable in find_removables(options, package):
                yield removable
        except lputils.PackageMissing as message:
            print(message)
            if options.skip_missing:
                print("Skipping")
            else:
                print("Exiting")
                sys.exit(1)


def remove_package(options, packages):
    removables = []

    print("Removing packages from %s:" % options.suite)
    for removable, direct in find_all_removables(options, packages):
        removables.append((removable, direct))
        print("\t%s%s" % ("" if direct else "\t", removable.display_name))
    print("Comment: %s" % options.removal_comment)

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

        removals = []
        for removable, direct in removables:
            if direct:
                removable.requestDeletion(
                    removal_comment=options.removal_comment)
                removals.append(removable)

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


def main():
    parser = OptionParser(
        usage='usage: %prog -m "comment" [options] package [...]',
        epilog=lputils.ARCHIVE_REFERENCE_DESCRIPTION)
    parser.add_option(
        "-l", "--launchpad", dest="launchpad_instance", default="production")
    parser.add_option(
        "-n", "--dry-run", default=False, action="store_true",
        help="only show removals that would be performed")
    parser.add_option(
        "-y", "--confirm-all", default=False, action="store_true",
        help="do not ask for confirmation")
    parser.add_option("-A", "--archive", help="remove from ARCHIVE")
    parser.add_option(
        "-s", "--suite", metavar="SUITE", help="remove from SUITE")
    parser.add_option(
        "-a", "--architecture", dest="architectures", action="append",
        metavar="ARCHITECTURE",
        help="architecture tag (may be given multiple times)")
    parser.add_option(
        "-e", "--version",
        metavar="VERSION", help="package version (default: current version)")
    parser.add_option(
        "-b", "--binary", dest="binaryonly",
        default=False, action="store_true", help="remove binaries only")
    parser.add_option("-m", "--removal-comment", help="removal comment")
    parser.add_option(
        "--skip-missing", default=False, action="store_true",
        help=(
            "When a package cannot be removed, normally this script exits "
            "with a non-zero status. With --skip-missing instead, the "
            "error is printed and removing continues"))

    # Deprecated in favour of -A.
    parser.add_option(
        "-d", "--distribution", default="ubuntu", help=SUPPRESS_HELP)
    parser.add_option("-p", "--ppa", help=SUPPRESS_HELP)
    parser.add_option("--ppa-name", help=SUPPRESS_HELP)
    parser.add_option(
        "-j", "--partner", default=False, action="store_true",
        help=SUPPRESS_HELP)

    options, args = parser.parse_args()

    options.launchpad = Launchpad.login_with(
        "remove-package", options.launchpad_instance, version="devel")
    lputils.setup_location(options)

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

    remove_package(options, args)


if __name__ == '__main__':
    main()