Ejemplo n.º 1
0
def _invoke(console_output, cmd, *arguments):
    try:
        if console_output:
            retcode, stdout, stderr = cmd[arguments] & pb.TEE()
        else:
            retcode, stdout, stderr = cmd[arguments].run()

        if stdout:
            logging.debug("stdout from %s:\n%s", cmd, stdout)
        if stderr:
            logging.debug("stderr from %s:\n%s", cmd, stderr)

        return retcode, stdout, stderr
    except pb.ProcessExecutionError as pee:
        msg = "cmd exited with code {}: {}".format(pee.retcode, cmd[arguments])
        logging.critical(pee.stderr)
        die(msg, pee.retcode)
Ejemplo n.º 2
0
    def __call__(self, args):
        commands = self.call(args, 'prepare')

        if commands is None:
            return

        if isinstance(commands, plumbum.commands.BaseCommand) or (isinstance(
                commands, (tuple, list)) and len(commands) == 2 and isinstance(
                    commands[0], plumbum.commands.ExecutionModifier)):
            commands = (commands, )

        send = hasattr(commands, 'send')
        run_kwargs = {
            key: value
            for (key, value) in vars(self.prepare).items()
            if key in self.run_kws
        }

        result = thrown = None
        empty_result = (None, None, None)
        iterator = iter(commands)

        while True:
            try:
                if send and result is not None:
                    command = iterator.send(result)
                elif send and thrown is not None:
                    command = iterator.throw(thrown)
                else:
                    command = next(iterator)
            except StopIteration:
                break

            if isinstance(command, (tuple, list)):
                (modifier, command) = command
            else:
                modifier = None

            self.print_command(command)

            if args.execute_commands:
                try:
                    if modifier is not None:
                        if run_kwargs:
                            result = command & modifier(**run_kwargs)
                        else:
                            result = command & modifier

                        if result is None:
                            result = empty_result
                    elif args.foreground:
                        result = command & plumbum.TEE(**run_kwargs)
                    else:
                        result = command.run(**run_kwargs)
                except Exception as exc:
                    if not send:
                        raise

                    result = None
                    thrown = exc
                else:
                    thrown = None
            else:
                result = empty_result