cli: don't read/write readline history more than once
Previously, when creating multiple CLI objects, each one would append the ~/.mininet_history file to readline's internal list. When writing the file back it would be duplicated for each CLI object created. So, over a few mininet runs the history file would grow exponentially. This change moves the readline setup code out of the CLI constructor into a class method and adds a flag to prevent it from being executed more than once.
This commit is contained in:
+23
-11
@@ -56,20 +56,12 @@ class CLI( Cmd ):
|
|||||||
Cmd.__init__( self )
|
Cmd.__init__( self )
|
||||||
info( '*** Starting CLI:\n' )
|
info( '*** Starting CLI:\n' )
|
||||||
|
|
||||||
# Set up history if readline is available
|
|
||||||
try:
|
|
||||||
import readline
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
history_path = os.path.expanduser('~/.mininet_history')
|
|
||||||
if os.path.isfile(history_path):
|
|
||||||
readline.read_history_file(history_path)
|
|
||||||
atexit.register(lambda: readline.write_history_file(history_path))
|
|
||||||
|
|
||||||
if self.inputFile:
|
if self.inputFile:
|
||||||
self.do_source( self.inputFile )
|
self.do_source( self.inputFile )
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self.setup_readline()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
# Make sure no nodes are still waiting
|
# Make sure no nodes are still waiting
|
||||||
@@ -84,6 +76,26 @@ class CLI( Cmd ):
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
output( '\nInterrupt\n' )
|
output( '\nInterrupt\n' )
|
||||||
|
|
||||||
|
has_setup_readline = False
|
||||||
|
@classmethod
|
||||||
|
def setup_readline( cls ):
|
||||||
|
"Set up history if readline is available"
|
||||||
|
|
||||||
|
# Only set up readline once to prevent multiplying the history file
|
||||||
|
if cls.has_setup_readline:
|
||||||
|
return
|
||||||
|
cls.has_setup_readline = True
|
||||||
|
|
||||||
|
try:
|
||||||
|
import readline
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
history_path = os.path.expanduser('~/.mininet_history')
|
||||||
|
if os.path.isfile(history_path):
|
||||||
|
readline.read_history_file(history_path)
|
||||||
|
atexit.register(lambda: readline.write_history_file(history_path))
|
||||||
|
|
||||||
def emptyline( self ):
|
def emptyline( self ):
|
||||||
"Don't repeat last command when you hit return."
|
"Don't repeat last command when you hit return."
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user