def process_command(self, command, argument_list, connection, shell=False):
        """
        Run a command with arguments.

        :param command: command
        :type command: Command
        :param argument_list: the list of arguments for the command
        :type argument_list: list
        :param connection: connection (can be None in the case of an ``OFFLINE`` command)
        :type connection: Connection
        :param shell: should we use shell mode?
        :type shell: bool
        """
        if shell or command.OFFLINE:
            p = command.get_command_parser()
        else:
            p = command.get_command_parser(make_connection_parser())
        try:
            args = p.parse_args(argument_list)
        except SystemExit:
            return
        command.set_connection(connection)
        command.set_arguments(args)
        try:
            return command.run()
        except (KeyboardInterrupt, EOFError):
            print
            print "Command interrupted."
            return
        except Exception as e:
            sys.stdout.flush()
            sys.stderr.write('%s\n' % e)
            sys.stderr.flush()
            print_exc()
    def get_shell_parser(self, offline=False):
        """
        Returns the parser for shell arguments.

        :return: parser for shell commands
        :rtype: argparse.ArgumentParser
        """
        parents = [] if offline else [make_connection_parser()]
        parser = ArgumentParser(conflict_handler='resolve',
                                description=self.DESCRIPTION,
                                parents=parents)

        # override default help
        parser.add_argument('-h',
                            '--help',
                            action='store_true',
                            help="show this help message and exit")
        parser.add_argument('-v',
                            '--version',
                            action='store_true',
                            help="show version")
        parser.add_argument('command',
                            metavar='<command>',
                            help='"%s" or empty to use shell' %
                            '", "'.join(self.COMMANDS),
                            nargs='?')
        return parser
Example #3
0
    def process_command(self, command, argument_list, connection, shell=False):
        """
        Run a command with arguments.

        :param command: command
        :type command: Command
        :param argument_list: the list of arguments for the command
        :type argument_list: list
        :param connection: connection (can be None in the case of an ``OFFLINE`` command)
        :type connection: Connection
        :param shell: should we use shell mode?
        :type shell: bool
        """
        if shell or command.OFFLINE:
            p = command.get_command_parser()
        else:
            p = command.get_command_parser(make_connection_parser())
        try:
            args = p.parse_args(argument_list)
        except SystemExit:
            return
        command.set_connection(connection)
        command.set_arguments(args)
        try:
            return command.run()
        except (KeyboardInterrupt, EOFError):
            print
            print "Command interrupted."
            return
        except Exception as e:
            sys.stdout.flush()
            sys.stderr.write('%s\n' % e)
            sys.stderr.flush()
            print_exc()
    def get_shell_parser(self):
        """
        Returns the parser for shell arguments.

        :return: parser for shell commands
        :rtype: argparse.ArgumentParser
        """
        parser = ArgumentParser(conflict_handler='resolve', description=self.DESCRIPTION,
                                parents=[make_connection_parser()])
        # override default help
        parser.add_argument('-h', '--help', action='store_true', help="show this help message and exit")
        parser.add_argument('command', metavar='<command>', help='"%s" or empty to use shell' % '", "'.join(self.COMMANDS), nargs='?')
        return parser
    def preloop(self):
        # Entry point. Check whether we should run a script and exit, or start an interactive shell.

        parser = self.get_shell_parser()
        args, others = parser.parse_known_args()

        if args.version:
            print(__version__)
            exit(0)

        command = self.COMMANDS.get(args.command)
        if command:
            command = command()
        elif args.command:
            print("*** Unknown command: %s" % args.command)
            print(get_help(parser))
            exit(0)
        elif others:
            print("*** Unknown arguments: %s" % ' '.join(others))
            print(get_help(parser))
            exit(0)

        if args.help:
            if not command:
                print(get_help(parser))
            elif command.OFFLINE:
                print(get_help(command.get_command_parser()))
            else:
                print(
                    get_help(
                        command.get_command_parser(make_connection_parser())))
            exit(0)

        if command:
            if not command.OFFLINE:
                self.setup_connection(args)
            else:
                # parse arguments that have same name as connection parser
                parser = self.get_shell_parser(offline=True)
                _, others = parser.parse_known_args()

            exit_code = self.process_command(command, others)
            exit(exit_code)

        # do shell
        try:
            readline.read_history_file(self.get_history_file_path())
            readline.set_history_length(1000)
        except (IOError, NameError):
            pass
        self.set_shell_user(args)
Example #6
0
    def preloop(self):
        """
        Entry point. Check whether we should run a script and exit, or start an interactive shell.
        """

        parser = self.get_shell_parser()
        args, others = parser.parse_known_args()

        command = self.COMMANDS.get(args.command)
        if command:
            command = command()
        elif args.command:
            print "*** Unknown command: %s" % args.command
            print get_help(parser)
            exit(0)
        elif others:
            print "*** Unknown arguments: %s" % ' '.join(others)
            print get_help(parser)
            exit(0)

        if args.help:
            if not command:
                print get_help(parser)
            elif command.OFFLINE:
                print get_help(command.get_command_parser())
            else:
                print get_help(
                    command.get_command_parser(make_connection_parser()))
            exit(0)

        if command:
            if not command.OFFLINE:
                connection = get_connection(args)
            else:
                connection = None
            self.process_command(command, others, connection)
            exit(0)

        # do shell
        try:
            readline.read_history_file(self.get_history_file_path())
        except (IOError, NameError):
            pass
        self.set_shell_user(args)
    def preloop(self):
        """
        Entry point. Check whether we should run a script and exit, or start an interactive shell.
        """

        parser = self.get_shell_parser()
        args, others = parser.parse_known_args()

        command = self.COMMANDS.get(args.command)
        if command:
            command = command()
        elif args.command:
            print "*** Unknown command: %s" % args.command
            print get_help(parser)
            exit(0)
        elif others:
            print "*** Unknown arguments: %s" % ' '.join(others)
            print get_help(parser)
            exit(0)

        if args.help:
            if not command:
                print get_help(parser)
            elif command.OFFLINE:
                print get_help(command.get_command_parser())
            else:
                print get_help(command.get_command_parser(make_connection_parser()))
            exit(0)

        if command:
            if not command.OFFLINE:
                connection = get_connection(args)
            else:
                connection = None
            self.process_command(command, others, connection)
            exit(0)

        # do shell
        try:
            readline.read_history_file(self.get_history_file_path())
        except (IOError, NameError):
            pass
        self.set_shell_user(args)
    def process_command(self, command, argument_list, shell=False):
        """
        Runs the given command with the provided arguments and returns the exit code

        :param command: command
        :type command: Command
        :param argument_list: the list of arguments for the command
        :type argument_list: list
        :param shell: should we use shell mode?
        :type shell: bool
        :return: 0 if the command was executed successfully, 1 otherwise

        :rtype: int
        """
        if shell or command.OFFLINE:
            p = command.get_command_parser()
        else:
            p = command.get_command_parser(make_connection_parser())
        try:
            args = p.parse_args(argument_list)
        except SystemExit:
            return 1
        command.set_connection(self.connection)
        command.set_arguments(args)
        try:
            command.run()
            return 0
        except (KeyboardInterrupt, EOFError):
            print()
            print("Command interrupted.")
        except GenestackException as e:
            sys.stdout.flush()
            sys.stderr.write('%s\n' % e)
        except Exception:
            sys.stdout.flush()
            print_exc()
        return 1