#NEWLINE# def run_cmd( self , cmd , cwd = None , extra_env = None ) : #NEWLINE# #INDENT# '\n        Execute cmd line in sub-shell\n        ' #NEWLINE# all_output = [ ] #NEWLINE# cmd_parts = [ ] #NEWLINE# for part in cmd : #NEWLINE# #INDENT# if ( len ( part ) > 45 ) : #NEWLINE# #INDENT# part = ( ( part [ : 20 ] + '...' ) + part [ ( - 20 ) : ] ) #NEWLINE# #UNINDENT# if ( ( ' ' in part ) or ( '\n' in part ) or ( '"' in part ) or ( "'" in part ) ) : #NEWLINE# #INDENT# part = ( '"%s"' % part . replace ( '"' , '\\"' ) ) #NEWLINE# #UNINDENT# cmd_parts . append ( part ) #NEWLINE# #UNINDENT# cmd_desc = ' ' . join ( cmd_parts ) #NEWLINE# logger . debug ( ( ' ** Running command %s' % cmd_desc ) ) #NEWLINE# stdout = subprocess . PIPE #NEWLINE# if extra_env : #NEWLINE# #INDENT# env = os . environ . copy ( ) #NEWLINE# if extra_env : #NEWLINE# #INDENT# env . update ( extra_env ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# env = None #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# proc = subprocess . Popen ( [ ' ' . join ( cmd ) ] , stderr = subprocess . STDOUT , stdin = None , stdout = stdout , cwd = cwd , env = env , shell = True ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# e = sys . exc_info ( ) [ 1 ] #NEWLINE# logger . error ( ( 'Error %s while executing command %s' % ( e , cmd_desc ) ) ) #NEWLINE# raise #NEWLINE# #UNINDENT# stdout = proc . stdout #NEWLINE# while stdout : #NEWLINE# #INDENT# line = stdout . readline ( ) #NEWLINE# if ( not line ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# line = line . rstrip ( ) #NEWLINE# all_output . append ( line ) #NEWLINE# logger . info ( line ) #NEWLINE# #UNINDENT# proc . wait ( ) #NEWLINE# if proc . returncode : #NEWLINE# #INDENT# for s in all_output : #NEWLINE# #INDENT# logger . critical ( s ) #NEWLINE# #UNINDENT# raise OSError ( ( 'Command %s failed with error code %s' % ( cmd_desc , proc . returncode ) ) ) #NEWLINE# #UNINDENT# return ( proc . returncode , all_output )