root/trunk/bin/umit @ 4240

Revision 4240, 6.9 kB (checked in by gpolo, 4 years ago)

Restructuring umit, second step. See ticket #229.

  • 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
28import platform
29
30# used by exception hook
31import cgitb
32import tempfile
33
34if not hasattr(sys, 'frozen'):
35    _source_path = os.path.abspath(
36            os.path.join(os.path.dirname(__file__), os.path.pardir))
37    if os.path.exists(os.path.join(_source_path, 'MANIFEST.in')):
38        # Assuming umit is being executed from a svn checkout.
39        sys.path.insert(0, _source_path)
40
41from umit.core.I18N import _
42from umit.core.Version import VERSION
43#########################
44
45UMIT_DEVELOPMENT = os.environ.get("UMIT_DEVELOPMENT", True)
46
47class UmitExceptionHook(object):
48    def __call__(self, etype, emsg, etb):
49        import warnings
50        warnings.filterwarnings("error", module = "gtk")
51        try:
52            import gtk
53            from umit.gui.BugReport import CrashReport
54            from higwidgets.higdialogs import HIGAlertDialog
55        except Warning, e:
56            print e.message
57            sys.exit(-1)
58        warnings.resetwarnings()
59
60        # Getting dependencies versions
61        import higwidgets
62        import umit.core
63        import umit.gui
64
65        gtk_version = "%s.%s.%s" % gtk.gtk_version
66        pygtk_version = "%s.%s.%s" % gtk.ver
67        higwidgets_version = getattr(higwidgets, "__version__", "< 0.9.5")
68        python_version = sys.version
69        nmap_version = os.popen2("nmap -V")[1].read().strip("\n")
70        try:
71            osuname = " ".join(os.uname())
72        except AttributeError:
73            # os.uname is not available under Windows, and other unlikely
74            # systems
75            try:
76                osuname = " ".join(platform.win32_ver())
77            except AttributeError:
78                osuname = "UNKNOWN"
79
80        umit_version = VERSION
81        umitCore_version = getattr(umit.core, "__version__", "< 0.9.5")
82        umitGUI_version = getattr(umit.gui, "__version__", "< 0.9.5")
83
84        versions = _("""
85Versions:
86---
87GTK: %s
88PyGTK: %s
89HIGWidgets: %s
90Python: %s
91Nmap: %s
92Operating System: %s
93Umit: %s
94UmitCore: %s
95UmitGUI: %s
96---""") % (gtk_version,
97           pygtk_version,
98           higwidgets_version,
99           python_version,
100           nmap_version,
101           osuname,
102           umit_version,
103           umitCore_version,
104           umitGUI_version)
105
106        if etype == ImportError:
107            d = HIGAlertDialog(type=gtk.MESSAGE_ERROR,
108                message_format=_("Import error"),
109                secondary_text=_("\nA required module was not "
110                    "found.\n\nError:") + " %s" % emsg)
111            d.run()
112            d.destroy()
113            return
114        crash_text = cgitb.text((etype, emsg, etb))
115        crash_text_dialog = "\n%s\n%s\n" % (versions, crash_text)
116        crash_text= "{{{\n%s\n%s\n}}}" % (versions, crash_text)
117       
118        #Dialog info
119        extrainfo_dialog = "%-17s %s\n%-17s %s\n%-17s %s\n%-17s %s\n" % (
120            "sys.platform", sys.platform, "os.name", os.name, 
121            "Gtk version", '.'.join(map(str, gtk.gtk_version)), 
122            "Umit version", VERSION)
123        crashmsg_dialog = "Crash Report\n%s\n%s\nDescription\n%s\n%s" % \
124                        ('=' * 10, extrainfo_dialog, '-' * 20,\
125                         crash_text_dialog)
126       
127        extrainfo = "%-17s %s\n[[BR]]%-17s %s\n[[BR]]%-17s %s\n[[BR]]%-17s %s[[BR]]\n" % (
128            "sys.platform", sys.platform, "os.name", os.name, 
129            "Gtk version", '.'.join(map(str, gtk.gtk_version)), 
130            "Umit version", VERSION)
131        crashmsg = "Crash Report\n[[BR]]%s[[BR]]\n[[BR]]%s\nDescription\n%s\n%s" % ('=' * 10, 
132            extrainfo, '-' * 20, crash_text)
133
134        try:
135            try:
136                cwin = CrashReport("Umit Crash - '%s'" % emsg, crashmsg,
137                                   description_dialog=crashmsg_dialog)
138                cwin.show_all()
139                while True: 
140                    # keeping running while bug report is not sent successfully,
141                    # or until the user closes the window.
142                    result = cwin.run()
143                    if result in (gtk.RESPONSE_CANCEL,
144                        gtk.RESPONSE_DELETE_EVENT,
145                        gtk.RESPONSE_NONE):
146
147                        cwin.destroy()
148                        break
149            except:
150                tempfd, tempname = tempfile.mkstemp()
151                os.write(tempfd, crashmsg_dialog)
152                d = HIGAlertDialog(type=gtk.MESSAGE_ERROR,
153                    message_format=_("Bug not reported"),
154                    secondary_text=_("A critical error occourried during "
155                        "Umit execution, \nand it was not properly reported " 
156                        "to our bug tracker. The crash description was saved to: "
157                        "%s, so you can still report it on our bug "
158                        "tracker.") % tempname)
159                os.close(tempfd)
160                d.run()
161                d.destroy()
162        finally:
163            gtk.main_quit()
164
165
166if not UMIT_DEVELOPMENT:
167    from tempfile import mktemp
168    # Generating temporary files names
169    stdout_output = mktemp()
170    stderr_output = mktemp()
171
172    old_stdout = sys.stdout
173    old_stderr = sys.stderr
174
175    _stdout = open(stdout_output, "w")
176    _stderr = open(stderr_output, "w")
177
178    sys.stdout = _stdout
179    sys.stderr = _stderr
180
181   
182    sys.excepthook = UmitExceptionHook()
183
184def main(args):
185    # Setting the umit home directory
186    from umit.core.Paths import Path
187    Path.set_umit_conf(os.path.split(args[0])[0])
188    #################################
189    Path.set_running_path(os.path.abspath(os.path.dirname(sys.argv[0])))
190
191    from umit.gui.App import App
192
193    umit_app = App()
194
195    if os.name == "posix":
196        signal.signal(signal.SIGHUP, umit_app.safe_shutdown)
197    signal.signal(signal.SIGTERM, umit_app.safe_shutdown)
198    signal.signal(signal.SIGINT, umit_app.safe_shutdown)
199
200    try:
201        umit_app.run()
202    except KeyboardInterrupt:
203        sys.exit(signal.SIGINT)
204
205if __name__ == "__main__":
206    main(sys.argv)
Note: See TracBrowser for help on using the browser.