root/trunk/umit @ 4035

Revision 4035, 6.3 kB (checked in by gpolo, 4 years ago)

Separate interpolation from the translatable string

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2005-2006 Insecure.Com LLC.
5# Copyright (C) 2007-2008 Adriano Monteiro Marques
6#
7# Authors: Adriano Monteiro Marques <adriano@umitproject.org>
8#          Cleber Rodrigues <cleber.gnu@gmail.com>
9#          Frederico Silva Ribeiro <ribeiro.fsilva@gmail.com>
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
25import os
26import sys
27import signal
28
29# used by exception hook
30import cgitb
31import tempfile
32
33from umitCore.I18N import _
34from umitCore.Version import VERSION
35#########################
36
37UMIT_DEVELOPMENT = os.environ.get("UMIT_DEVELOPMENT", True)
38
39class UmitExceptionHook(object):
40    def __call__(self, etype, emsg, etb):
41        import warnings
42        warnings.filterwarnings("error", module = "gtk")
43        try:
44            import gtk
45            from umitGUI.BugReport import CrashReport
46            from higwidgets.higdialogs import HIGAlertDialog
47        except Warning, e:
48            print e.message
49            sys.exit(-1)
50        warnings.resetwarnings()
51
52        # Getting dependencies versions
53        import higwidgets
54        import umitCore
55        import umitGUI
56
57        gtk_version = "%s.%s.%s" % gtk.gtk_version
58        pygtk_version = "%s.%s.%s" % gtk.ver
59        higwidgets_version = getattr(higwidgets, "__version__", "< 0.9.5")
60        python_version = sys.version
61        nmap_version = os.popen2("nmap -V")[1].read().strip("\n")
62        osuname = " ".join(os.uname())
63        umit_version = VERSION
64        umitCore_version = getattr(umitCore, "__version__", "< 0.9.5")
65        umitGUI_version = getattr(umitGUI, "__version__", "< 0.9.5")
66
67        versions = _("""
68Versions:
69---
70GTK: %s
71PyGTK: %s
72HIGWidgets: %s
73Python: %s
74Nmap: %s
75Operating System: %s
76Umit: %s
77UmitCore: %s
78UmitGUI: %s
79---""") % (gtk_version,
80           pygtk_version,
81           higwidgets_version,
82           python_version,
83           nmap_version,
84           osuname,
85           umit_version,
86           umitCore_version,
87           umitGUI_version)
88
89        if etype == ImportError:
90            d = HIGAlertDialog(type=gtk.MESSAGE_ERROR,
91                message_format=_("Import error"),
92                secondary_text=_("\nA required module was not "
93                    "found.\n\nError:") + " %s" % emsg)
94            d.run()
95            d.destroy()
96            return
97        crash_text = cgitb.text((etype, emsg, etb))
98        crash_text_dialog = "\n%s\n%s\n" % (versions, crash_text)
99        crash_text= "{{{\n%s\n%s\n}}}" % (versions, crash_text)
100       
101        #Dialog info
102        extrainfo_dialog = "%-17s %s\n%-17s %s\n%-17s %s\n%-17s %s\n" % (
103            "sys.platform", sys.platform, "os.name", os.name, 
104            "Gtk version", '.'.join(map(str, gtk.gtk_version)), 
105            "Umit version", VERSION)
106        crashmsg_dialog = "Crash Report\n%s\n%s\nDescription\n%s\n%s" % \
107                        ('=' * 10, extrainfo_dialog, '-' * 20,\
108                         crash_text_dialog)
109       
110        extrainfo = "%-17s %s\n[[BR]]%-17s %s\n[[BR]]%-17s %s\n[[BR]]%-17s %s[[BR]]\n" % (
111            "sys.platform", sys.platform, "os.name", os.name, 
112            "Gtk version", '.'.join(map(str, gtk.gtk_version)), 
113            "Umit version", VERSION)
114        crashmsg = "Crash Report\n[[BR]]%s[[BR]]\n[[BR]]%s\nDescription\n%s\n%s" % ('=' * 10, 
115            extrainfo, '-' * 20, crash_text)
116
117        try:
118            try:
119                cwin = CrashReport("Umit Crash - '%s'" % emsg, crashmsg,
120                                   description_dialog=crashmsg_dialog)
121                cwin.show_all()
122                while True: 
123                    # keeping running while bug report is not sent successfully,
124                    # or until the user closes the window.
125                    result = cwin.run()
126                    if result in (gtk.RESPONSE_CANCEL,
127                        gtk.RESPONSE_DELETE_EVENT,
128                        gtk.RESPONSE_NONE):
129
130                        cwin.destroy()
131                        break
132            except:
133                tempfd, tempname = tempfile.mkstemp()
134                os.write(tempfd, crashmsg_dialog)
135                d = HIGAlertDialog(type=gtk.MESSAGE_ERROR,
136                    message_format=_("Bug not reported"),
137                    secondary_text=_("A critical error occourried during "
138                        "Umit execution, \nand it was not properly reported " 
139                        "to our bug tracker. The crash description was saved to: "
140                        "%s, so you can still report it on our bug "
141                        "tracker.") % tempname)
142                os.close(tempfd)
143                d.run()
144                d.destroy()
145        finally:
146            gtk.main_quit()
147
148
149if not UMIT_DEVELOPMENT:
150    from tempfile import mktemp
151    # Generating temporary files names
152    stdout_output = mktemp()
153    stderr_output = mktemp()
154
155    old_stdout = sys.stdout
156    old_stderr = sys.stderr
157
158    _stdout = open(stdout_output, "w")
159    _stderr = open(stderr_output, "w")
160
161    sys.stdout = _stdout
162    sys.stderr = _stderr
163
164   
165    sys.excepthook = UmitExceptionHook()
166
167def main(args):
168    # Setting the umit home directory
169    from umitCore.Paths import Path
170    Path.set_umit_conf(os.path.split(args[0])[0])
171    #################################
172    Path.set_running_path(os.path.abspath(os.path.dirname(sys.argv[0])))
173
174    from umitGUI.App import App
175
176    umit_app = App()
177
178    if os.name == "posix":
179        signal.signal(signal.SIGHUP, umit_app.safe_shutdown)
180    signal.signal(signal.SIGTERM, umit_app.safe_shutdown)
181    signal.signal(signal.SIGINT, umit_app.safe_shutdown)
182
183    try:
184        umit_app.run()
185    except KeyboardInterrupt:
186        sys.exit(signal.SIGINT)
187
188if __name__ == "__main__":
189    main(sys.argv)
Note: See TracBrowser for help on using the browser.