Skip to content
Snippets Groups Projects
Commit f063b023 authored by lantz's avatar lantz
Browse files

Merge pull request #478 from rlane/fix-readline-history

cli: don't read/write readline history more than once
parents 17ba6a7c a1edb167
No related branches found
No related tags found
No related merge requests found
...@@ -45,6 +45,10 @@ class CLI( Cmd ): ...@@ -45,6 +45,10 @@ class CLI( Cmd ):
prompt = 'mininet> ' prompt = 'mininet> '
def __init__( self, mininet, stdin=sys.stdin, script=None ): def __init__( self, mininet, stdin=sys.stdin, script=None ):
"""Start and run interactive or batch mode CLI
mininet: Mininet network object
stdin: standard input for CLI
script: script to run in batch mode"""
self.mn = mininet self.mn = mininet
# Local variable bindings for py command # Local variable bindings for py command
self.locals = { 'net': mininet } self.locals = { 'net': mininet }
...@@ -56,7 +60,21 @@ def __init__( self, mininet, stdin=sys.stdin, script=None ): ...@@ -56,7 +60,21 @@ def __init__( self, mininet, stdin=sys.stdin, script=None ):
Cmd.__init__( self ) Cmd.__init__( self )
info( '*** Starting CLI:\n' ) info( '*** Starting CLI:\n' )
# Set up history if readline is available if self.inputFile:
self.do_source( self.inputFile )
return
self.initReadline()
self.run()
readlineInited = False
@classmethod
def initReadline( cls ):
"Set up history if readline is available"
# Only set up readline once to prevent multiplying the history file
if cls.readlineInited:
return
cls.readlineInited = True
try: try:
import readline import readline
except ImportError: except ImportError:
...@@ -67,22 +85,26 @@ def __init__( self, mininet, stdin=sys.stdin, script=None ): ...@@ -67,22 +85,26 @@ def __init__( self, mininet, stdin=sys.stdin, script=None ):
readline.read_history_file(history_path) readline.read_history_file(history_path)
atexit.register(lambda: readline.write_history_file(history_path)) atexit.register(lambda: readline.write_history_file(history_path))
if self.inputFile: def run( self ):
self.do_source( self.inputFile ) "Run our cmdloop(), catching KeyboardInterrupt"
return
while True: while True:
try: try:
# Make sure no nodes are still waiting # Make sure no nodes are still waiting
for node in self.mn.values(): for node in self.mn.values():
while node.waiting: while node.waiting:
info( 'stopping', node, '\n' )
node.sendInt() node.sendInt()
node.waitOutput() node.waitOutput()
if self.isatty(): if self.isatty():
quietRun( 'stty echo sane intr "^C"' ) quietRun( 'stty echo sane intr ^C' )
self.cmdloop() self.cmdloop()
break break
except KeyboardInterrupt: except KeyboardInterrupt:
output( '\nInterrupt\n' ) # Output a message - unless it's also interrupted
try:
output( '\nInterrupt\n' )
except:
pass
def emptyline( self ): def emptyline( self ):
"Don't repeat last command when you hit return." "Don't repeat last command when you hit return."
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment