~3v1n0/ubuntu-archive-tools/sru-review-bileto-support

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#! /usr/bin/python

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

"""Manage build chroots."""

from __future__ import print_function

__metaclass__ = type

import argparse
import hashlib
import subprocess
import sys
try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

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

import lputils


def get_chroot(args):
    das = args.architectures[0]
    suite_arch = "%s/%s" % (args.suite, das.architecture_tag)
    url = das.getChrootURL(pocket=args.pocket)
    if url is None:
        print("No chroot for %s" % suite_arch)
        return 1
    if args.dry_run:
        print("Would fetch %s" % url)
    else:
        # We use wget here to save on having to implement a progress bar
        # with urlretrieve.
        command = ["wget"]
        if args.filepath is not None:
            command.extend(["-O", args.filepath])
        command.append(url)
        subprocess.check_call(command)
    return 0


def info_chroot(args):
    das = args.architectures[0]
    url = das.getChrootURL(pocket=args.pocket)
    if url is not None:
        print(url)
    return 0


def remove_chroot(args):
    das = args.architectures[0]
    previous_url = das.getChrootURL(pocket=args.pocket)
    if previous_url is not None:
        print("Previous chroot: %s" % previous_url)
    suite_arch = "%s/%s" % (args.suite, das.architecture_tag)
    if args.dry_run:
        print("Would remove chroot from %s" % suite_arch)
    else:
        if not args.confirm_all:
            if YesNoQuestion().ask(
                    "Remove chroot from %s" % suite_arch, "no") == "no":
                return 0
        das.removeChroot(pocket=args.pocket)
    return 0


def set_chroot(args):
    das = args.architectures[0]
    previous_url = das.getChrootURL(pocket=args.pocket)
    if previous_url is not None:
        print("Previous chroot: %s" % previous_url)
    suite_arch = "%s/%s" % (args.suite, das.architecture_tag)
    if args.build_url:
        target = "%s from %s" % (args.filepath, args.build_url)
    else:
        target = args.filepath
    if args.dry_run:
        print("Would set chroot for %s to %s" % (suite_arch, target))
    else:
        if not args.confirm_all:
            if YesNoQuestion().ask(
                    "Set chroot for %s to %s" % (suite_arch, target),
                    "no") == "no":
                return 0
        if args.build_url:
            das.setChrootFromBuild(
                livefsbuild=urlparse(args.build_url).path,
                filename=args.filepath, pocket=args.pocket)
        else:
            with open(args.filepath, "rb") as f:
                data = f.read()
                sha1sum = hashlib.sha1(data).hexdigest()
            das.setChroot(data=data, sha1sum=sha1sum, pocket=args.pocket)
    return 0


commands = {
    "get": get_chroot,
    "info": info_chroot,
    "remove": remove_chroot,
    "set": set_chroot}


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-l", "--launchpad", dest="launchpad_instance", default="production")
    parser.add_argument(
        "-n", "--dry-run", default=False, action="store_true",
        help="only show removals that would be performed")
    parser.add_argument(
        "-y", "--confirm-all", default=False, action="store_true",
        help="do not ask for confirmation")
    parser.add_argument(
        "-d", "--distribution", default="ubuntu",
        metavar="DISTRIBUTION", help="manage chroots for DISTRIBUTION")
    parser.add_argument(
        "-s", "--suite", "--series", dest="suite", metavar="SUITE",
        help="manage chroots for SUITE")
    parser.add_argument(
        "-a", "--architecture", metavar="ARCHITECTURE", required=True,
        help="manage chroots for ARCHITECTURE")
    parser.add_argument(
        "--from-build", dest="build_url", metavar="URL",
        help="Live filesystem build URL to set chroot from")
    parser.add_argument(
        "-f", "--filepath", metavar="PATH",
        help="Chroot file path (or file name if --from-build is given)")
    parser.add_argument("command", choices=sorted(commands.keys()))
    args = parser.parse_args()

    if args.command == "set" and args.filepath is None:
        parser.error("The set command requires a chroot file path (-f).")

    if args.command in ("get", "info"):
        login_method = Launchpad.login_anonymously
    else:
        login_method = Launchpad.login_with
    args.launchpad = login_method(
        "manage-chroot", args.launchpad_instance, version="devel")
    lputils.setup_location(args)

    if args.command == "set" and args.build_url:
        parsed_build_url = urlparse(args.build_url)
        if parsed_build_url.scheme != "":
            service_host = args.launchpad._root_uri.host
            web_host = urlparse(web_root_for_service_root(
                str(args.launchpad._root_uri))).hostname
            if parsed_build_url.hostname not in (service_host, web_host):
                parser.error(
                    "%s is not on this Launchpad instance (%s)" % (
                        args.build_url, web_host))

    return commands[args.command](args)


if __name__ == '__main__':
    sys.exit(main())