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 __HOLE__:
                pass
            else:
                LOG.debug('Last lines from %s:\n%s', file_name, content)
dataset/ETHPy150Open openstack/anvil/anvil/shell.py/execute_save_output