<s> """<STR_LIT>""" <EOL> import subprocess <EOL> import types <EOL> PIPE = subprocess . PIPE <EOL> STDOUT = subprocess . STDOUT <EOL> """<STR_LIT>""" <EOL> DEVNULL = subprocess . STDOUT <EOL> """<STR_LIT>""" <EOL> ANY = - <NUM_LIT:1> <EOL> class ProcError ( Exception ) : <EOL> pass <EOL> class CmdFailedError ( ProcError ) : <EOL> def __init__ ( self , args , msg , cmd_err = None ) : <EOL> msg = '<STR_LIT>' % ( args , msg ) <EOL> if cmd_err : <EOL> indented_err = '<STR_LIT:U+0020>' + '<STR_LIT>' . join ( cmd_err . splitlines ( ) ) <EOL> msg = msg + '<STR_LIT>' + indented_err <EOL> ProcError . __init__ ( self , msg ) <EOL> self . cmd = args <EOL> self . stderr = cmd_err <EOL> class CmdExitCodeError ( CmdFailedError ) : <EOL> def __init__ ( self , args , exit_code , expected_rc = None , cmd_err = None ) : <EOL> msg = '<STR_LIT>' % ( exit_code , ) <EOL> CmdFailedError . __init__ ( self , args , msg , cmd_err ) <EOL> self . exitCode = exit_code <EOL> self . expectedExitCode = expected_rc <EOL> class CmdTerminatedError ( CmdFailedError ) : <EOL> def __init__ ( self , args , signum , expected_sig = None , cmd_err = None ) : <EOL> msg = '<STR_LIT>' % ( signum , ) <EOL> CmdFailedError . __init__ ( self , args , msg , cmd_err ) <EOL> self . signal = signum <EOL> self . expectedSignal = expected_sig <EOL> def _check_result ( args , result , expected , cmd_err , ex_class ) : <EOL> if expected == ANY : <EOL> return <EOL> if expected == None : <EOL> raise ex_class ( args , result , expected , cmd_err ) <EOL> if isinstance ( expected , ( list , tuple ) ) : <EOL> if not result in expected : <EOL> raise ex_class ( args , result , expected , cmd_err ) <EOL> else : <EOL> if result != expected : <EOL> raise ex_class ( args , result , expected , cmd_err ) <EOL> def check_exit_code ( args , exit_code , expected_rc , cmd_err ) : <EOL> return _check_result ( args , exit_code , expected_rc , cmd_err , <EOL> CmdExitCodeError ) <EOL> def check_signal ( args , signum , expected_sig , cmd_err ) : <EOL> return _check_result ( args , signum , expected_sig , cmd_err , <EOL> CmdTerminatedError ) <EOL> def check_status ( args , status , expected_rc = <NUM_LIT:0> , expected_sig = None , cmd_err = None ) : <EOL> if status >= <NUM_LIT:0> : <EOL> check_exit_code ( args , status , expected_rc , cmd_err ) <EOL> else : <EOL> check_signal ( args , - status , expected_sig , cmd_err ) <EOL> def popen_cmd ( args , cwd = None , env = None , stdin = '<STR_LIT>' , <EOL> stdout = subprocess . PIPE , stderr = subprocess . PIPE ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( stdin , types . StringTypes ) : <EOL> stdin = file ( stdin , '<STR_LIT:r>' ) <EOL> if isinstance ( stdout , types . StringTypes ) : <EOL> stdout = file ( stdout , '<STR_LIT:w>' ) <EOL> if isinstance ( stderr , types . StringTypes ) : <EOL> stderr = file ( stderr , '<STR_LIT:w>' ) <EOL> p = subprocess . Popen ( args , stdin = stdin , stdout = stdout , stderr = stderr , <EOL> cwd = cwd , env = env , close_fds = True ) <EOL> return p <EOL> def run_cmd ( args , cwd = None , env = None , expected_rc = <NUM_LIT:0> , expected_sig = None , <EOL> stdin = '<STR_LIT>' , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) : <EOL> """<STR_LIT>""" <EOL> p = popen_cmd ( args , cwd = cwd , env = env , stdin = stdin , stdout = stdout , <EOL> stderr = stderr ) <EOL> ( cmd_out , cmd_err ) = p . communicate ( ) <EOL> status = p . wait ( ) <EOL> check_status ( args , status , expected_rc , expected_sig , cmd_err ) <EOL> return ( status , cmd_out , cmd_err ) <EOL> def run_simple_cmd ( args , cwd = None , env = None , stdout = subprocess . PIPE ) : <EOL> """<STR_LIT>""" <EOL> ( exit_code , cmd_out , cmd_err ) = run_cmd ( args , cwd = cwd , env = env , expected_rc = <NUM_LIT:0> , expected_sig = None , <EOL> stdout = stdout ) <EOL> if cmd_err : <EOL> msg = '<STR_LIT>' <EOL> raise CmdFailedError ( args , msg , cmd_err ) <EOL> return cmd_out <EOL> def run_oneline_cmd ( args , cwd = None , env = None ) : <EOL> """<STR_LIT>""" <EOL> cmd_out = run_simple_cmd ( args , cwd = cwd , env = env ) <EOL> if not cmd_out : <EOL> msg = '<STR_LIT>' <EOL> raise CmdFailedError ( args , msg ) <EOL> lines = cmd_out . split ( '<STR_LIT:\n>' ) <EOL> num_lines = len ( lines ) <EOL> if num_lines < <NUM_LIT:2> : <EOL> msg = '<STR_LIT>' <EOL> raise CmdFailedError ( args , msg ) <EOL> elif num_lines > <NUM_LIT:2> or lines [ <NUM_LIT:1> ] : <EOL> msg = '<STR_LIT>' <EOL> raise CmdFailedError ( args , msg ) <EOL> return lines [ <NUM_LIT:0> ] </s>
