Example #1
0
    def _command(self, cmd, kwargs):
        user = kwargs.pop("user", None)
        preserve_environment = kwargs.pop("preserve_environment", False)
        env = kwargs.pop("env", None)

        command = self._command_prefix(user, preserve_environment)

        # short cut if env not set
        if env is None:
            command += cmd
            log.debug(" ".join((str(x) for x in command)))
            yield command
            return

        tmp_dir = os.path.join(self.location, "tmp")
        with NamedTemporaryFile(dir=tmp_dir) as tmp_file:
            for key, value in env.iteritems():
                tmp_cmd = "export %s=%s" % (
                    pipes.quote(key), pipes.quote(value))
                tmp_file.write(tmp_cmd)
                tmp_file.write("\n")
            tmp_file.write('"$@"\n')
            tmp_file.flush()

            chroot_tmp_file = os.path.basename(tmp_file.name)
            chroot_tmp_file = os.path.join("/tmp", chroot_tmp_file)

            command += ['sh', '-e', chroot_tmp_file] + cmd
            log.debug(" ".join((str(x) for x in command)))
            yield command
Example #2
0
    def _command(self, cmd, kwargs):
        user = kwargs.pop("user", None)
        preserve_environment = kwargs.pop("preserve_environment", False)
        env = kwargs.pop("env", None)

        command = self._command_prefix(user, preserve_environment)

        # short cut if env not set
        if env is None:
            command += cmd
            log.debug(" ".join((str(x) for x in command)))
            yield command
            return

        tmp_dir = os.path.join(self.location, "tmp")
        with NamedTemporaryFile(dir=tmp_dir) as tmp_file:
            for key, value in env.iteritems():
                tmp_cmd = "export %s=%s" % (pipes.quote(key),
                                            pipes.quote(value))
                tmp_file.write(tmp_cmd)
                tmp_file.write("\n")
            tmp_file.write('"$@"\n')
            tmp_file.flush()

            chroot_tmp_file = os.path.basename(tmp_file.name)
            chroot_tmp_file = os.path.join("/tmp", chroot_tmp_file)

            command += ['sh', '-e', chroot_tmp_file] + cmd
            log.debug(" ".join((str(x) for x in command)))
            yield command
Example #3
0
    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()
Example #4
0
    def create_file(self, whence, user=None):
        tmp_dir = os.path.join(self.location, "tmp")
        with NamedTemporaryFile(dir=tmp_dir) as tmp_file:
            chroot_tmp_file = os.path.basename(tmp_file.name)
            chroot_tmp_file = os.path.join("/tmp", chroot_tmp_file)

            log.debug("creating %s" % (tmp_file.name))
            yield tmp_file
            tmp_file.flush()
            self.check_call(['cp', chroot_tmp_file, whence], user=user)
Example #5
0
    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()
Example #6
0
    def create_file(self, whence, user=None):
        tmp_dir = os.path.join(self.location, "tmp")
        with NamedTemporaryFile(dir=tmp_dir) as tmp_file:
            chroot_tmp_file = os.path.basename(tmp_file.name)
            chroot_tmp_file = os.path.join("/tmp", chroot_tmp_file)

            log.debug("creating %s" % (tmp_file.name))
            yield tmp_file
            tmp_file.flush()
            self.check_call(['cp', chroot_tmp_file, whence], user=user)
Example #7
0
    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
Example #8
0
    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
Example #9
0
 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)
Example #10
0
 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)
Example #11
0
 def copy(self, what, whence, user=None):
     with self.create_file(whence, user) as f:
         log.debug("copying %s to %s" % (what, f.name))
         with open(what) as src:
             shutil.copyfileobj(src, f)
Example #12
0
 def copy(self, what, whence, user=None):
     with self.create_file(whence, user) as f:
         log.debug("copying %s to %s" % (what, f.name))
         with open(what) as src:
             shutil.copyfileobj(src, f)