#NEWLINE# def call_subprocess( cmd , show_stdout = True , filter_stdout = None , cwd = None , raise_on_returncode = True , command_level = logger . DEBUG , command_desc = None , extra_environ = None ) : #NEWLINE# #INDENT# if ( command_desc is None ) : #NEWLINE# #INDENT# cmd_parts = [ ] #NEWLINE# for part in cmd : #NEWLINE# #INDENT# 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# command_desc = ' ' . join ( cmd_parts ) #NEWLINE# #UNINDENT# if show_stdout : #NEWLINE# #INDENT# stdout = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# stdout = subprocess . PIPE #NEWLINE# #UNINDENT# logger . log ( command_level , ( 'Running command %s' % command_desc ) ) #NEWLINE# env = os . environ . copy ( ) #NEWLINE# if extra_environ : #NEWLINE# #INDENT# env . update ( extra_environ ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# proc = subprocess . Popen ( cmd , stderr = subprocess . STDOUT , stdin = None , stdout = stdout , cwd = cwd , env = env ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# e = sys . exc_info ( ) [ 1 ] #NEWLINE# logger . fatal ( ( 'Error %s while executing command %s' % ( e , command_desc ) ) ) #NEWLINE# raise #NEWLINE# #UNINDENT# all_output = [ ] #NEWLINE# if ( stdout is not None ) : #NEWLINE# #INDENT# stdout = proc . stdout #NEWLINE# while 1 : #NEWLINE# #INDENT# line = console_to_str ( stdout . readline ( ) ) #NEWLINE# if ( not line ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# line = line . rstrip ( ) #NEWLINE# all_output . append ( ( line + '\n' ) ) #NEWLINE# if filter_stdout : #NEWLINE# #INDENT# level = filter_stdout ( line ) #NEWLINE# if isinstance ( level , tuple ) : #NEWLINE# #INDENT# ( level , line ) = level #NEWLINE# #UNINDENT# logger . log ( level , line ) #NEWLINE# if ( not logger . stdout_level_matches ( level ) ) : #NEWLINE# #INDENT# logger . show_progress ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# logger . info ( line ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# ( returned_stdout , returned_stderr ) = proc . communicate ( ) #NEWLINE# all_output = [ ( returned_stdout or '' ) ] #NEWLINE# #UNINDENT# proc . wait ( ) #NEWLINE# if proc . returncode : #NEWLINE# #INDENT# if raise_on_returncode : #NEWLINE# #INDENT# if all_output : #NEWLINE# #INDENT# logger . notify ( ( 'Complete output from command %s:' % command_desc ) ) #NEWLINE# logger . notify ( ( '\n' . join ( all_output ) + '\n----------------------------------------' ) ) #NEWLINE# #UNINDENT# raise InstallationError ( ( 'Command %s failed with error code %s in %s' % ( command_desc , proc . returncode , cwd ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logger . warn ( ( 'Command %s had error code %s in %s' % ( command_desc , proc . returncode , cwd ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( stdout is not None ) : #NEWLINE# #INDENT# return '' . join ( all_output )