Added a link button in the bug report dialog to existing bug reports
[zeroinstall/zeroinstall-mseaborn.git] / zeroinstall / 0launch-gui / bugs.py
blob76acf38f1ed5044c1a28268cf3e153dad0b8a157
1 # Copyright (C) 2007, Thomas Leonard
2 # See http://0install.net/0compile.html
4 import sys, os
5 import gtk, pango
6 import dialog
7 import logging
9 import zeroinstall
10 from zeroinstall import support
12 def report_bug(policy, iface):
13 assert iface
15 # TODO: Check the interface to decide where to send bug reports
17 issue_file = '/etc/issue'
18 if os.path.exists(issue_file):
19 issue = file(issue_file).read().strip()
20 else:
21 issue = "(file '%s' not found)" % issue
23 text = 'Problem with %s\n' % iface.uri
24 if iface.uri != policy.root:
25 text = ' (while attempting to run %s)\n' % policy.root
26 text += '\n'
28 text += 'Zero Install: Version %s, with Python %s\n' % (zeroinstall.version, sys.version)
30 text += '\nChosen implementations:\n'
32 if not policy.ready:
33 text += ' Failed to select all required implementations\n'
35 for chosen_iface in policy.implementation:
36 text += '\n Interface: %s\n' % chosen_iface.uri
37 impl = policy.implementation[chosen_iface]
38 if impl:
39 text += ' Version: %s\n' % impl.get_version()
40 if impl.feed.url != chosen_iface.uri:
41 text += ' From feed: %s\n' % impl.feed.url
42 text += ' ID: %s\n' % impl.id
43 else:
44 text += ' No implementation selected\n'
46 text += '\nSystem:\n %s\n\nIssue:\n %s\n' % ('\n '.join(os.uname()), issue)
48 reporter = BugReporter(policy, iface, text)
49 reporter.show()
51 class BugReporter(dialog.Dialog):
52 def __init__(self, policy, iface, env):
53 dialog.Dialog.__init__(self)
55 self.sf_group_id = 76468
56 self.sf_artifact_id = 929902
58 self.set_title('Report a Bug')
59 self.set_modal(True)
60 self.set_has_separator(False)
61 self.policy = policy
62 self.frames = []
64 vbox = gtk.VBox(False, 4)
65 vbox.set_border_width(10)
66 self.vbox.pack_start(vbox, True, True, 0)
68 self.set_default_size(gtk.gdk.screen_width() / 2, -1)
70 def frame(title, contents, buffer):
71 fr = gtk.Frame()
72 label = gtk.Label('')
73 label.set_markup('<b>%s</b>' % title)
74 fr.set_label_widget(label)
75 fr.set_shadow_type(gtk.SHADOW_NONE)
76 vbox.pack_start(fr, True, True, 0)
78 align = gtk.Alignment(0, 0, 1, 1)
79 align.set_padding(0, 0, 16, 0)
80 fr.add(align)
81 align.add(contents)
83 self.frames.append((title, buffer))
85 def text_area(text = None, mono = False):
86 swin = gtk.ScrolledWindow()
87 swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
88 swin.set_shadow_type(gtk.SHADOW_IN)
90 tv = gtk.TextView()
91 tv.set_wrap_mode(gtk.WRAP_WORD)
92 swin.add(tv)
93 if text:
94 tv.get_buffer().insert_at_cursor(text)
96 if mono:
97 tv.modify_font(pango.FontDescription('mono'))
99 tv.set_accepts_tab(False)
101 return swin, tv.get_buffer()
103 actual = text_area()
104 frame("What doesn't work?", *actual)
106 expected = text_area()
107 frame('What did you expect to happen?', *expected)
109 errors_box = gtk.VBox(False, 0)
110 errors_swin, errors_buffer = text_area(mono = True)
111 errors_box.pack_start(errors_swin, True, True, 0)
112 buttons = gtk.HButtonBox()
113 buttons.set_layout(gtk.BUTTONBOX_START)
114 errors_box.pack_start(buttons, False, True, 4)
115 get_errors = gtk.Button('Run it now and record the output')
116 get_errors.connect('clicked', lambda button: self.collect_output(errors_buffer))
117 buttons.add(get_errors)
119 frame('Are any errors or warnings displayed?', errors_box, errors_buffer)
121 if dialog.last_error:
122 errors_buffer.insert_at_cursor(str(dialog.last_error))
124 environ = text_area(env, mono = True)
125 frame('Information about your setup', *environ)
127 browse_url = 'http://sourceforge.net/tracker/?group_id=%d&atid=%d' % (self.sf_group_id, self.sf_artifact_id)
128 location_hbox = gtk.HBox(False, 4)
129 location_hbox.pack_start(gtk.Label(_('Bugs reports will be sent to:')), False, True, 0)
130 if hasattr(gtk, 'LinkButton'):
131 import browser
132 url_box = gtk.LinkButton(browse_url)
133 url_box.connect('clicked', lambda button: browser.open_in_browser(browse_url))
134 else:
135 url_box = gtk.Label(browse_url)
136 url_box.set_selectable(True)
137 location_hbox.pack_start(url_box, False, True, 0)
138 vbox.pack_start(location_hbox, False, True, 0)
140 self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
141 self.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
142 self.set_default_response(gtk.RESPONSE_OK)
144 def resp(box, r):
145 if r == gtk.RESPONSE_OK:
146 text = ''
147 for title, buffer in self.frames:
148 start = buffer.get_start_iter()
149 end = buffer.get_end_iter()
150 text += '%s\n\n%s\n\n' % (title, buffer.get_text(start, end).strip())
151 title = 'Bug for %s' % iface.get_name()
152 self.report_bug(title, text)
153 self.destroy()
154 dialog.alert(self, "Your bug report has been sent. Thank you.",
155 type = gtk.MESSAGE_INFO)
156 else:
157 self.destroy()
158 self.connect('response', resp)
160 self.show_all()
162 def collect_output(self, buffer):
163 import logging
164 from zeroinstall.injector import run
166 iter = buffer.get_end_iter()
167 buffer.place_cursor(iter)
169 if not self.policy.ready:
170 missing = [iface.uri for iface in self.policy.implementation if self.policy.implementation[iface] is None]
171 buffer.insert_at_cursor("Can't run: no version has been selected for:\n- " +
172 "\n- ".join(missing))
173 return
174 uncached = self.policy.get_uncached_implementations()
175 if uncached:
176 buffer.insert_at_cursor("Can't run: the chosen versions have not been downloaded yet. I need:\n\n- " +
177 "\n\n- " . join(['%s version %s\n (%s)' %(x[0].uri, x[1].get_version(), x[1].id) for x in uncached]))
178 return
180 from zeroinstall.injector import selections
181 sels = selections.Selections(self.policy)
182 doc = sels.toDOM()
184 self.hide()
185 try:
186 gtk.gdk.flush()
187 iter = buffer.get_end_iter()
188 buffer.place_cursor(iter)
190 # Tell 0launch to run the program
191 doc.documentElement.setAttribute('run-test', 'true')
192 payload = doc.toxml('utf-8')
193 sys.stdout.write(('Length:%8x\n' % len(payload)) + payload)
194 sys.stdout.flush()
196 reply = support.read_bytes(0, len('Length:') + 9)
197 assert reply.startswith('Length:')
198 test_output = support.read_bytes(0, int(reply.split(':', 1)[1], 16))
200 # Cope with invalid UTF-8
201 import codecs
202 decoder = codecs.getdecoder('utf-8')
203 data = decoder(test_output, 'replace')[0]
205 buffer.insert_at_cursor(data)
206 finally:
207 self.show()
209 def report_bug(self, title, text):
210 try:
211 import urllib
212 from urllib2 import urlopen
214 stream = urlopen('http://sourceforge.net/tracker/index.php',
215 urllib.urlencode({
216 'group_id': str(self.sf_group_id),
217 'atid': str(self.sf_artifact_id),
218 'func': 'postadd',
219 'is_private': '0',
220 'summary': title,
221 'details': text}))
222 stream.read()
223 stream.close()
224 except:
225 # Write to stderr in the hope that it doesn't get lost
226 print >>sys.stderr, "Error sending bug report: %s\n\n%s" % (title, text)
227 raise