Normal
 %£    # Copyright (C) 2009-2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Handling and reporting crashes.

A crash is an exception propagated up almost to the top level of Breezy.

If we have apport <https://launchpad.net/apport/>, we store a report of the
crash using apport into its /var/crash spool directory, from where the user
can either manually send it to Launchpad.  In some cases (at least Ubuntu
development releases), Apport may pop up a window asking if they want
to send it.

Without apport, we just write a crash report to stderr and the user can report
this manually if the wish.

We never send crash data across the network without user opt-in.

In principle apport can run on any platform though as of Feb 2010 there seem
to be some portability bugs.

To force this off in brz turn set APPORT_DISABLE in the environment or
-Dno_apport.
"""

# for interactive testing, try the 'brz assert-fail' command
# or see http://code.launchpad.net/~mbp/bzr/bzr-fail
#
# to test with apport it's useful to set
# export APPORT_IGNORE_OBSOLETE_PACKAGES=1

from io import StringIO
import os
import platform
import pprint
import sys
import time

import breezy
from . import (
    bedding,
    debug,
    osutils,
    plugin,
    trace,
    )


def report_bug(exc_info, stderr):
    if ('no_apport' in debug.debug_flags) or \
            os.environ.get('APPORT_DISABLE', None):
        return report_bug_legacy(exc_info, stderr)
    try:
        if report_bug_to_apport(exc_info, stderr):
            # wrote a file; if None then report the old way
            return
    except ImportError as e:
        trace.mutter("couldn't find apport bug-reporting library: %s" % e)
    except Exception as e:
        # this should only happen if apport is installed but it didn't
        # work, eg because of an io error writing the crash file
        trace.mutter("brz: failed to report crash using apport: %r" % e)
        trace.log_exception_quietly()
    return report_bug_legacy(exc_info, stderr)


def report_bug_legacy(exc_info, err_file):
    """Report a bug by just printing a message to the user."""
    trace.print_exception(exc_info, err_file)
    err_file.write('\n')
    import textwrap

    def print_wrapped(l):
        err_file.write(textwrap.fill(
            l, width=78, subsequent_indent='    ') + '\n')
    print_wrapped('brz %s on python %s (%s)\n' %
                  (breezy.__version__,
                   breezy._format_version_tuple(sys.version_info),
                   platform.platform(aliased=1)))
    print_wrapped('arguments: %r\n' % sys.argv)
    print_wrapped(textwrap.fill(
        'plugins: ' + plugin.format_concise_plugin_list(),
        width=78,
        subsequent_indent='    ',
        ) + '\n')
    print_wrapped(
        'encoding: {!r}, fsenc: {!r}, lang: {!r}\n'.format(
            osutils.get_user_encoding(), sys.getfilesystemencoding(),
            os.environ.get('LANG')))
    # We used to show all the plugins here, but it's too verbose.
    err_file.write(
        "\n"
        "*** Breezy has encountered an internal error.  This probably indicates a\n"
        "    bug in Breezy.  You can help us fix it by filing a bug report at\n"
        "        https://bugs.launchpad.net/brz/+filebug\n"
        "    including this traceback and a description of the problem.\n"
        )


def report_bug_to_apport(exc_info, stderr):
    """Report a bug to apport f