/usr/lib/python3/dist-packages/click/commands/chroot.py is in python3-click-package 0.4.43+16.04.20160203-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | #! /usr/bin/python3
# Copyright (C) 2013 Canonical Ltd.
# Author: Brian Murray <brian@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/>.
"""Use and manage a Click chroot."""
from __future__ import print_function
from argparse import ArgumentParser, REMAINDER
from contextlib import contextmanager
import os
from click.chroot import (
ClickChroot,
ClickChrootAlreadyExistsException,
ClickChrootDoesNotExistException,
)
from click import osextras
def requires_root(parser):
if os.getuid() != 0:
parser.error("must be run as root; try sudo")
@contextmanager
def message_on_error(exc, msg):
"""
Context Manager that prints the error message 'msg' on exception 'exc'
"""
try:
yield
except exc:
print(msg)
# FIXME: i18n(?)
class ErrorMessages:
EXISTS = """A chroot for that name and architecture already exists.
Please see the man-page how to use it."""
NOT_EXISTS = """A chroot for that name and architecture does not exist.
Please use 'create' to create it."""
def create(parser, args):
if not osextras.find_on_path("debootstrap"):
parser.error(
"debootstrap not installed and configured; install click-dev and "
"debootstrap")
requires_root(parser)
chroot = ClickChroot(
args.architecture, args.framework, name=args.name, series=args.series)
with message_on_error(
ClickChrootAlreadyExistsException, ErrorMessages.EXISTS):
return chroot.create(args.keep_broken_chroot)
# if we reach this point there was a error so return exit_status 1
return 1
def install(parser, args):
packages = args.packages
chroot = ClickChroot(
args.architecture, args.framework, name=args.name,
session=args.session)
with message_on_error(
ClickChrootDoesNotExistException, ErrorMessages.NOT_EXISTS):
return chroot.install(*packages)
# if we reach this point there was a error so return exit_status 1
return 1
def destroy(parser, args):
requires_root(parser)
# ask for confirmation?
chroot = ClickChroot(args.architecture, args.framework, name=args.name)
with message_on_error(
ClickChrootDoesNotExistException, ErrorMessages.NOT_EXISTS):
return chroot.destroy()
# if we reach this point there was a error so return exit_status 1
return 1
def execute(parser, args):
program = args.program
if not program:
program = ["/bin/bash"]
chroot = ClickChroot(
args.architecture, args.framework, name=args.name,
session=args.session)
with message_on_error(
ClickChrootDoesNotExistException, ErrorMessages.NOT_EXISTS):
return chroot.run(*program)
# if we reach this point there was a error so return exit_status 1
return 1
def maint(parser, args):
program = args.program
if not program:
program = ["/bin/bash"]
chroot = ClickChroot(
args.architecture, args.framework, name=args.name,
session=args.session)
with message_on_error(
ClickChrootDoesNotExistException, ErrorMessages.NOT_EXISTS):
return chroot.maint(*program)
# if we reach this point there was a error so return exit_status 1
return 1
def upgrade(parser, args):
chroot = ClickChroot(
args.architecture, args.framework, name=args.name,
session=args.session)
with message_on_error(
ClickChrootDoesNotExistException, ErrorMessages.NOT_EXISTS):
return chroot.upgrade()
# if we reach this point there was a error so return exit_status 1
return 1
def begin_session(parser, args):
chroot = ClickChroot(
args.architecture, args.framework, name=args.name,
session=args.session)
with message_on_error(
ClickChrootDoesNotExistException, ErrorMessages.NOT_EXISTS):
return chroot.begin_session()
# if we reach this point there was a error so return exit_status 1
return 1
def end_session(parser, args):
chroot = ClickChroot(
args.architecture, args.framework, name=args.name,
session=args.session)
with message_on_error(
ClickChrootDoesNotExistException, ErrorMessages.NOT_EXISTS):
return chroot.end_session()
# if we reach this point there was a error so return exit_status 1
return 1
def exists(parser, args):
chroot = ClickChroot(args.architecture, args.framework, name=args.name)
# return shell exit codes 0 on success, 1 on failure
if chroot.exists():
return 0
else:
return 1
def run(argv):
parser = ArgumentParser("click chroot")
subparsers = parser.add_subparsers(
description="management subcommands",
help="valid commands")
parser.add_argument(
"-a", "--architecture", required=True,
help="architecture for the chroot")
parser.add_argument(
"-f", "--framework", default="ubuntu-sdk-14.04",
help="framework for the chroot (default: ubuntu-sdk-14.04)")
parser.add_argument(
"-s", "--series",
help="series to use for a newly-created chroot (defaults to a series "
"appropriate for the framework)")
parser.add_argument(
"-n", "--name", default="click",
help=(
"name of the chroot (default: click; the framework and "
"architecture will be appended)"))
create_parser = subparsers.add_parser(
"create",
help="create a chroot of the provided architecture")
create_parser.add_argument(
"-k", "--keep-broken-chroot", default=False, action="store_true",
help="Keep the chroot even if creating it fails (default is to delete "
"it)")
create_parser.set_defaults(func=create)
destroy_parser = subparsers.add_parser(
"destroy",
help="destroy the chroot")
destroy_parser.set_defaults(func=destroy)
upgrade_parser = subparsers.add_parser(
"upgrade",
help="upgrade the chroot")
upgrade_parser.add_argument(
"-n", "--session-name",
dest='session',
help="persistent chroot session name to upgrade")
upgrade_parser.set_defaults(func=upgrade)
install_parser = subparsers.add_parser(
"install",
help="install packages in the chroot")
install_parser.add_argument(
"-n", "--session-name",
dest='session',
help="persistent chroot session name to install packages in")
install_parser.add_argument(
"packages", nargs="+",
help="packages to install")
install_parser.set_defaults(func=install)
execute_parser = subparsers.add_parser(
"run",
help="run a program in the chroot")
execute_parser.add_argument(
"-n", "--session-name",
dest='session',
help="persistent chroot session name to run a program in")
execute_parser.add_argument(
"program", nargs=REMAINDER,
help="program to run with arguments")
execute_parser.set_defaults(func=execute)
maint_parser = subparsers.add_parser(
"maint",
help="run a maintenance command in the chroot")
maint_parser.add_argument(
"-n", "--session-name",
dest='session',
help="persistent chroot session name to run a maintenance command in")
maint_parser.add_argument(
"program", nargs=REMAINDER,
help="program to run with arguments")
maint_parser.set_defaults(func=maint)
begin_parser = subparsers.add_parser(
"begin-session",
help="begin a persistent chroot session")
begin_parser.add_argument(
"session",
help="new session name")
begin_parser.set_defaults(func=begin_session)
end_parser = subparsers.add_parser(
"end-session",
help="end a persistent chroot session")
end_parser.add_argument(
"session",
help="session name to end")
end_parser.set_defaults(func=end_session)
exists_parser = subparsers.add_parser(
"exists",
help="test if the given chroot exists")
exists_parser.set_defaults(func=exists)
args = parser.parse_args(argv)
if not hasattr(args, "func"):
parser.print_help()
return 1
if (not osextras.find_on_path("schroot") or
not os.path.exists("/etc/schroot/click/fstab")):
parser.error(
"schroot not installed and configured; install click-dev and "
"schroot")
return args.func(parser, args)
|