def _yyoom(self, arglist, on_completed=None): if not on_completed: on_completed = lambda data, errored: None if not sh.isdir(self._logs_dir): sh.mkdirslist(self._logs_dir) with tempfile.NamedTemporaryFile(suffix=".json") as fh: cmdline = [ self.yyoom_executable, "--output-file", fh.name, "--verbose", ] cmdline.extend(arglist) log_filename = sh.joinpths(self._logs_dir, _generate_log_filename(arglist)) LOG.debug("Running yyoom: log output will be placed in %s", log_filename) try: sh.execute_save_output(cmdline, log_filename) except excp.ProcessExecutionError: with excp.reraise(): try: fh.seek(0) data = utils.parse_json(fh.read()) except Exception: LOG.exception("Failed to parse YYOOM output") else: on_completed(data, True) else: fh.seek(0) data = utils.parse_json(fh.read()) on_completed(data, False) return data
def execute_save_output(cmd, file_name, **kwargs): """Helper method to execute a command through subprocess and save stdout and stderr into a file. """ kwargs = kwargs.copy() mkdirslist(dirname(file_name)) try: with open(file_name, 'wb') as fh: return execute(cmd, stdout_fh=fh, stderr_fh=fh, **kwargs) except excp.ProcessExecutionError: with excp.reraise(): try: with open(file_name, 'rb') as fh: lines = collections.deque(fh, maxlen=_TRUNCATED_OUTPUT_LINES) content = "".join(lines) except IOError: pass else: LOG.debug('Last lines from %s:\n%s', file_name, content)
def activate(): try: failure() except Exception: with exc.reraise(): buff.append(1)
def activate(): try: failure() except Exception: with exc.reraise(): raise RuntimeError("Really broken")