def log_and_run(command, raise_exception_on_failure=True): def log_output(results): if results is None: logger.info(" # <no output>") lines = results.split("\n") for line in lines: logger.info(" # %s", line.rstrip()) try: logger.info("[%s] %s", datetime.datetime.now(), command) try: results = check_output(command, shell=True, stderr=STDOUT).rstrip() finally: # Restore terminal settings in case the command we ran manipulated them. Note: # under concurrent access, this is still not a perfect solution since another thread's # child process may break the settings again before we log below. termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, _term_settings) log_output(results) return results except CalledProcessError as e: # Wrap the original exception with one that gives a better error # message (including command output). logger.info(" # Return code: %s", e.returncode) log_output(e.output) if raise_exception_on_failure: raise CommandExecError(e)
def log_and_run(command): try: logger.info(command) return check_output(command, shell=True, stderr=STDOUT).rstrip() except CalledProcessError as e: # Wrap the original exception with one that gives a better error # message (including command output). raise CommandExecError(e)
def log_and_run(command): try: logger.info(command) results = check_output(command, shell=True, stderr=STDOUT).rstrip() lines = results.split("\n") for line in lines: logger.info(" # %s", line) return results except CalledProcessError as e: # Wrap the original exception with one that gives a better error # message (including command output). logger.info(" # Return code: %s", e.returncode) raise CommandExecError(e)