def execute(*cmd, **kwargs):
 process_input = kwargs.pop('process_input', None)
 check_exit_code = kwargs.pop('check_exit_code', [0])
 ignore_exit_code = False
 if isinstance(check_exit_code, bool):
  ignore_exit_code = (not check_exit_code)
  check_exit_code = [0]
 elif isinstance(check_exit_code, int):
  check_exit_code = [check_exit_code]
 delay_on_retry = kwargs.pop('delay_on_retry', True)
 attempts = kwargs.pop('attempts', 1)
 run_as_root = kwargs.pop('run_as_root', False)
 shell = kwargs.pop('shell', False)
 if len(kwargs):
  raise exception.NovaException((_('Got unknown keyword args to utils.execute: %r') % kwargs))
 if (run_as_root and (os.geteuid() != 0)):
  cmd = (['sudo', 'nova-rootwrap', CONF.rootwrap_config] + list(cmd))
 cmd = map(str, cmd)
 while (attempts > 0):
  attempts -= 1
  try:
   LOG.debug(_('Running cmd (subprocess): %s'), ' '.join(cmd))
   _PIPE = subprocess.PIPE
   if (os.name == 'nt'):
    preexec_fn = None
    close_fds = False
   else:
    preexec_fn = _subprocess_setup
    close_fds = True
   obj = subprocess.Popen(cmd, stdin=_PIPE, stdout=_PIPE, stderr=_PIPE, close_fds=close_fds, preexec_fn=preexec_fn, shell=shell)
   result = None
   if (process_input is not None):
    result = obj.communicate(process_input)
   else:
    result = obj.communicate()
   obj.stdin.close()
   _returncode = obj.returncode
   LOG.debug((_('Result was %s') % _returncode))
   if ((not ignore_exit_code) and (_returncode not in check_exit_code)):
    (stdout, stderr) = result
    raise exception.ProcessExecutionError(exit_code=_returncode, stdout=stdout, stderr=stderr, cmd=' '.join(cmd))
   return result
  except exception.ProcessExecutionError:
   if (not attempts):
    raise
   else:
    LOG.debug(_('%r failed. Retrying.'), cmd)
    if delay_on_retry:
     greenthread.sleep((random.randint(20, 200) / 100.0))
  finally:
   greenthread.sleep(0)

def execute ( * cmd , ** kwargs ) : process_input = kwargs . pop ( 'process_input' , None ) check_exit_code = kwargs . pop ( 'check_exit_code' , [ 0 ] ) ignore_exit_code = False if isinstance ( check_exit_code , bool ) : ignore_exit_code = ( not check_exit_code ) check_exit_code = [ 0 ] elif isinstance ( check_exit_code , int ) : check_exit_code = [ check_exit_code ] delay_on_retry = kwargs . pop ( 'delay_on_retry' , True ) attempts = kwargs . pop ( 'attempts' , 1 ) run_as_root = kwargs . pop ( 'run_as_root' , False ) shell = kwargs . pop ( 'shell' , False ) if len ( kwargs ) : raise exception . NovaException ( ( _ ( 'Got unknown keyword args to utils.execute: %r' ) % kwargs ) ) if ( run_as_root and ( os . geteuid ( ) != 0 ) ) : cmd = ( [ 'sudo' , 'nova-rootwrap' , CONF . rootwrap_config ] + list ( cmd ) ) cmd = map ( str , cmd ) while ( attempts > 0 ) : attempts -= 1 try : LOG . debug ( _ ( 'Running cmd (subprocess): %s' ) , ' ' . join ( cmd ) ) _PIPE = subprocess . PIPE if ( os . name == 'nt' ) : preexec_fn = None close_fds = False else : preexec_fn = _subprocess_setup close_fds = True obj = subprocess . Popen ( cmd , stdin = _PIPE , stdout = _PIPE , stderr = _PIPE , close_fds = close_fds , preexec_fn = preexec_fn , shell = shell ) result = None if ( process_input is not None ) : result = obj . communicate ( process_input ) else : result = obj . communicate ( ) obj . stdin . close ( ) _returncode = obj . returncode LOG . debug ( ( _ ( 'Result was %s' ) % _returncode ) ) if ( ( not ignore_exit_code ) and ( _returncode not in check_exit_code ) ) : ( stdout , stderr ) = result raise exception . ProcessExecutionError ( exit_code = _returncode , stdout = stdout , stderr = stderr , cmd = ' ' . join ( cmd ) ) return result except exception . ProcessExecutionError : if ( not attempts ) : raise else : LOG . debug ( _ ( '%r failed. Retrying.' ) , cmd ) if delay_on_retry : greenthread . sleep ( ( random . randint ( 20 , 200 ) / 100.0 ) ) finally : greenthread . sleep ( 0 )
create a l{deferred} from a callable and arguments .