~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/crash.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-03-25 00:02:51 UTC
  • mfrom: (5106.1.1 version-bump)
  • Revision ID: pqm@pqm.ubuntu.com-20100325000251-bwsv5c5d3l9x3lnn
(Jelmer) Bump API version for 2.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009-2011 Canonical Ltd
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
 
17
18
"""Handling and reporting crashes.
18
19
 
19
20
A crash is an exception propagated up almost to the top level of Bazaar.
20
21
 
21
22
If we have apport <https://launchpad.net/apport/>, we store a report of the
22
 
crash using apport into its /var/crash spool directory, from where the user
 
23
crash using apport into it's /var/crash spool directory, from where the user
23
24
can either manually send it to Launchpad.  In some cases (at least Ubuntu
24
25
development releases), Apport may pop up a window asking if they want
25
26
to send it.
32
33
In principle apport can run on any platform though as of Feb 2010 there seem
33
34
to be some portability bugs.
34
35
 
35
 
To force this off in bzr turn set APPORT_DISABLE in the environment or 
 
36
To force this off in bzr turn set APPORT_DISBLE in the environment or 
36
37
-Dno_apport.
37
38
"""
38
39
 
39
 
from __future__ import absolute_import
40
 
 
41
40
# for interactive testing, try the 'bzr assert-fail' command 
42
41
# or see http://code.launchpad.net/~mbp/bzr/bzr-fail
43
42
#
71
70
            return
72
71
    except ImportError, e:
73
72
        trace.mutter("couldn't find apport bug-reporting library: %s" % e)
 
73
        pass
74
74
    except Exception, e:
75
75
        # this should only happen if apport is installed but it didn't
76
76
        # work, eg because of an io error writing the crash file
77
 
        trace.mutter("bzr: failed to report crash using apport: %r" % e)
78
 
        trace.log_exception_quietly()
 
77
        stderr.write("bzr: failed to report crash using apport:\n "
 
78
            "    %r\n" % e)
 
79
        pass
79
80
    return report_bug_legacy(exc_info, stderr)
80
81
 
81
82
 
83
84
    """Report a bug by just printing a message to the user."""
84
85
    trace.print_exception(exc_info, err_file)
85
86
    err_file.write('\n')
86
 
    import textwrap
87
 
    def print_wrapped(l):
88
 
        err_file.write(textwrap.fill(l,
89
 
            width=78, subsequent_indent='    ') + '\n')
90
 
    print_wrapped('bzr %s on python %s (%s)\n' % \
91
 
        (bzrlib.__version__,
92
 
        bzrlib._format_version_tuple(sys.version_info),
93
 
        platform.platform(aliased=1)))
94
 
    print_wrapped('arguments: %r\n' % sys.argv)
95
 
    print_wrapped(textwrap.fill(
96
 
        'plugins: ' + plugin.format_concise_plugin_list(),
97
 
        width=78,
98
 
        subsequent_indent='    ',
99
 
        ) + '\n')
100
 
    print_wrapped(
 
87
    err_file.write('bzr %s on python %s (%s)\n' % \
 
88
                       (bzrlib.__version__,
 
89
                        bzrlib._format_version_tuple(sys.version_info),
 
90
                        platform.platform(aliased=1)))
 
91
    err_file.write('arguments: %r\n' % sys.argv)
 
92
    err_file.write(
101
93
        'encoding: %r, fsenc: %r, lang: %r\n' % (
102
94
            osutils.get_user_encoding(), sys.getfilesystemencoding(),
103
95
            os.environ.get('LANG')))
104
 
    # We used to show all the plugins here, but it's too verbose.
 
96
    err_file.write("plugins:\n")
 
97
    err_file.write(_format_plugin_list())
105
98
    err_file.write(
106
 
        "\n"
 
99
        "\n\n"
107
100
        "*** Bazaar has encountered an internal error.  This probably indicates a\n"
108
101
        "    bug in Bazaar.  You can help us fix it by filing a bug report at\n"
109
102
        "        https://bugs.launchpad.net/bzr/+filebug\n"
119
112
    # this function is based on apport_package_hook.py, but omitting some of the
120
113
    # Ubuntu-specific policy about what to report and when
121
114
 
122
 
    # This import is apparently not used, but we're doing it so that if the
123
 
    # import fails, the exception will be caught at a higher level and we'll
124
 
    # report the error by other means.
 
115
    # if the import fails, the exception will be caught at a higher level and
 
116
    # we'll report the error by other means
125
117
    import apport
126
118
 
127
119
    crash_filename = _write_apport_report_to_file(exc_info)
151
143
    exc_type, exc_object, exc_tb = exc_info
152
144
 
153
145
    pr = Report()
154
 
    # add_proc_info sets the ExecutablePath, InterpreterPath, etc.
 
146
    # add_proc_info gets the executable and interpreter path, which is needed,
 
147
    # plus some less useful stuff like the memory map
155
148
    pr.add_proc_info()
156
 
    # It also adds ProcMaps which for us is rarely useful and mostly noise, so
157
 
    # let's remove it.
158
 
    del pr['ProcMaps']
159
149
    pr.add_user_info()
160
150
 
161
151
    # Package and SourcePackage are needed so that apport will report about even
170
160
    pr['Platform'] = platform.platform(aliased=1)
171
161
    pr['UserEncoding'] = osutils.get_user_encoding()
172
162
    pr['FileSystemEncoding'] = sys.getfilesystemencoding()
173
 
    pr['Locale'] = os.environ.get('LANG', 'C')
 
163
    pr['Locale'] = os.environ.get('LANG')
174
164
    pr['BzrPlugins'] = _format_plugin_list()
175
165
    pr['PythonLoadedModules'] = _format_module_list()
176
166
    pr['BzrDebugFlags'] = pprint.pformat(debug.debug_flags)
177
167
 
178
 
    # actually we'd rather file directly against the upstream product, but
179
 
    # apport does seem to count on there being one in there; we might need to
180
 
    # redirect it elsewhere anyhow
181
 
    pr['SourcePackage'] = 'bzr'
182
 
    pr['Package'] = 'bzr'
183
 
 
184
 
    # tell apport to file directly against the bzr package using 
185
 
    # <https://bugs.launchpad.net/bzr/+bug/391015>
186
 
    #
187
 
    # XXX: unfortunately apport may crash later if the crashdb definition
188
 
    # file isn't present
189
 
    pr['CrashDb'] = 'bzr'
190
 
 
191
168
    tb_file = StringIO()
192
169
    traceback.print_exception(exc_type, exc_object, exc_tb, file=tb_file)
193
170
    pr['Traceback'] = tb_file.getvalue()
259
236
        os.open(filename, 
260
237
            os.O_WRONLY|os.O_CREAT|os.O_EXCL,
261
238
            0600),
262
 
        'wb')
 
239
        'w')
263
240
 
264
241
 
265
242
def _format_plugin_list():
266
 
    return ''.join(plugin.describe_plugins(show_paths=True))
 
243
    plugin_lines = []
 
244
    for name, a_plugin in sorted(plugin.plugins().items()):
 
245
        plugin_lines.append("  %-20s %s [%s]" %
 
246
            (name, a_plugin.path(), a_plugin.__version__))
 
247
    return '\n'.join(plugin_lines)
267
248
 
268
249
 
269
250
def _format_module_list():