def run(cmd, *params, **kwargs): fatal = kwargs.get('fatal', True) direct = kwargs.get('direct', False) log_cmd = kwargs.get('log_cmd', True) log_fail = kwargs.get('log_fail', True) input = kwargs.get('input') print_stdout = kwargs.get('print_stdout', False) print_stderr = kwargs.get('print_stderr', False) print_output = kwargs.get('print_output', False) cmd = [cmd] + list(params) cmd_str = ' '.join(cmd) if log_cmd: log.command(log.term.cmd(cmd_str)) if print_output: print_stdout = True print_stderr = True if input: stdin = subprocess.PIPE else: stdin = None if direct: stdout = None stderr = None else: stdout = subprocess.PIPE stderr = subprocess.PIPE prc = subprocess.Popen(cmd, stdin=stdin, stdout=stdout, stderr=stdout) out, err = prc.communicate(input=input) if out: out = out.rstrip() if print_stdout: log.info(out) else: out = '' if err: err = err.rstrip() if print_stderr: log.info(err) else: err = '' cout = _CommandOutput(out) cout.stderr = err cout.return_code = prc.returncode cout.cmd = cmd_str if prc.returncode != 0: if log_fail: log_cmd_fail(cmd_str, cout) if fatal: raise exception.CommandFailed(cmd=cmd, out=cout) return cout
def __call(self, command, check_result=True, print_out=False): """Util function for calling commands and printing them nicely""" log.command(command) if not int(os.environ["WARM_VERBOSE_LOGGING"]) and not print_out: if platform == "win32": command = command + " > $null" else: command = command + " &> /dev/null" if check_result: return check_output(command, shell=True).decode('utf-8') else: return call(command, stdout=subprocess.DEVNULL, shell=True)
def run(cmd, *params, **kwargs): fatal = kwargs.get("fatal", True) direct = kwargs.get("direct", False) log_cmd = kwargs.get("log_cmd", True) log_fail = kwargs.get("log_fail", True) input = kwargs.get("input") print_stdout = kwargs.get("print_stdout", False) print_stderr = kwargs.get("print_stderr", False) print_output = kwargs.get("print_output", False) cmd = [cmd] + list(params) cmd_str = " ".join(cmd) if log_cmd: log.command(log.term.cmd(cmd_str)) if print_output: print_stdout = True print_stderr = True if input: stdin = subprocess.PIPE else: stdin = None if direct: stdout = None stderr = None else: stdout = subprocess.PIPE stderr = subprocess.PIPE try: prc = subprocess.Popen(cmd, stdin=stdin, stdout=stdout, stderr=stdout) except OSError as ex: raise exception.CommandNotFound(cmd=cmd[0]) out, err = prc.communicate(input=input) if out: out = out.rstrip() if print_stdout: log.info(out) else: out = "" if err: err = err.rstrip() if print_stderr: log.info(err) else: err = "" cout = _CommandOutput(out) cout.stderr = err cout.return_code = prc.returncode cout.cmd = cmd_str if prc.returncode != 0: if log_fail: log_cmd_fail(cmd_str, cout) if fatal: raise exception.CommandFailed(cmd=cmd, out=cout) return cout