schroot-0.3/0000755000175000017500000000000012165614027012411 5ustar tagtag00000000000000schroot-0.3/setup.cfg0000644000175000017500000000007312165614027014232 0ustar tagtag00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 schroot-0.3/PKG-INFO0000644000175000017500000000032512165614027013506 0ustar tagtag00000000000000Metadata-Version: 1.0 Name: schroot Version: 0.3 Summary: schroot chroot schroots! Home-page: http://pault.ag/ Author: Paul Tagliamonte Author-email: tag@pault.ag License: Expat Description: UNKNOWN Platform: any schroot-0.3/schroot/0000755000175000017500000000000012165614027014072 5ustar tagtag00000000000000schroot-0.3/schroot/core.py0000644000175000017500000000051012150164155015364 0ustar tagtag00000000000000import logging log = logging.getLogger('schroot') def set_debug(): log.setLevel(logging.DEBUG) _ch = logging.StreamHandler() _ch.setLevel(logging.DEBUG) _formatter = logging.Formatter( '[%(levelname)s] %(created)f: (%(funcName)s) %(message)s') _ch.setFormatter(_formatter) log.addHandler(_ch) schroot-0.3/schroot/__init__.py0000644000175000017500000000013012165614003016167 0ustar tagtag00000000000000from schroot.chroot import schroot # noqa __appname__ = "schroot" __version__ = "0.3" schroot-0.3/schroot/utils.py0000644000175000017500000000162412152433154015603 0ustar tagtag00000000000000import subprocess import shlex def run_command(command, stdin=None, encoding='utf-8', return_codes=None): if not isinstance(command, list): command = shlex.split(command) try: pipe = subprocess.Popen(command, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError: return (None, None, -1) kwargs = {} if stdin: kwargs['input'] = stdin.read() (output, stderr) = (x.decode(encoding) for x in pipe.communicate(**kwargs)) if return_codes is not None: if not isinstance(return_codes, tuple): return_codes = (return_codes, ) if pipe.returncode not in return_codes: raise SchrootCommandError("Bad return code %d" % pipe.returncode) return (output, stderr, pipe.returncode) schroot-0.3/schroot/errors.py0000644000175000017500000000005212150164155015751 0ustar tagtag00000000000000 class SchrootError(Exception): pass schroot-0.3/schroot/chroot.py0000644000175000017500000001124412165613767015756 0ustar tagtag00000000000000from 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() schroot-0.3/schroot.egg-info/0000755000175000017500000000000012165614027015564 5ustar tagtag00000000000000schroot-0.3/schroot.egg-info/top_level.txt0000644000175000017500000000001012165614026020304 0ustar tagtag00000000000000schroot schroot-0.3/schroot.egg-info/PKG-INFO0000644000175000017500000000032512165614026016660 0ustar tagtag00000000000000Metadata-Version: 1.0 Name: schroot Version: 0.3 Summary: schroot chroot schroots! Home-page: http://pault.ag/ Author: Paul Tagliamonte Author-email: tag@pault.ag License: Expat Description: UNKNOWN Platform: any schroot-0.3/schroot.egg-info/dependency_links.txt0000644000175000017500000000000112165614026021631 0ustar tagtag00000000000000 schroot-0.3/schroot.egg-info/SOURCES.txt0000644000175000017500000000033512165614026017450 0ustar tagtag00000000000000setup.py schroot/__init__.py schroot/chroot.py schroot/core.py schroot/errors.py schroot/utils.py schroot.egg-info/PKG-INFO schroot.egg-info/SOURCES.txt schroot.egg-info/dependency_links.txt schroot.egg-info/top_level.txtschroot-0.3/setup.py0000755000175000017500000000065712150164220014123 0ustar tagtag00000000000000from schroot import __appname__, __version__ from setuptools import setup long_description = "" setup( name=__appname__, version=__version__, scripts=[], packages=[ 'schroot', ], author="Paul Tagliamonte", author_email="tag@pault.ag", long_description=long_description, description='schroot chroot schroots!', license="Expat", url="http://pault.ag/", platforms=['any'], )