def check_call( cls, command, verbose=False, timeout=None, error_info=None, expected=None, raise_on_err=True, **kwargs): """Execute command and check for return code Timeout limitation: read tick is 100 ms. :type command: str :type verbose: bool :type timeout: int :type error_info: str :type expected: list :type raise_on_err: bool :rtype: ExecResult :raises: DevopsCalledProcessError """ if expected is None: expected = [proc_enums.ExitCodes.EX_OK] else: expected = [ proc_enums.ExitCodes(code) if ( isinstance(code, int) and code in proc_enums.ExitCodes.__members__.values()) else code for code in expected ] ret = cls.execute(command, verbose, timeout, **kwargs) if ret['exit_code'] not in expected: message = ( "{append}Command '{cmd!r}' returned exit code {code!s} while " "expected {expected!s}\n" "\tSTDOUT:\n" "{stdout}" "\n\tSTDERR:\n" "{stderr}".format( append=error_info + '\n' if error_info else '', cmd=command, code=ret['exit_code'], expected=expected, stdout=ret['stdout_str'], stderr=ret['stderr_str'] )) logger.error(message) if raise_on_err: raise error.DevopsCalledProcessError( command, ret['exit_code'], expected=expected, stdout=ret['stdout_str'], stderr=ret['stderr_str']) return ret
def exit_code(self, new_val): """Return(exit) code of command :type new_val: int """ if not isinstance(new_val, (int, proc_enums.ExitCodes)): raise TypeError('Exit code is strictly int') with self.lock: if isinstance(new_val, int) and \ new_val in proc_enums.ExitCodes.__members__.values(): new_val = proc_enums.ExitCodes(new_val) self.__exit_code = new_val
def check_call(cls, command, verbose=False, timeout=None, error_info=None, expected=None, raise_on_err=True, **kwargs): """Execute command and check for return code Timeout limitation: read tick is 100 ms. :type command: str :type verbose: bool :type timeout: int :type error_info: str :type expected: list :type raise_on_err: bool :rtype: ExecResult :raises: DevopsCalledProcessError """ if expected is None: expected = [proc_enums.ExitCodes.EX_OK] else: expected = [ proc_enums.ExitCodes(code) if (isinstance(code, int) and code in proc_enums.ExitCodes.__members__.values()) else code for code in expected ] ret = cls.execute(command, verbose, timeout, **kwargs) if ret['exit_code'] not in expected: message = (log_templates.CMD_UNEXPECTED_EXIT_CODE.format( append=error_info + '\n' if error_info else '', cmd=command, code=ret['exit_code'], expected=expected)) logger.error(message) if raise_on_err: raise error.DevopsCalledProcessError( command, ret['exit_code'], expected=expected, stdout=ret['stdout_brief'], stderr=ret['stderr_brief']) return ret