<s> """<STR_LIT>""" <EOL> import shlex <EOL> import subprocess <EOL> __author__ = '<STR_LIT>' <EOL> __license__ = '<STR_LIT>' <EOL> __version__ = ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> class ShellException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class MissingCommandException ( ShellException ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class CommandError ( ShellException ) : <EOL> """<STR_LIT>""" <EOL> error_code = <NUM_LIT:1> <EOL> class Shell ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , has_input = False , record_output = True , record_errors = True , <EOL> strip_empty = True ) : <EOL> self . has_input = has_input <EOL> self . record_output = record_output <EOL> self . record_errors = record_errors <EOL> self . strip_empty = strip_empty <EOL> self . last_command = '<STR_LIT>' <EOL> self . line_breaks = '<STR_LIT:\n>' <EOL> self . pid = None <EOL> self . code = <NUM_LIT:0> <EOL> self . _popen = None <EOL> self . _stdout = '<STR_LIT>' <EOL> self . _stderr = '<STR_LIT>' <EOL> def _split_command ( self , command ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( command , ( tuple , list ) ) : <EOL> return command <EOL> return shlex . split ( command ) <EOL> def _handle_output ( self , stdout , stderr ) : <EOL> """<STR_LIT>""" <EOL> if self . record_output : <EOL> if stdout != None : <EOL> self . _stdout += stdout <EOL> if self . record_errors : <EOL> if stderr != None : <EOL> self . _stderr += stderr <EOL> def _communicate ( self , the_input = None ) : <EOL> """<STR_LIT>""" <EOL> stdout , stderr = self . _popen . communicate ( input = the_input ) <EOL> self . _handle_output ( stdout , stderr ) <EOL> if self . _popen . returncode is not None : <EOL> self . code = self . _popen . returncode <EOL> def run ( self , command ) : <EOL> """<STR_LIT>""" <EOL> self . last_command = command <EOL> command_bits = self . _split_command ( command ) <EOL> kwargs = { <EOL> '<STR_LIT>' : subprocess . PIPE , <EOL> '<STR_LIT>' : subprocess . PIPE , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> if self . has_input : <EOL> kwargs [ '<STR_LIT>' ] = subprocess . PIPE <EOL> self . _popen = subprocess . Popen ( <EOL> command_bits , <EOL> ** kwargs <EOL> ) <EOL> self . pid = self . _popen . pid <EOL> if not self . has_input : <EOL> self . _communicate ( ) <EOL> return self <EOL> def write ( self , the_input ) : <EOL> """<STR_LIT>""" <EOL> if not self . _popen : <EOL> raise MissingCommandException ( <EOL> "<STR_LIT>" <EOL> ) <EOL> self . _communicate ( the_input ) <EOL> return self <EOL> def kill ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _popen : <EOL> raise MissingCommandException ( <EOL> "<STR_LIT>" <EOL> ) <EOL> self . _popen . kill ( ) <EOL> return self <EOL> def output ( self , raw = False ) : <EOL> """<STR_LIT>""" <EOL> if raw : <EOL> return self . _stdout <EOL> lines = self . _stdout . split ( self . line_breaks ) <EOL> if self . strip_empty : <EOL> lines = [ line for line in lines if line ] <EOL> return lines <EOL> def errors ( self , raw = False ) : <EOL> """<STR_LIT>""" <EOL> if raw : <EOL> return self . _stderr <EOL> lines = self . _stderr . split ( self . line_breaks ) <EOL> if self . strip_empty : <EOL> lines = [ line for line in lines if line ] <EOL> return lines <EOL> def shell ( command , has_input = False , record_output = True , record_errors = True , <EOL> strip_empty = True ) : <EOL> """<STR_LIT>""" <EOL> sh = Shell ( <EOL> has_input = has_input , <EOL> record_output = record_output , <EOL> record_errors = record_errors , <EOL> strip_empty = strip_empty <EOL> ) <EOL> return sh . run ( command ) </s>
