def wait(self): """ Wait for asynchronous subprocess to exit Args: None Returns: list of result strings Raises: CommandStateError when no subprocess is active """ #-- if (self.proc is None): raise CommandStateError() try: self.proc.wait() retcode = self.proc.returncode msgs = self._format_result(retcode, None, None) del self.proc self.proc = None return msgs except Exception as e: raise CommandError(ExceptionHandler.format_exception(e))
def async (self, cmd): """ Execute subprocess asynchronously Args: cmd (string): shell command to execute Returns: None Raises: see sync method above. Notes: see sync method above. """ #-- if (isinstance(cmd, str)): shell_mode = True elif (isinstance(cmd, (list, tuple))): shell_mode = False else: raise CommandParameterError(self.name, 'cmd', cmd) try: self.proc = subprocess.Popen(cmd, shell=shell_mode, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except Exception as e: raise CommandError(ExceptionHandler.format_exception(e))
def sync(self, cmd): """ Execute subprocess synchronously Args: cmd (string): shell command to execute cmd (list): command arguments to pass Returns: A list of strings: ['retcode' : 0, 'output' : '...', 'errors' : '...' ] Raises: CommandParameterError when command is not a string type Notes: Shell command is a string like "cd tmp && ls". Command arguments is a list like ['gedit', 'file1', file2',...] Output and errors strings are only returned to the caller when the subprocess returns output or errors. """ #-- if (ut.is_string_type(cmd)): shellmode = True elif (isinstance(cmd, (list, tuple))): shellmode = False else: raise CommandParameterError('cmd', cmd) try: self.proc = subprocess.Popen(cmd, shell=shellmode, stdout=subprocess.PIPE, stderr=subprocess.PIPE) bstdout, bstderr = self.proc.communicate() retcode = self.proc.returncode self.proc.stdout.close() self.proc.stderr.close() self.proc = None return self._format_result(retcode, bstdout, bstderr) except Exception as e: raise CommandError(ExceptionHandler.format_exception(e))
def sync(self, cmd): """ Execute subprocess synchronously Args: cmd (string): shell command to execute cmd (list): command arguments to pass Returns: A list of strings: ['retcode' : 0, 'output' : '...', 'errors' : '...' ] Raises: CommandParameterError when command is not a string type Notes: Shell command is a string like "cd tmp && ls". Command arguments is a list like ['gedit', 'file1', file2',...] Output and errors strings are only returned to the caller when the subprocess returns output or errors. """ #-- if (ut.is_string_type(cmd)): shellmode = True elif (isinstance(cmd, (list,tuple))): shellmode = False else: raise CommandParameterError('cmd', cmd) try: self.proc = subprocess.Popen(cmd, shell=shellmode, stdout=subprocess.PIPE, stderr=subprocess.PIPE) bstdout, bstderr = self.proc.communicate() retcode = self.proc.returncode self.proc.stdout.close() self.proc.stderr.close() self.proc = None return self._format_result(retcode, bstdout, bstderr) except Exception as e: raise CommandError(ExceptionHandler.format_exception(e))