def check_subprocesses_to_logfile(what, logfile, cmds, append=False, **kwargs): """Wrapper around check_subprocess that logs output to a logfile. Args: what: [string] For logging purposes, what is the command for. logfile: [path] The logfile to write to. cmds: [list of string] A list of commands to run. append: [boolean] Open the log file as append if true, write new default. kwargs: [kwargs] Additional keyword arguments to pass to check_subprocess. """ mode = 'a' if append else 'w' how = 'Appending' if append else 'Logging' logging.info('%s %s to %s', how, what, logfile) ensure_dir_exists(os.path.dirname(logfile)) with open(logfile, mode) as stream: try: check_subprocess_sequence(cmds, stream=stream, embed_errors=False, **kwargs) except Exception: logging.error('%s failed. Log file [%s] follows:', what, logfile) with open(logfile, 'r') as readagain: output = readagain.read() log_embedded_output(logging.ERROR, logfile, output) logging.error('%s failed. See embedded logfile above', what) ensure_dir_exists(ERROR_LOGFILE_DIR) error_path = os.path.join('errors', os.path.basename(logfile)) logging.info('Copying error log file to %s', error_path) with open(error_path, 'w') as f: f.write(output) raise
def check_commit_or_no_changes(self, git_dir, commit_commandline_args): """A variant of check_run 'commit' that tolerates 'no changes' errors.""" retcode, stdout = self.run_git(git_dir, "commit " + commit_commandline_args) if retcode == 1: last_line = stdout.split("\n")[-1] if last_line.lower().find("nothing to commit") >= 0: logging.debug("No changes to commit -- raw changelog is unchanged.") return stdout log_embedded_output(logging.ERROR, "command output", stdout) raise_and_log_error(ExecutionError("git failed.")) return stdout
def check_commit_or_no_changes(self, git_dir, commit_commandline_args): """A variant of check_run 'commit' that tolerates 'no changes' errors.""" retcode, stdout = self.run_git( git_dir, 'commit ' + commit_commandline_args) if retcode == 1: last_line = stdout.split('\n')[-1] if last_line.lower().find('nothing to commit') >= 0: logging.debug('No changes to commit -- raw changelog is unchanged.') return stdout log_embedded_output(logging.ERROR, 'command output', stdout) raise_and_log_error(ExecutionError('git failed.')) return stdout
def check_subprocess(cmd, stream=None, **kwargs): """Run_subprocess and raise CalledProcessError if it fails.""" embed_errors = kwargs.pop('embed_errors', True) retcode, stdout = run_subprocess(cmd, stream=stream, **kwargs) if retcode == 0: return stdout.strip() if embed_errors: log_embedded_output(logging.ERROR, 'command output', stdout) logging.error('Command failed. See embedded output above.') else: lines = stdout.split('\n') if lines > 10: lines = lines[-10:] log_embedded_output(logging.ERROR, 'Command failed with last %d lines' % len(lines), '\n'.join(lines)) program = os.path.basename(shlex.split(cmd)[0]) raise_and_log_error(ExecutionError(program + ' failed.', program=program))
def check_subprocesses_to_logfile(what, logfile, cmds, append=False, **kwargs): """Wrapper around check_subprocess that logs output to a logfile. Args: what: [string] For logging purposes, what is the command for. logfile: [path] The logfile to write to. cmds: [list of string] A list of commands to run. append: [boolean] Open the log file as append if true, write new default. kwargs: [kwargs] Additional keyword arguments to pass to check_subprocess. """ mode = "a" if append else "w" how = "Appending" if append else "Logging" logging.info("%s %s to %s", how, what, logfile) ensure_dir_exists(os.path.dirname(logfile)) with io.open(logfile, mode, encoding="utf-8") as stream: try: check_subprocess_sequence(cmds, stream=stream, embed_errors=False, **kwargs) except Exception as ex: logging.error("%s failed. Log file [%s] follows:", what, logfile) import traceback traceback.print_exc() with io.open(logfile, "r", encoding="utf-8") as readagain: output = readagain.read() log_embedded_output(logging.ERROR, logfile, output) logging.error( "Caught exception %s\n%s failed. See embedded logfile above", ex, what ) ensure_dir_exists(ERROR_LOGFILE_DIR) error_path = os.path.join("errors", os.path.basename(logfile)) logging.info("Copying error log file to %s", error_path) with io.open(error_path, "w", encoding="utf-8") as f: f.write(output) f.write("\n--------\n") f.write("Exeception caught in parent process:\n%s" % ex) raise
def check_subprocess(cmd, stream=None, **kwargs): """Run_subprocess and raise CalledProcessError if it fails.""" # pylint: disable=inconsistent-return-statements embed_errors = kwargs.pop("embed_errors", True) retcode, stdout = run_subprocess(cmd, stream=stream, **kwargs) if retcode == 0: return stdout.strip() if embed_errors: log_embedded_output(logging.ERROR, "command output", stdout) logging.error("Command failed. See embedded output above.") else: lines = stdout.split("\n") if len(lines) > 30: lines = lines[-30:] log_embedded_output( logging.ERROR, "Command failed with last %d lines" % len(lines), "\n".join(lines), ) program = os.path.basename(shlex.split(cmd)[0]) raise_and_log_error(ExecutionError(program + " failed.", program=program))