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

1304 by Colin Watson
new-binary-debian-universe: port to Python 3
1
#! /usr/bin/python3
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
2
3
# Copyright (C) 2009, 2010, 2011, 2012 Canonical Ltd.
4
# Authors:
5
#   Martin Pitt <martin.pitt@ubuntu.com>
6
#   Colin Watson <cjwatson@ubuntu.com>
7
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; version 3 of the License.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
"""Process binary NEW entries from Debian.
21
22
Find and accept all binary NEW entries built by source packages synced
23
directly from Debian.  These do not typically need significant review.
24
"""
25
26
from __future__ import print_function
27
28
import atexit
29
from optparse import OptionParser
30
import os
31
import shutil
32
import subprocess
33
import sys
34
import tempfile
35
36
from launchpadlib.launchpad import Launchpad
1303 by Colin Watson
new-binary-debian-universe: simplify urllib imports using six
37
from six.moves.urllib.parse import unquote, urlsplit
38
from six.moves.urllib.request import urlopen, urlretrieve
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
39
from ubuntutools.question import YesNoQuestion
40
41
import lputils
42
43
44
CONSUMER_KEY = "new-binary-debian-universe"
45
46
47
temp_dir = None
48
49
50
def ensure_temp_dir():
51
    global temp_dir
52
    if temp_dir is None:
53
        temp_dir = tempfile.mkdtemp()
54
        atexit.register(shutil.rmtree, temp_dir)
55
56
643 by Colin Watson
new-binary-debian-universe: only operate on a specific pocket if given a suite option
57
def find_matching_uploads(options, explicit_suite):
58
    kwargs = {}
59
    if explicit_suite:
60
        kwargs["pocket"] = options.pocket
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
61
    uploads = options.series.getPackageUploads(
643 by Colin Watson
new-binary-debian-universe: only operate on a specific pocket if given a suite option
62
        archive=options.archive, status="New", **kwargs)
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
63
    for upload in uploads:
64
        if upload.contains_build:
65
            if upload.changes_file_url is None:
66
                continue
67
            # display_name is inaccurate for the theoretical case of an
68
            # upload containing multiple builds, but in practice it's close
69
            # enough.
70
            source = upload.display_name.split(",")[0]
71
            if source == "linux":
72
                continue
73
            binaries = upload.getBinaryProperties()
515 by Colin Watson
new-binary-debian-universe: fix custom upload exclusion
74
            binaries = [b for b in binaries if "customformat" not in b]
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
75
            if [b for b in binaries if "ubuntu" in b["version"]]:
76
                continue
77
            changes_file = urlopen(upload.changes_file_url)
78
            try:
1304 by Colin Watson
new-binary-debian-universe: port to Python 3
79
                changes = changes_file.read().decode("UTF-8", errors="replace")
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
80
            finally:
81
                changes_file.close()
82
            if (" unstable; urgency=" not in changes and
677 by Colin Watson
make all scripts pass current stricter pep8(1) in raring
83
                    " experimental; urgency=" not in changes):
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
84
                continue
85
86
            if options.lintian:
87
                ensure_temp_dir()
88
                for url in upload.binaryFileUrls():
89
                    if (not url.endswith("_all.deb") and
677 by Colin Watson
make all scripts pass current stricter pep8(1) in raring
90
                            not url.endswith("_i386.deb")):
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
91
                        continue
92
                    filename = unquote(urlsplit(url)[2].split("/")[-1])
93
                    print("Fetching %s ..." % filename)
94
                    path = os.path.join(temp_dir, filename)
95
                    urlretrieve(url, path)
96
                    lintian = subprocess.Popen(
97
                        ["lintian", path], stdout=subprocess.PIPE,
98
                        universal_newlines=True)
99
                    out = lintian.communicate()[0]
100
                    if lintian.returncode != 0:
101
                        print("\n=== %s ===\n%s" % (filename, out),
102
                              file=sys.stderr)
103
513 by Colin Watson
new-binary-debian-universe: show overrides for each binary
104
            yield upload, binaries
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
105
106
644 by Colin Watson
new-binary-debian-universe: fix find_and_accept signature
107
def find_and_accept(options, explicit_suite):
917 by Colin Watson
new-binary-debian-universe: collect all upload information before starting user interaction; seems to make the tool more comfortable to use
108
    for upload, binaries in list(
109
            find_matching_uploads(options, explicit_suite)):
809 by Colin Watson
new-binary-debian-universe: allow limiting by source packages using new --source option
110
        if options.source and upload.package_name not in options.source:
111
            continue
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
112
        display = "%s/%s (%s)" % (
113
            upload.display_name, upload.display_version, upload.display_arches)
114
        if options.dry_run:
115
            print("Would accept %s" % display)
116
        else:
513 by Colin Watson
new-binary-debian-universe: show overrides for each binary
117
            for binary in binaries:
118
                if "customformat" not in binary:
119
                    print("%s | %s Component: %s Section: %s Priority: %s" % (
120
                        "N" if binary["is_new"] else "*", binary["name"],
121
                        binary["component"], binary["section"],
122
                        binary["priority"]))
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
123
            if not options.confirm_all:
124
                if YesNoQuestion().ask("Accept %s" % display, "no") == "no":
125
                    continue
126
            print("Accepting %s" % display)
127
            upload.acceptFromQueue()
128
129
130
def main():
131
    parser = OptionParser(usage="usage: %prog [options]")
132
    parser.add_option(
133
        "-l", "--launchpad", dest="launchpad_instance", default="production")
134
    parser.add_option(
135
        "-d", "--distribution", metavar="DISTRO", default="ubuntu",
136
        help="look in distribution DISTRO")
137
    parser.add_option(
138
        "-s", "--suite", metavar="SUITE", help="look in suite SUITE")
139
    parser.add_option(
140
        "-n", "--dry-run", default=False, action="store_true",
141
        help="don't make any modifications")
142
    parser.add_option(
143
        "-y", "--confirm-all", default=False, action="store_true",
144
        help="do not ask for confirmation")
145
    parser.add_option(
146
        "--lintian", default=False, action="store_true",
147
        help="run packages through Lintian")
809 by Colin Watson
new-binary-debian-universe: allow limiting by source packages using new --source option
148
    parser.add_option(
149
        "--source", action="append", metavar="NAME",
150
        help="only consider source package NAME")
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
151
    options, _ = parser.parse_args()
152
153
    options.launchpad = Launchpad.login_with(
154
        CONSUMER_KEY, options.launchpad_instance, version="devel")
643 by Colin Watson
new-binary-debian-universe: only operate on a specific pocket if given a suite option
155
    explicit_suite = options.suite is not None
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
156
    lputils.setup_location(options)
157
643 by Colin Watson
new-binary-debian-universe: only operate on a specific pocket if given a suite option
158
    find_and_accept(options, explicit_suite)
508 by Colin Watson
new-binary-debian-universe: new script, based on a local script on ftpmaster but converted to use the API
159
160
161
if __name__ == '__main__':
162
    main()