/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
1
# Copyright (C) 2009 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
18
"""Handling and reporting crashes.
19
"""
20
4584.3.17 by Martin Pool
Better message in apport crash
21
# for interactive testing, try the 'bzr assert-fail' command 
22
# or see http://code.launchpad.net/~mbp/bzr/bzr-fail
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
23
24
import os
4584.3.13 by Martin Pool
Refactor _format_plugin_list and include list of loaded modules in apport
25
import pprint
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
26
import sys
27
import time
28
29
import bzrlib
30
from bzrlib import (
31
    config,
4584.3.16 by Martin Pool
Add -Dno_apport and fallback if apport fails
32
    debug,
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
33
    osutils,
34
    plugin,
35
    trace,
36
    )
37
38
39
def report_bug(exc_info, stderr):
4584.3.16 by Martin Pool
Add -Dno_apport and fallback if apport fails
40
    if 'no_apport' not in debug.debug_flags:
41
        try:
42
            report_bug_to_apport(exc_info, stderr)
43
            return
44
        except Exception, e:
4584.3.21 by Martin Pool
Start adding tests for apport
45
            # this should only happen if apport is installed but it didn't
46
            # work, eg because of an io error writing the crash file
4584.3.16 by Martin Pool
Add -Dno_apport and fallback if apport fails
47
            sys.stderr.write("failed to report crash using apport: %r"  % e)
48
            pass
49
    report_bug_legacy(exc_info, stderr)
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
50
51
52
def report_bug_legacy(exc_info, err_file):
53
    """Report a bug by just printing a message to the user."""
54
    trace.print_exception(exc_info, err_file)
55
    err_file.write('\n')
56
    err_file.write('bzr %s on python %s (%s)\n' % \
57
                       (bzrlib.__version__,
58
                        bzrlib._format_version_tuple(sys.version_info),
59
                        sys.platform))
60
    err_file.write('arguments: %r\n' % sys.argv)
61
    err_file.write(
62
        'encoding: %r, fsenc: %r, lang: %r\n' % (
63
            osutils.get_user_encoding(), sys.getfilesystemencoding(),
64
            os.environ.get('LANG')))
65
    err_file.write("plugins:\n")
66
    for name, a_plugin in sorted(plugin.plugins().items()):
67
        err_file.write("  %-20s %s [%s]\n" %
68
            (name, a_plugin.path(), a_plugin.__version__))
69
    err_file.write(
4584.3.19 by Martin Pool
Tweak crash message and use the same one with apport or without.
70
        "*** Bazaar has encountered an internal error.  This probably indicates a\n"
71
        "*** bug in Bazaar.  You can help us fix it by filing a bug report at\n"
72
        "***     https://bugs.launchpad.net/bzr/+filebug\n"
73
        "*** including this traceback and a description of the problem.\n"
74
        )
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
75
76
77
def report_bug_to_apport(exc_info, stderr):
78
    """Report a bug to apport for optional automatic filing.
79
    
80
    :returns: True if the bug was filed or otherwise handled; 
81
        False to use a fallback method.
82
    """
83
    # this is based on apport_package_hook.py, but omitting some of the
84
    # Ubuntu-specific policy about what to report and when
85
    try:
4584.3.8 by Martin Pool
Remove code testing for unavailability of apport
86
        from apport.report import Report
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
87
    except ImportError, e:
88
        trace.warning("couldn't find apport bug-reporting library: %s" % e)
89
        return False
90
4584.3.21 by Martin Pool
Start adding tests for apport
91
    crash_file = _open_crash_file()
92
    try:
93
        _write_apport_report_to_file(crash_file)
94
    finally:
95
        crash_file.close()
96
97
    stderr.write("bzr: ERROR: %s.%s: %s\n" 
98
        "\n"
99
        "*** Bazaar has encountered an internal error.  This probably indicates a\n"
100
        "*** bug in Bazaar.  You can help us fix it by filing a bug report at\n"
101
        "***     https://bugs.launchpad.net/bzr/+filebug\n"
102
        "*** attaching the crash file\n"
103
        "***     %s\n"
104
        "*** and including a description of the problem.\n"
105
        % (exc_info[0].__module__, exc_info[0].__name__, exc_info[1],
106
           crash_file.name))
107
    return True
108
109
110
def _write_apport_report_to_file(exc_info, crash_file):
111
    import platform
112
    from apport.report import Report
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
113
    pr = Report()
114
    # add_proc_info gives you the memory map of the process: this seems rarely
115
    # useful for Bazaar and it does make the report harder to scan, though it
116
    # does tell you what binary modules are loaded.
117
    # pr.add_proc_info()
118
    pr.add_user_info()
119
    pr['BzrVersion'] = bzrlib.__version__
120
    pr['PythonVersion'] = bzrlib._format_version_tuple(sys.version_info)
121
    pr['Platform'] = platform.platform(aliased=1)
122
    pr['UserEncoding'] = osutils.get_user_encoding()
123
    pr['FileSystemEncoding'] = sys.getfilesystemencoding()
124
    pr['Locale'] = os.environ.get('LANG')
4584.3.13 by Martin Pool
Refactor _format_plugin_list and include list of loaded modules in apport
125
    pr['BzrPlugins'] = _format_plugin_list()
126
    pr['PythonLoadedModules'] = _format_module_list()
4584.3.21 by Martin Pool
Start adding tests for apport
127
    pr.write(crash_file)
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
128
129
130
def _open_crash_file():
131
    crash_dir = config.crash_dir()
132
    # user-readable only, just in case the contents are sensitive.
133
    if not osutils.isdir(crash_dir):
134
        os.makedirs(crash_dir, mode=0700)
135
    filename = 'bzr-%s-%s.crash' % (
136
        osutils.compact_date(time.time()),
137
        os.getpid(),)
138
    return open(osutils.pathjoin(crash_dir, filename), 'wt')
4584.3.13 by Martin Pool
Refactor _format_plugin_list and include list of loaded modules in apport
139
140
141
def _format_plugin_list():
142
    plugin_lines = []
143
    for name, a_plugin in sorted(plugin.plugins().items()):
144
        plugin_lines.append("  %-20s %s [%s]" %
145
            (name, a_plugin.path(), a_plugin.__version__))
146
    return '\n'.join(plugin_lines)
147
148
149
def _format_module_list():
150
    return pprint.pformat(sys.modules)