Skip to content

Commit

Permalink
fix: dev: Add some sleeps to allow bash to turn echo off
Browse files Browse the repository at this point in the history
Command to set prompt was sometimes too fast and were sent
before bash turned off echo (stty -echo) resulting in
unwanted information being displayed. This commit makes
sure bash always have time to turn echo off.
  • Loading branch information
Javier Peralta committed May 16, 2017
1 parent e524ff6 commit c009b2f
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/topology/platforms/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from warnings import warn
from re import sub as regex_sub
from collections import OrderedDict
from time import sleep
from abc import ABCMeta, abstractmethod

from six import add_metaclass
Expand Down Expand Up @@ -672,20 +673,27 @@ def _register_loggers(


class PExpectBashShell(PExpectShell):
"""
Custom shell class for Bash.
"""Custom shell class for Bash.
This custom base class will setup the prompt ``PS1`` to the
``FORCED_PROMPT`` value of the class and will disable the echo of the
device by issuing the ``stty -echo`` command. All this is done in the
``_setup_shell()`` call, which is overriden by this class.
See :class:`PExpectShell`.
:param float delay_after_echo_off: Number of seconds pexpect
should wait after setting echo off before sending another command,
this allows bash enough time to properly turn echo off.
"""
FORCED_PROMPT = '@~~==::BASH_PROMPT::==~~@'

def __init__(
self,
initial_prompt='\w+@.+:.+[#$] ', try_filter_echo=False,
**kwargs):
delay_after_echo_off=0.15, **kwargs):

self._delay_after_echo_off = delay_after_echo_off

super(PExpectBashShell, self).__init__(
PExpectBashShell.FORCED_PROMPT,
Expand All @@ -708,10 +716,17 @@ def _setup_shell(self, connection=None):

# Remove echo
spawn.sendline('stty -echo')

# Sleep for a bit to give bash time to turn echo off
sleep(self._delay_after_echo_off)

spawn.expect(
self._initial_prompt, timeout=self._timeout
)

# Sleep for a bit to give bash time to turn echo off
sleep(self._delay_after_echo_off)

# Change prompt to a pexpect secure prompt
spawn.sendline(
'export PS1={}'.format(PExpectBashShell.FORCED_PROMPT)
Expand Down

0 comments on commit c009b2f

Please sign in to comment.