<s> '''<STR_LIT>''' <EOL> import pexpect , sys <EOL> from framework . lib . general import * <EOL> from framework . shell import blocking_shell <EOL> from collections import defaultdict <EOL> class PExpectShell ( blocking_shell . Shell ) : <EOL> def __init__ ( self ) : <EOL> blocking_shell . Shell . __init__ ( self ) <EOL> self . Connection = None <EOL> self . Options = None <EOL> self . CommandTimeOffset = '<STR_LIT>' <EOL> def CheckConnection ( self , AbortMessage ) : <EOL> if not self . Connection : <EOL> cprint ( "<STR_LIT>" + AbortMessage ) <EOL> return False <EOL> return True <EOL> def Read ( self , Time = <NUM_LIT:1> ) : <EOL> Output = '<STR_LIT>' <EOL> if not self . CheckConnection ( '<STR_LIT>' ) : <EOL> return Output <EOL> try : <EOL> Output = self . Connection . after <EOL> if Output == None : <EOL> Output = '<STR_LIT>' <EOL> print Output <EOL> except pexpect . EOF : <EOL> cprint ( "<STR_LIT>" ) <EOL> return Output <EOL> return Output <EOL> def FormatCommand ( self , Command ) : <EOL> if "<STR_LIT>" in self . Options and '<STR_LIT>' in self . Options : <EOL> return self . Options [ '<STR_LIT>' ] + '<STR_LIT::>' + self . Options [ '<STR_LIT>' ] + '<STR_LIT>' + Command <EOL> else : <EOL> return "<STR_LIT>" + Command <EOL> def Run ( self , Command ) : <EOL> Output = '<STR_LIT>' <EOL> Cancelled = False <EOL> if not self . CheckConnection ( "<STR_LIT>" + Command ) : <EOL> return Output <EOL> LogCommand = self . FormatCommand ( Command ) <EOL> CommandInfo = self . StartCommand ( LogCommand , LogCommand ) <EOL> try : <EOL> cprint ( "<STR_LIT>" + Command ) <EOL> self . Connection . sendline ( Command ) <EOL> self . FinishCommand ( CommandInfo , Cancelled ) <EOL> except pexpect . EOF : <EOL> Cancelled = True <EOL> cprint ( "<STR_LIT>" ) <EOL> self . FinishCommand ( CommandInfo , Cancelled ) <EOL> except KeyboardInterrupt : <EOL> Cancelled = True <EOL> self . FinishCommand ( CommandInfo , Cancelled ) <EOL> Output += self . error_handler . UserAbort ( '<STR_LIT>' , Output ) <EOL> if not Cancelled : <EOL> self . FinishCommand ( CommandInfo , Cancelled ) <EOL> return Output <EOL> def Expect ( self , Pattern , TimeOut = - <NUM_LIT:1> ) : <EOL> if self . Connection == None : <EOL> return False <EOL> try : <EOL> self . Connection . expect ( Pattern , TimeOut ) <EOL> except pexpect . EOF : <EOL> cprint ( "<STR_LIT>" ) <EOL> except pexpect . TIMEOUT : <EOL> cprint ( "<STR_LIT>" + Pattern + "<STR_LIT>" ) <EOL> cprint ( "<STR_LIT>" ) <EOL> print self . Connection . after <EOL> cprint ( "<STR_LIT>" ) <EOL> print self . Connection . after <EOL> return True <EOL> def RunCommandList ( self , CommandList ) : <EOL> Output = "<STR_LIT>" <EOL> for Command in CommandList : <EOL> Output += self . Run ( Command ) <EOL> return Output <EOL> def Open ( self , Options , PluginInfo ) : <EOL> self . Options = Options <EOL> Output = '<STR_LIT>' <EOL> if not self . Connection : <EOL> CommandList = [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in Options : <EOL> Name , Command = Options [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> CommandList += Command . split ( "<STR_LIT:;>" ) <EOL> CmdCount = <NUM_LIT:1> <EOL> for Cmd in CommandList : <EOL> if CmdCount == <NUM_LIT:1> : <EOL> self . Connection = pexpect . spawn ( Cmd ) <EOL> self . Connection . logfile = sys . stdout <EOL> else : <EOL> self . Run ( Cmd ) <EOL> CmdCount += <NUM_LIT:1> <EOL> if '<STR_LIT>' in Options and Options [ '<STR_LIT>' ] : <EOL> Output += self . RunCommandList ( Options [ '<STR_LIT>' ] ) <EOL> return Output <EOL> def Kill ( self ) : <EOL> cprint ( "<STR_LIT>" ) <EOL> self . Connection . kill ( <NUM_LIT:0> ) <EOL> self . Connection = None <EOL> def Wait ( self ) : <EOL> cprint ( "<STR_LIT>" ) <EOL> self . Connection . wait ( ) <EOL> self . Connection = None <EOL> def Close ( self , PluginInfo ) : <EOL> if self . Connection == None : <EOL> cprint ( "<STR_LIT>" ) <EOL> return False <EOL> if '<STR_LIT>' in self . Options and self . Options [ '<STR_LIT>' ] : <EOL> cprint ( "<STR_LIT>" ) <EOL> self . RunCommandList ( self . Options [ '<STR_LIT>' ] . split ( self . Options [ '<STR_LIT>' ] ) ) <EOL> cprint ( "<STR_LIT>" ) <EOL> self . Run ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in self . Options and self . Options [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . Kill ( ) <EOL> else : <EOL> self . Wait ( ) <EOL> def IsClosed ( self ) : <EOL> return self . Connection == None </s>
