Python pexpect.spawnu() Examples

The following are 15 code examples of pexpect.spawnu(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pexpect , or try the search function .
Example #1
Source File: sever_handler.py    From JapaneseTokenizers with MIT License 6 votes vote down vote up
def launch_process(self, command):
        # type: (Union[bytes,text_type])->None
        """* What you can do
        - It starts process and keep it.
        """
        if not self.option is None:
            command_plus_option = self.command + " " + self.option
        else:
            command_plus_option = self.command

        if six.PY3:
            if shutil.which(command) is None:
                raise Exception("No command at {}".format(command))
            else:
                self.process_analyzer = pexpect.spawnu(command_plus_option)
                self.process_id = self.process_analyzer.pid
        else:
            doc_command_string = "echo '' | {}".format(command)
            command_check = os.system(doc_command_string)
            if not command_check == 0:
                raise Exception("No command at {}".format(command))
            else:
                self.process_analyzer = pexpect.spawnu(command_plus_option)
                self.process_id = self.process_analyzer.pid 
Example #2
Source File: dual_proxy_server.py    From universe with MIT License 6 votes vote down vote up
def recv_ClientInit(self, block):
        # start reward proxy.
        self._log_info('Starting reward proxy server')
        self.reward_proxy = pexpect.spawnu(self.factory.reward_proxy_bin,
                                           logfile=sys.stdout,
                                           timeout=None)

        # wait on reward proxy to be up.
        self._log_info('Waiting for reward proxy server')
        self.reward_proxy.expect('\[RewardProxyServer\]')
        self.reward_proxy_thread = threading.Thread(target=lambda: self.reward_proxy.expect(pexpect.EOF))
        self.reward_proxy_thread.start()

        self._log_info('Reward proxy server is up %s', self.reward_proxy.before)

        super(DualProxyServer, self).recv_ClientInit(block)

        self.logfile_dir = self.log_manager.logfile_dir 
Example #3
Source File: replwrap.py    From camr with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, cmd_or_spawn, orig_prompt, prompt_change,
                 new_prompt=PEXPECT_PROMPT,
                 continuation_prompt=PEXPECT_CONTINUATION_PROMPT):
        if isinstance(cmd_or_spawn, str):
            self.child = pexpect.spawnu(cmd_or_spawn, echo=False)
        else:
            self.child = cmd_or_spawn
        if self.child.echo:
            # Existing spawn instance has echo enabled, disable it
            # to prevent our input from being repeated to output.
            self.child.setecho(False)
            self.child.waitnoecho()

        if prompt_change is None:
            self.prompt = orig_prompt
        else:
            self.set_prompt(orig_prompt,
                        prompt_change.format(new_prompt, continuation_prompt))
            self.prompt = new_prompt
        self.continuation_prompt = continuation_prompt

        self._expect_prompt() 
Example #4
Source File: client.py    From wharfee with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def attach(self, *args, **kwargs):
        """
        Attach to a running container.
        :param kwargs:
        :return: None
        """
        if not args:
            return ['Container name or ID is required.']

        container = args[0]

        def on_after():
            self.is_refresh_containers = True
            self.is_refresh_running = True
            return ['\rDetached from {0}.'.format(container)]

        self.after = on_after

        command = format_command_line('attach', False, args, kwargs)
        process = pexpect.spawnu(command)
        process.interact() 
Example #5
Source File: client.py    From wharfee with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def shell(self, *args, **_):
        """
        Get the shell into a running container. A shortcut for
        docker exec -it /usr/bin/env bash.
        :param kwargs:
        :return: None
        """
        if not args:
            return ['Container name or ID is required.']

        container = args[0]

        shellcmd = 'bash'
        if len(args) > 1:
            shellcmd = ' '.join(args[1:])

        self.after = lambda: ['\rShell to {0} is closed.'.format(container)]

        command = 'docker exec -it {0} {1}'.format(container, shellcmd)
        process = pexpect.spawnu(command)
        process.interact() 
Example #6
Source File: steps.py    From cycli with MIT License 5 votes vote down vote up
def step_start_cycli(context):
    context.cli = pexpect.spawnu("cycli -u neo4j -p password") 
Example #7
Source File: steps.py    From cycli with MIT License 5 votes vote down vote up
def step_start_cycli_read_only(context):
    context.cli = pexpect.spawnu("cycli -u neo4j -p password -r") 
Example #8
Source File: wrappers.py    From pgcli with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run_cli(context, run_args=None, prompt_check=True, currentdb=None):
    """Run the process using pexpect."""
    run_args = run_args or []
    cli_cmd = context.conf.get("cli_command")
    cmd_parts = [cli_cmd] + run_args
    cmd = " ".join(cmd_parts)
    context.cli = pexpect.spawnu(cmd, cwd=context.package_root)
    context.logfile = StringIO()
    context.cli.logfile = context.logfile
    context.exit_sent = False
    context.currentdb = currentdb or context.conf["dbname"]
    context.cli.sendline("\pset pager always")
    if prompt_check:
        wait_prompt(context) 
Example #9
Source File: sever_handler.py    From JapaneseTokenizers with MIT License 5 votes vote down vote up
def restart_process(self):
        # type: ()->None
        if not self.option is None:
            command_plus_option = self.command + " " + self.option
        else:
            command_plus_option = self.command

        self.process_analyzer.kill(sig=9)
        self.process_analyzer = pexpect.spawnu(command_plus_option)
        self.process_id = self.process_analyzer.pid 
Example #10
Source File: cli.py    From simdem with MIT License 5 votes vote down vote up
def get_shell(self):
        """Gets or creates the shell in which to run commands for the
        supplied demo
        """
        if self._shell == None:
            child = pexpect.spawnu('/bin/bash', env=self.demo.env.get(), echo=False, timeout=None)
            ps1 = PEXPECT_PROMPT[:5] + u'\[\]' + PEXPECT_PROMPT[5:]
            ps2 = PEXPECT_CONTINUATION_PROMPT[:5] + u'\[\]' + PEXPECT_CONTINUATION_PROMPT[5:]
            prompt_change = u"PS1='{0}' PS2='{1}' PROMPT_COMMAND=''".format(ps1, ps2)
            self._shell = pexpect.replwrap.REPLWrapper(child, u'\$', prompt_change)
        return self._shell 
Example #11
Source File: test_cli.py    From kube-shell with Apache License 2.0 5 votes vote down vote up
def step_run_cli(self):
        self.cli = pexpect.spawnu('kube-shell') 
Example #12
Source File: wrappers.py    From mssql-cli with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run_cli(context, run_args=None):
    """Run the process using pexpect."""
    run_args = run_args or []
    cli_cmd = context.conf.get('cli_command')
    cmd_parts = [cli_cmd] + run_args
    cmd = ' '.join(cmd_parts)
    context.cli = pexpect.spawnu(cmd, cwd=context.package_root)
    context.exit_sent = False
    context.currentdb = context.conf['dbname'] 
Example #13
Source File: client.py    From wharfee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def login(self, *args, **kwargs):
        """
        Register or log in to a Docker registry server.
        :param kwargs:
        :return: None
        """
        self.after = lambda: ['\r']

        command = format_command_line('login', False, args, kwargs)
        process = pexpect.spawnu(command)
        process.interact() 
Example #14
Source File: client.py    From wharfee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def push(self, *args, **kwargs):
        """
        Push an image into repository. Equivalent of docker push.
        :param kwargs:
        :return: interactive.
        """
        if not args or len(args) < 1:
            return ['Image name (tagged) is required.']

        tag_valid, tag_message = self._is_repo_tag_valid(args[0])
        if not tag_valid:
            return [tag_message]

        self.after = lambda: ['\r']

        # TODO: this command didn't have to use pexpect.
        # But it was easier to call the official CLI than try and figure out
        # why requests throw this error:
        # File "venv/wharfee/lib/python2.7/site-packages/requests/packages/
        # urllib3/response.py", line 267, in read
        # raise ReadTimeoutError(self._pool, None, 'Read timed out.')
        # requests.packages.urllib3.exceptions.ReadTimeoutError:
        # HTTPSConnectionPool(host='192.168.59.103', port=2376): Read timed out.
        command = format_command_line('push', False, args, kwargs)
        process = pexpect.spawnu(command)
        process.interact() 
Example #15
Source File: client.py    From wharfee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def call_external_cli(self, cmd, *args, **kwargs):
        """
        Call the "official" CLI if needed.
        :param args:
        :param kwargs:
        :return:
        """
        called = False

        is_force = kwargs.pop('force', False)
        is_interactive = kwargs.get('interactive', None)
        is_tty = kwargs.get('tty', None)
        is_attach = kwargs.get('attach', None)
        is_attach_bool = is_attach in [True, False]

        def execute_external():
            """
            Call the official cli
            """
            command = format_command_line(cmd, False, args, kwargs)
            process = pexpect.spawnu(command)
            process.interact()

        def on_after_interactive():
            # \r is to make sure when there is some error output,
            # prompt is back to beginning of line
            self.is_refresh_containers = True
            self.is_refresh_running = True
            return ['\rInteractive terminal is closed.']

        def on_after_attach():
            self.is_refresh_containers = True
            self.is_refresh_running = True
            return ['Container exited.\r']

        if is_force or is_interactive or is_tty or (is_attach and not is_attach_bool):
            self.after = on_after_attach if is_attach or (not is_interactive and not is_tty) else on_after_interactive
            called = True
            execute_external()

        return called, args, kwargs