#NEWLINE# def execute( cmd , process_input = None , check_exit_code = True , cwd = None , shell = False , env_overrides = None , stdout_fh = subprocess . PIPE , stderr_fh = subprocess . PIPE ) : #NEWLINE# #INDENT# "Helper method to execute a command through subprocess.\n\n    :param cmd:             Command passed to subprocess.Popen.\n    :param process_input:   Input send to opened process.\n    :param check_exit_code: Specifies whether to check process return code.\n                            If return code is other then `0` - exception will\n                            be raised.\n    :param cwd:             The child's current directory will be changed to\n                            `cwd` before it is executed.\n    :param shell:           Specifies whether to use the shell as the program\n                            to execute.\n    :param env_overrides:   Process environment parameters to override.\n    :param stdout_fh:       Stdout file handler.\n    :param stderr_fh:       Stderr file handler.\n    :returns:               A tuple, (stdout, stderr) from the spawned process.\n    :raises:                :class:`exceptions.ProcessExecutionError` when\n                            process ends with other then `0` return code.\n    " #NEWLINE# cmd = map ( str , cmd ) #NEWLINE# str_cmd = subprocess . list2cmdline ( cmd ) #NEWLINE# if shell : #NEWLINE# #INDENT# cmd = str_cmd #NEWLINE# LOG . debug ( ( 'Running shell cmd: %r' % cmd ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# LOG . debug ( ( 'Running cmd: %r' % cmd ) ) #NEWLINE# #UNINDENT# if ( process_input is not None ) : #NEWLINE# #INDENT# process_input = str ( process_input ) #NEWLINE# LOG . debug ( ( 'Process input: %s' % process_input ) ) #NEWLINE# #UNINDENT# if cwd : #NEWLINE# #INDENT# LOG . debug ( ( 'Process working directory: %r' % cwd ) ) #NEWLINE# #UNINDENT# process_env = None #NEWLINE# if ( env_overrides and len ( env_overrides ) ) : #NEWLINE# #INDENT# process_env = env . get ( ) #NEWLINE# for ( k , v ) in env_overrides . items ( ) : #NEWLINE# #INDENT# LOG . debug ( "Using environment override '%s' => '%s'" , k , v ) #NEWLINE# process_env [ k ] = str ( v ) #NEWLINE# #UNINDENT# #UNINDENT# exec_kwargs = { #NEWLINE# #INDENT# 'stdin' : subprocess . PIPE , #NEWLINE# 'stdout' : stdout_fh , #NEWLINE# 'stderr' : stderr_fh , #NEWLINE# 'close_fds' : True , #NEWLINE# 'shell' : shell , #NEWLINE# 'cwd' : cwd , #NEWLINE# 'env' : process_env , #NEWLINE# #UNINDENT# } #NEWLINE# result = ( '' , '' ) #NEWLINE# try : #NEWLINE# #INDENT# obj = subprocess . Popen ( cmd , ** exec_kwargs ) #NEWLINE# result = obj . communicate ( env_overrides ) #NEWLINE# #UNINDENT# except OSError as e : #NEWLINE# #INDENT# raise excp . ProcessExecutionError ( str_cmd , exec_kwargs = exec_kwargs , description = ( '%s: [%s, %s]' % ( e , e . errno , e . strerror ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# rc = obj . returncode #NEWLINE# #UNINDENT# stdout = ( result [ 0 ] or '' ) #NEWLINE# stderr = ( result [ 1 ] or '' ) #NEWLINE# if ( ( rc != 0 ) and check_exit_code ) : #NEWLINE# #INDENT# e = excp . ProcessExecutionError ( str_cmd , exec_kwargs = exec_kwargs , stdout = stdout , stderr = stderr , exit_code = rc , where_output = 'debug log' ) #NEWLINE# LOG . debug ( 'Stdout: %s' , e . stdout ) #NEWLINE# LOG . debug ( 'Stderr: %s' , e . stderr ) #NEWLINE# raise e #NEWLINE# #UNINDENT# return ( stdout , stderr )