Skip to content

Commit

Permalink
Merge pull request #54 from HPENetworking/pexpect-timing-fix
Browse files Browse the repository at this point in the history
fix: dev: Add some sleeps to allow bash to turn echo off
  • Loading branch information
ocelotl authored May 16, 2017
2 parents e524ff6 + c009b2f commit 424fa88
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 424fa88

Please sign in to comment.