~rbalint/ubuntu-archive-tools/bzr-to-git

« back to all changes in this revision

Viewing changes to change-override

  • Committer: Balint Reczey
  • Date: 2020-12-18 12:26:22 UTC
  • Revision ID: balint.reczey@canonical.com-20201218122622-nq4zud81cqg40bl4
Moved to git at https://git.launchpad.net/ubuntu-archive-tools

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python3
2
 
 
3
 
# Copyright 2012 Canonical Ltd.
4
 
# Author: Colin Watson <cjwatson@ubuntu.com>
5
 
 
6
 
# This program is free software: you can redistribute it and/or modify
7
 
# it under the terms of the GNU General Public License as published by
8
 
# the Free Software Foundation; version 3 of the License.
9
 
#
10
 
# This program is distributed in the hope that it will be useful,
11
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
# GNU General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License
16
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
 
18
 
"""Override a publication."""
19
 
 
20
 
from __future__ import print_function
21
 
 
22
 
from collections import OrderedDict
23
 
from optparse import OptionParser, SUPPRESS_HELP
24
 
 
25
 
from launchpadlib.launchpad import Launchpad
26
 
from ubuntutools.question import YesNoQuestion
27
 
 
28
 
import lputils
29
 
 
30
 
 
31
 
def find_publications(options, packages):
32
 
    for package in packages:
33
 
        # Change matching source.
34
 
        if (options.source_and_binary or options.binary_and_source or
35
 
                options.source_only):
36
 
            source = lputils.find_latest_published_source(options, package)
37
 
            yield "source", source
38
 
 
39
 
        # Change all binaries for matching source.
40
 
        if options.source_and_binary:
41
 
            for binary in source.getPublishedBinaries():
42
 
                if not binary.is_debug:
43
 
                    yield "binary", binary
44
 
        # Change matching binaries.
45
 
        elif not options.source_only:
46
 
            for binary in lputils.find_latest_published_binaries(
47
 
                    options, package):
48
 
                if not binary.is_debug:
49
 
                    yield "binary", binary
50
 
 
51
 
 
52
 
def stringify_phased_update_percentage(phased_update_percentage):
53
 
    if phased_update_percentage is None:
54
 
        return "100%"
55
 
    else:
56
 
        return '%s%%' % phased_update_percentage
57
 
 
58
 
 
59
 
def stringify_binary_kwargs(binary_kwargs):
60
 
    for key, value in binary_kwargs.items():
61
 
        if key == "new_phased_update_percentage":
62
 
            yield stringify_phased_update_percentage(value)
63
 
        else:
64
 
            yield value
65
 
 
66
 
 
67
 
def change_overrides(options, packages):
68
 
    source_kwargs = OrderedDict()
69
 
    binary_kwargs = OrderedDict()
70
 
    if options.component:
71
 
        print("Override component to %s" % options.component)
72
 
        source_kwargs["new_component"] = options.component
73
 
        binary_kwargs["new_component"] = options.component
74
 
    if options.section:
75
 
        print("Override section to %s" % options.section)
76
 
        source_kwargs["new_section"] = options.section
77
 
        binary_kwargs["new_section"] = options.section
78
 
    if options.priority:
79
 
        print("Override priority to %s" % options.priority)
80
 
        binary_kwargs["new_priority"] = options.priority
81
 
    if options.percentage is not None:
82
 
        print("Override percentage to %s" % options.percentage)
83
 
        binary_kwargs["new_phased_update_percentage"] = options.percentage
84
 
 
85
 
    publications = []
86
 
    for pubtype, publication in find_publications(options, packages):
87
 
        if pubtype == "source" and not source_kwargs:
88
 
            continue
89
 
 
90
 
        publications.append((pubtype, publication))
91
 
 
92
 
        if pubtype == "source":
93
 
            print("%s: %s/%s -> %s" % (
94
 
                publication.display_name,
95
 
                publication.component_name, publication.section_name,
96
 
                "/".join(source_kwargs.values())))
97
 
        else:
98
 
            print("%s: %s/%s/%s/%s -> %s" % (
99
 
                publication.display_name,
100
 
                publication.component_name, publication.section_name,
101
 
                publication.priority_name.lower(),
102
 
                stringify_phased_update_percentage(
103
 
                    publication.phased_update_percentage),
104
 
                "/".join(stringify_binary_kwargs(binary_kwargs))))
105
 
 
106
 
    if options.dry_run:
107
 
        print("Dry run; no publications overridden.")
108
 
    else:
109
 
        if not options.confirm_all:
110
 
            if YesNoQuestion().ask("Override", "no") == "no":
111
 
                return
112
 
 
113
 
        num_overridden = 0
114
 
        num_same = 0
115
 
        for pubtype, publication in publications:
116
 
            if pubtype == "source":
117
 
                kwargs = source_kwargs
118
 
            else:
119
 
                kwargs = binary_kwargs
120
 
            if publication.changeOverride(**kwargs):
121
 
                num_overridden += 1
122
 
            else:
123
 
                print("%s remained the same" % publication.display_name)
124
 
                num_same += 1
125
 
 
126
 
        summary = []
127
 
        if num_overridden:
128
 
            summary.append("%d %s overridden" % (
129
 
                num_overridden,
130
 
                "publication" if num_overridden == 1 else "publications"))
131
 
        if num_same:
132
 
            summary.append("%d %s remained the same" % (
133
 
                num_same, "publication" if num_same == 1 else "publications"))
134
 
        if summary:
135
 
            print("%s." % "; ".join(summary))
136
 
 
137
 
 
138
 
def main():
139
 
    parser = OptionParser(
140
 
        usage="usage: %prog -s suite [options] package [...]",
141
 
        epilog=lputils.ARCHIVE_REFERENCE_DESCRIPTION)
142
 
    parser.add_option(
143
 
        "-l", "--launchpad", dest="launchpad_instance", default="production")
144
 
    parser.add_option(
145
 
        "-n", "--dry-run", default=False, action="store_true",
146
 
        help="only show removals that would be performed")
147
 
    parser.add_option(
148
 
        "-y", "--confirm-all", default=False, action="store_true",
149
 
        help="do not ask for confirmation")
150
 
    parser.add_option("-A", "--archive", help="override in ARCHIVE")
151
 
    parser.add_option(
152
 
        "-s", "--suite", metavar="SUITE", help="override in SUITE")
153
 
    parser.add_option(
154
 
        "-a", "--architecture", dest="architectures", action="append",
155
 
        metavar="ARCHITECTURE",
156
 
        help="architecture tag (may be given multiple times)")
157
 
    parser.add_option(
158
 
        "-e", "--version",
159
 
        metavar="VERSION", help="package version (default: current version)")
160
 
    parser.add_option(
161
 
        "-S", "--source-and-binary", default=False, action="store_true",
162
 
        help="select source and all binaries from this source")
163
 
    parser.add_option(
164
 
        "-B", "--binary-and-source", default=False, action="store_true",
165
 
        help="select source and binary (of the same name)")
166
 
    parser.add_option(
167
 
        "-t", "--source-only", default=False, action="store_true",
168
 
        help="select source packages only")
169
 
    parser.add_option(
170
 
        "-c", "--component",
171
 
        metavar="COMPONENT", help="move package to COMPONENT")
172
 
    parser.add_option(
173
 
        "-p", "--priority",
174
 
        metavar="PRIORITY", help="move package to PRIORITY")
175
 
    parser.add_option(
176
 
        "-x", "--section",
177
 
        metavar="SECTION", help="move package to SECTION")
178
 
    parser.add_option(
179
 
        "-z", "--percentage", type="int", default=None,
180
 
        metavar="PERCENTAGE", help="set phased update percentage")
181
 
 
182
 
    # Deprecated in favour of -A.
183
 
    parser.add_option(
184
 
        "-d", "--distribution", default="ubuntu", help=SUPPRESS_HELP)
185
 
    parser.add_option(
186
 
        "-j", "--partner", default=False, action="store_true",
187
 
        help=SUPPRESS_HELP)
188
 
    options, args = parser.parse_args()
189
 
 
190
 
    if (not options.component and not options.section and not options.priority
191
 
            and options.percentage is None):
192
 
        parser.error(
193
 
            "You must override at least one of component, section, "
194
 
            "priority, and percentage.")
195
 
 
196
 
    options.launchpad = Launchpad.login_with(
197
 
        "change-override", options.launchpad_instance, version="devel")
198
 
    lputils.setup_location(options)
199
 
 
200
 
    change_overrides(options, args)
201
 
 
202
 
 
203
 
if __name__ == '__main__':
204
 
    main()