def testSplitSizes(self):
     assert len(pexpect.split_command_line(r'')) == 0
     assert len(pexpect.split_command_line(r'one')) == 1
     assert len(pexpect.split_command_line(r'one two')) == 2
     assert len(pexpect.split_command_line(r'one  two')) == 2
     assert len(pexpect.split_command_line(r'one   two')) == 2
     assert len(pexpect.split_command_line(r'one\ one')) == 1
     assert len(pexpect.split_command_line('\'one one\'')) == 1
     assert len(pexpect.split_command_line(r'one\"one')) == 1
     assert len(pexpect.split_command_line(r'This\' is a\'\ test')) == 3
Esempio n. 2
0
 def testSplitSizes(self):
     assert len(pexpect.split_command_line(r"")) == 0
     assert len(pexpect.split_command_line(r"one")) == 1
     assert len(pexpect.split_command_line(r"one two")) == 2
     assert len(pexpect.split_command_line(r"one  two")) == 2
     assert len(pexpect.split_command_line(r"one   two")) == 2
     assert len(pexpect.split_command_line(r"one\ one")) == 1
     assert len(pexpect.split_command_line("'one one'")) == 1
     assert len(pexpect.split_command_line(r"one\"one")) == 1
     assert len(pexpect.split_command_line(r"This\' is a\'\ test")) == 3
 def testSplitSizes(self):
     assert len(pexpect.split_command_line(r'')) == 0
     assert len(pexpect.split_command_line(r'one')) == 1
     assert len(pexpect.split_command_line(r'one two')) == 2
     assert len(pexpect.split_command_line(r'one  two')) == 2
     assert len(pexpect.split_command_line(r'one   two')) == 2
     assert len(pexpect.split_command_line(r'one\ one')) == 1
     assert len(pexpect.split_command_line('\'one one\'')) == 1
     assert len(pexpect.split_command_line(r'one\"one')) == 1
     assert len(pexpect.split_command_line(r'This\' is a\'\ test')) == 3
Esempio n. 4
0
    def run(self, cmd_args, out_stream=sys.stdout, env=None, verbose=False,
            prefix=None, postfix=None, accept_defaults=False, pattern_response=None,
            timeout=0, timeout_interval=1, debug=False, raise_on_interrupt=False):
        """
        Runs the command and returns the output, writing each the output to out_stream if verbose is True.

        :param cmd_args: list of command arguments or str command line
        :type cmd_args: list or str
        :param out_stream: the output stream
        :type out_stream: file
        :param env: the environment variables for the command to use.
        :type env: dict
        :param verbose: if verbose, then echo the command and it's output to stdout.
        :type verbose: bool
        :param prefix: list of command arguments to prepend to the command line
        :type prefix: list[str]
        :param postfix: list of command arguments to append to the command line
        :type postfix: list[str]
        :param accept_defaults: accept responses to default regexes.
        :type accept_defaults: bool
        :param pattern_response: dictionary whose key is a regular expression pattern that when matched
            results in the value being sent to the running process.  If the value is None, then no response is sent.
        :type pattern_response: dict[str, str]
        :param timeout: the maximum time to give the process to complete
        :type timeout: int
        :param timeout_interval: the time to sleep between process output polling
        :type timeout_interval: int
        :param debug: emit debugging info
        :type debug: bool
        :param raise_on_interrupt: on keyboard interrupt, raise the KeyboardInterrupt exception
        :type raise_on_interrupt: bool

        :returns: the output of the command
        :rtype: str
        """
        if isinstance(cmd_args, str):
            cmd_args = pexpect.split_command_line(cmd_args)

        self.display("run(%s, %s)\n\n" % (cmd_args, env), out_stream=out_stream, verbose=debug)
        if pattern_response:
            return self.run_pattern_response(cmd_args, out_stream=out_stream, verbose=verbose,
                                             prefix=prefix, postfix=postfix, debug=debug,
                                             pattern_response=pattern_response)
        if accept_defaults:
            return self.run_pattern_response(cmd_args, out_stream=out_stream, verbose=verbose,
                                             prefix=prefix, postfix=postfix, debug=debug)
        lines = []
        for line in self.run_generator(cmd_args, out_stream=out_stream, env=env, verbose=verbose,
                                       prefix=prefix, postfix=postfix,
                                       timeout=timeout, timeout_interval=timeout_interval,
                                       debug=debug, raise_on_interrupt=raise_on_interrupt):
            lines.append(line)
        return ''.join(lines)