~laney/ubuntu-archive-tools/retry-autopkgtest-regressions-bileto-v2

533 by Colin Watson
copy-package: new script, replacing copy-package.py
1
#! /usr/bin/python
2
3
# Copyright (C) 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
"""Copy package publication records."""
19
20
from __future__ import print_function
21
22
from optparse import OptionParser, Values
23
import sys
24
25
from launchpadlib.errors import HTTPError
26
from launchpadlib.launchpad import Launchpad
27
from ubuntutools.question import YesNoQuestion
28
29
import lputils
30
31
32
def find_publications(options, package):
33
    source = lputils.find_latest_published_source(options, package)
34
    yield source, source.source_package_version
35
36
    if options.include_binaries:
37
        for binary in source.getPublishedBinaries():
38
            yield binary, binary.binary_package_version
39
40
41
def copy_packages(options, packages):
42
    ret = True
43
44
    for package in packages:
45
        print("Copy candidates:")
46
47
        source = lputils.find_latest_published_source(options, package)
48
        print("\t%s" % source.display_name)
49
        num_copies = 1
50
51
        if options.include_binaries:
52
            for binary in source.getPublishedBinaries():
53
                print("\t%s" % binary.display_name)
54
                num_copies += 1
55
574 by Scott Kitterman
copy-package: Add --to-primary to enable copies from a PPA to the primary archive.
56
        print("Candidate copy target: %s" % options.destination.archive)
533 by Colin Watson
copy-package: new script, replacing copy-package.py
57
        if options.dry_run:
58
            print("Dry run; no packages copied.")
59
        else:
60
            if not options.confirm_all:
61
                if YesNoQuestion().ask("Copy", "no") == "no":
62
                    continue
63
64
            try:
65
                options.destination.archive.copyPackage(
66
                    source_name=package, version=source.source_package_version,
67
                    from_archive=options.archive,
68
                    to_pocket=options.destination.pocket,
69
                    to_series=options.destination.series.name,
70
                    include_binaries=options.include_binaries,
561 by Colin Watson
copy-package: add an option to automatically approve copies
71
                    unembargo=options.unembargo,
72
                    auto_approve=options.auto_approve)
533 by Colin Watson
copy-package: new script, replacing copy-package.py
73
74
                print("%d %s successfully copied." % (
75
                    num_copies, "package" if num_copies == 1 else "packages"))
76
            except HTTPError as e:
77
                print(e.content, file=sys.stderr)
78
                ret = False
79
80
    return ret
81
82
83
def main():
84
    parser = OptionParser(usage="usage: %prog [options] package [...]")
85
    parser.add_option(
86
        "-l", "--launchpad", dest="launchpad_instance", default="production")
87
    parser.add_option(
88
        "-n", "--dry-run", default=False, action="store_true",
633 by Colin Watson
copy-package: fix --dry-run help message
89
        help="only show copies that would be performed")
533 by Colin Watson
copy-package: new script, replacing copy-package.py
90
    parser.add_option(
91
        "-y", "--confirm-all", default=False, action="store_true",
92
        help="do not ask for confirmation")
93
    parser.add_option(
94
        "-d", "--distribution", default="ubuntu",
95
        metavar="DISTRIBUTION", help="copy from DISTRIBUTION")
96
    parser.add_option("-s", "--suite", metavar="SUITE", help="copy from SUITE")
97
    parser.add_option(
98
        "-a", "--architecture",
99
        metavar="ARCHITECTURE", help="architecture tag")
100
    parser.add_option(
101
        "-e", "--version",
102
        metavar="VERSION", help="package version (default: current version)")
103
    parser.add_option(
104
        "-c", "--component",
105
        metavar="COMPONENT", help="copy from COMPONENT")
106
    parser.add_option(
107
        "-p", "--ppa", metavar="OWNER", help="copy from PPA owned by OWNER")
108
    parser.add_option(
109
        "--ppa-name", metavar="NAME", help="copy from PPA named NAME")
110
    parser.add_option(
111
        "-j", "--partner", default=False, action="store_true",
112
        help="copy from partner archive")
113
    parser.add_option(
114
        "-b", "--include-binaries", default=False, action="store_true",
115
        help="copy related binaries")
579 by Colin Watson
copy-package: regularise formatting a bit
116
    parser.add_option(
117
        "--to-primary", default=False, action="store_true",
118
        help="copy (from PPA) to primary archive (default: False)")
533 by Colin Watson
copy-package: new script, replacing copy-package.py
119
    parser.add_option(
572 by Scott Kitterman
copy-package: If unspecified, destination distribution, suite, PPA owner, and partner default to copy from.
120
        "--to-distribution", metavar="DISTRIBUTION",
121
        help="copy to DISTRIBUTION (default: copy from distribution)")
677 by Colin Watson
make all scripts pass current stricter pep8(1) in raring
122
    parser.add_option(
123
        "--to-suite", metavar="SUITE",
572 by Scott Kitterman
copy-package: If unspecified, destination distribution, suite, PPA owner, and partner default to copy from.
124
        help="copy to SUITE (default: copy from suite)")
533 by Colin Watson
copy-package: new script, replacing copy-package.py
125
    parser.add_option(
572 by Scott Kitterman
copy-package: If unspecified, destination distribution, suite, PPA owner, and partner default to copy from.
126
        "--to-ppa", metavar="OWNER",
579 by Colin Watson
copy-package: regularise formatting a bit
127
        help="copy to PPA owned by OWNER (default: copy from PPA owner)")
533 by Colin Watson
copy-package: new script, replacing copy-package.py
128
    parser.add_option(
129
        "--to-ppa-name", metavar="NAME", help="copy to PPA named NAME")
130
    parser.add_option(
131
        "--to-partner", default=False, action="store_true",
132
        help="copy to partner archive")
133
    parser.add_option(
134
        "--unembargo", default=False, action="store_true",
135
        help="allow copying from a private archive to a public archive")
561 by Colin Watson
copy-package: add an option to automatically approve copies
136
    parser.add_option(
137
        "--auto-approve", default=False, action="store_true",
138
        help="automatically approve copy (requires queue admin permissions)")
533 by Colin Watson
copy-package: new script, replacing copy-package.py
139
    options, args = parser.parse_args()
140
141
    if options.ppa and options.partner:
142
        parser.error("cannot copy from partner archive and PPA simultaneously")
143
    if options.to_ppa and options.to_partner:
144
        parser.error("cannot copy to partner archive and PPA simultaneously")
145
146
    options.launchpad = Launchpad.login_with(
147
        "copy-package", options.launchpad_instance, version="devel")
148
    lputils.setup_location(options)
149
    options.destination = Values()
150
    options.destination.launchpad = options.launchpad
151
    options.destination.distribution = options.to_distribution
152
    options.destination.suite = options.to_suite
153
    options.destination.architecture = options.architecture
154
    options.destination.ppa = options.to_ppa
155
    options.destination.ppa_name = options.to_ppa_name
156
    options.destination.partner = options.to_partner
572 by Scott Kitterman
copy-package: If unspecified, destination distribution, suite, PPA owner, and partner default to copy from.
157
158
    # In cases where source is specified, but destination is not, default to
159
    # destination = source
160
    if options.destination.distribution is None:
161
        options.destination.distribution = options.distribution
162
    if options.destination.suite is None:
163
        options.destination.suite = options.suite
580 by Colin Watson
copy-package: more formatting
164
    if (options.ppa is not None and options.to_ppa is None and
677 by Colin Watson
make all scripts pass current stricter pep8(1) in raring
165
            not options.to_primary and not options.destination.partner):
574 by Scott Kitterman
copy-package: Add --to-primary to enable copies from a PPA to the primary archive.
166
        options.destination.ppa = options.ppa
659 by Colin Watson
copy-package: fix partner handling harder
167
    if (options.partner and not options.destination.partner and
677 by Colin Watson
make all scripts pass current stricter pep8(1) in raring
168
            not options.ppa):
572 by Scott Kitterman
copy-package: If unspecified, destination distribution, suite, PPA owner, and partner default to copy from.
169
        options.destination.partner = options.partner
170
658 by Colin Watson
copy-package: don't emit 'cross-partner copies are not allowed' error when assuming --to-partner implicitly
171
    if options.partner != options.destination.partner:
572 by Scott Kitterman
copy-package: If unspecified, destination distribution, suite, PPA owner, and partner default to copy from.
172
        parser.error("cross-partner copies are not allowed")
173
575 by Scott Kitterman
copy-package: Error out if --to-primary and --to-ppa-name= are set.
174
    if options.to_primary and options.to_ppa_name is not None:
579 by Colin Watson
copy-package: regularise formatting a bit
175
        parser.error("--to-ppa-name option set for copy to primary archive")
575 by Scott Kitterman
copy-package: Error out if --to-primary and --to-ppa-name= are set.
176
533 by Colin Watson
copy-package: new script, replacing copy-package.py
177
    lputils.setup_location(options.destination)
178
179
    if options.archive.private and not options.destination.archive.private:
180
        if not options.unembargo:
181
            parser.error(
182
                "copying from a private archive to a public archive requires "
183
                "the --unembargo option")
184
185
        # TODO some equivalent of canModifySuite check?
186
187
    if (options.distribution == options.destination.distribution and
677 by Colin Watson
make all scripts pass current stricter pep8(1) in raring
188
            options.suite == options.destination.suite and
189
            options.pocket == options.destination.pocket and
190
            options.archive == options.destination.archive):
533 by Colin Watson
copy-package: new script, replacing copy-package.py
191
        parser.error("copy destination must differ from source")
192
193
    if copy_packages(options, args):
194
        return 0
195
    else:
196
        return 1
197
198
199
if __name__ == '__main__':
200
    sys.exit(main())