/usr/share/pyshared/schroot/chroot.py is in python-schroot 0.3-1.
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 | from schroot.utils import run_command
from schroot.core import log
from schroot.errors import SchrootError
from contextlib import contextmanager
import shutil
import os
import subprocess
try:
import configparser
except ImportError:
import ConfigParser as configparser # meh, Python 2
class SchrootCommandError(SchrootError):
pass
SCHROOT_BASE = "/var/lib/schroot"
class SchrootChroot(object):
__slots__ = ('session', 'active', 'location')
def __init__(self):
self.session = None
self.active = False
self.location = None
def _command(self, cmd, kwargs):
user = kwargs.pop("user", None)
preserve_environment = kwargs.pop("preserve_environment", False)
command = ['schroot', '-r', '-c', self.session]
if user:
command += ['-u', user]
if preserve_environment:
command += ['-p']
command += ['--'] + cmd
log.debug(" ".join((str(x) for x in command)))
return command
def _safe_run(self, cmd):
# log.debug("Command: %s" % (" ".join(cmd)))
out, err, ret = run_command(cmd)
if ret != 0:
raise SchrootCommandError()
return out, err, ret
def copy(self, what, whence, user=None):
o, e, r = self.run(["mktemp", "-d"],
return_codes=0) # Don't pass user.
# it'll set the perms wonky.
where = o.strip()
try:
what = os.path.abspath(what)
fname = os.path.basename(what)
internal = os.path.join(where, fname)
pth = os.path.join(self.location, internal.lstrip(os.path.sep))
shutil.copy(what, pth)
self.run(['mv', internal, whence], user=user, return_codes=0)
finally:
self.run(['rm', '-rf', where], user=user, return_codes=0)
def get_session_config(self):
cfg = configparser.ConfigParser()
fil = os.path.join(SCHROOT_BASE, 'session', self.session)
if cfg.read(fil) == []:
raise SchrootError("SANITY FAILURE")
return cfg[self.session]
def start(self, chroot_name):
out, err, ret = self._safe_run(['schroot', '-b', '-c', chroot_name])
self.session = out.strip()
self.active = True
log.debug("new session: %s" % (self.session))
out, err, ret = self._safe_run([
'schroot', '--location', '-c', "session:%s" % self.session
])
self.location = out.strip()
def end(self):
if self.session is not None:
out, err, ret = self._safe_run(['schroot', '-e', '-c', self.session])
def __lt__(self, other):
return self.run(other, return_codes=0)
def __floordiv__(self, other):
return UserProxy(other, self)
@contextmanager
def create_file(self, whence, user=None):
o, e, r = self.run(["mktemp", "-d"],
return_codes=0) # Don't pass user.
# it'll set the perms wonky.
where = o.strip()
fname = os.path.basename(whence)
internal = os.path.join(where, fname)
pth = os.path.join(self.location, internal.lstrip(os.path.sep))
log.debug("creating %s" % (pth))
try:
with open(pth, "w") as f:
yield f
log.debug("copy %s to %s" % (internal, whence))
self.run(['mv', internal, whence], user=user, return_codes=0)
finally:
self.run(['rm', '-rf', where], return_codes=0)
def run(self, cmd, **kwargs):
command = self._command(cmd, kwargs)
return run_command(command, **kwargs)
def call(self, cmd, **kwargs):
command = self._command(cmd, kwargs)
return subprocess.call(command, **kwargs)
def check_call(self, cmd, **kwargs):
command = self._command(cmd, kwargs)
return subprocess.check_call(command, **kwargs)
def check_output(self, cmd, **kwargs):
command = self._command(cmd, kwargs)
return subprocess.check_output(command, **kwargs)
def Popen(self, cmd, **kwargs):
command = self._command(cmd, kwargs)
return subprocess.Popen(command, **kwargs)
class UserProxy(SchrootChroot):
__slots__ = ('user')
def __init__(self, user, other):
super(UserProxy, self).__init__()
self.user = user
for entry in other.__slots__:
setattr(self, entry, getattr(other, entry))
def run(self, cmd, return_codes=None):
return super(UserProxy, self).run(cmd, user=self.user,
return_codes=return_codes)
@contextmanager
def schroot(name):
ch = SchrootChroot()
try:
ch.start(name)
yield ch
finally:
ch.end()
|