New icon for Zero Install
[zeroinstall.git] / zeroinstall / 0launch-gui / bugs.py
blob4dbc9af12b45dcde766824b5d0b8af17b79cb8b8
1 # Copyright (C) 2009, Thomas Leonard
2 # See http://0install.net/0compile.html
4 import sys, os
5 import gtk, pango
6 import dialog
8 import zeroinstall
9 from zeroinstall import support
10 from zeroinstall.injector import selections
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_file
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 impls = policy.solver.details.get(chosen_iface, None)
45 if impls:
46 best, reason = impls[0]
47 note = 'best was %s, but: %s' % (best, reason)
48 else:
49 note = 'not considered; %d available' % len(chosen_iface.implementations)
51 text += ' No implementation selected (%s)\n' % note
53 if hasattr(os, 'uname'):
54 text += '\nSystem:\n %s\n\nIssue:\n %s\n' % ('\n '.join(os.uname()), issue)
55 else:
56 text += '\nSystem without uname()\n'
58 if policy.solver.ready:
59 sels = selections.Selections(policy)
60 text += "\n" + sels.toDOM().toprettyxml(encoding = 'utf-8')
62 reporter = BugReporter(policy, iface, text)
63 reporter.show()
65 class BugReporter(dialog.Dialog):
66 def __init__(self, policy, iface, env):
67 dialog.Dialog.__init__(self)
69 self.sf_group_id = 76468
70 self.sf_artifact_id = 929902
72 self.set_title(_('Report a Bug'))
73 self.set_modal(True)
74 self.set_has_separator(False)
75 self.policy = policy
76 self.frames = []
78 vbox = gtk.VBox(False, 4)
79 vbox.set_border_width(10)
80 self.vbox.pack_start(vbox, True, True, 0)
82 self.set_default_size(gtk.gdk.screen_width() / 2, -1)
84 def frame(title, contents, buffer):
85 fr = gtk.Frame()
86 label = gtk.Label('')
87 label.set_markup('<b>%s</b>' % title)
88 fr.set_label_widget(label)
89 fr.set_shadow_type(gtk.SHADOW_NONE)
90 vbox.pack_start(fr, True, True, 0)
92 align = gtk.Alignment(0, 0, 1, 1)
93 align.set_padding(0, 0, 16, 0)
94 fr.add(align)
95 align.add(contents)
97 self.frames.append((title, buffer))
99 def text_area(text = None, mono = False):
100 swin = gtk.ScrolledWindow()
101 swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
102 swin.set_shadow_type(gtk.SHADOW_IN)
104 tv = gtk.TextView()
105 tv.set_wrap_mode(gtk.WRAP_WORD)
106 swin.add(tv)
107 if text:
108 tv.get_buffer().insert_at_cursor(text)
110 if mono:
111 tv.modify_font(pango.FontDescription('mono'))
113 tv.set_accepts_tab(False)
115 return swin, tv.get_buffer()
117 actual = text_area()
118 frame(_("What doesn't work?"), *actual)
120 expected = text_area()
121 frame(_('What did you expect to happen?'), *expected)
123 errors_box = gtk.VBox(False, 0)
124 errors_swin, errors_buffer = text_area(mono = True)
125 errors_box.pack_start(errors_swin, True, True, 0)
126 buttons = gtk.HButtonBox()
127 buttons.set_layout(gtk.BUTTONBOX_START)
128 errors_box.pack_start(buttons, False, True, 4)
129 get_errors = gtk.Button(_('Run it now and record the output'))
130 get_errors.connect('clicked', lambda button: self.collect_output(errors_buffer))
131 buttons.add(get_errors)
133 frame(_('Are any errors or warnings displayed?'), errors_box, errors_buffer)
135 if dialog.last_error:
136 errors_buffer.insert_at_cursor(str(dialog.last_error))
138 environ = text_area(env, mono = True)
139 frame(_('Information about your setup'), *environ)
141 browse_url = 'http://sourceforge.net/tracker/?group_id=%d&atid=%d' % (self.sf_group_id, self.sf_artifact_id)
142 location_hbox = gtk.HBox(False, 4)
143 location_hbox.pack_start(gtk.Label(_('Bugs reports will be sent to:')), False, True, 0)
144 if hasattr(gtk, 'LinkButton'):
145 import browser
146 url_box = gtk.LinkButton(browse_url)
147 url_box.connect('clicked', lambda button: browser.open_in_browser(browse_url))
148 else:
149 url_box = gtk.Label(browse_url)
150 url_box.set_selectable(True)
151 location_hbox.pack_start(url_box, False, True, 0)
152 vbox.pack_start(location_hbox, False, True, 0)
154 self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
155 self.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
156 self.set_default_response(gtk.RESPONSE_OK)
158 def resp(box, r):
159 if r == gtk.RESPONSE_OK:
160 text = ''
161 for title, buffer in self.frames:
162 start = buffer.get_start_iter()
163 end = buffer.get_end_iter()
164 text += '%s\n\n%s\n\n' % (title, buffer.get_text(start, end).strip())
165 title = _('Bug for %s') % iface.get_name()
166 self.report_bug(title, text)
167 self.destroy()
168 dialog.alert(self, _("Your bug report has been sent. Thank you."),
169 type = gtk.MESSAGE_INFO)
170 else:
171 self.destroy()
172 self.connect('response', resp)
174 self.show_all()
176 def collect_output(self, buffer):
177 iter = buffer.get_end_iter()
178 buffer.place_cursor(iter)
180 if not self.policy.ready:
181 missing = [iface.uri for iface in self.policy.implementation if self.policy.implementation[iface] is None]
182 buffer.insert_at_cursor("Can't run: no version has been selected for:\n- " +
183 "\n- ".join(missing))
184 return
185 uncached = self.policy.get_uncached_implementations()
186 if uncached:
187 buffer.insert_at_cursor("Can't run: the chosen versions have not been downloaded yet. I need:\n\n- " +
188 "\n\n- " . join(['%s version %s\n (%s)' %(x[0].uri, x[1].get_version(), x[1].id) for x in uncached]))
189 return
191 from zeroinstall.injector import selections
192 sels = selections.Selections(self.policy)
193 doc = sels.toDOM()
195 self.hide()
196 try:
197 gtk.gdk.flush()
198 iter = buffer.get_end_iter()
199 buffer.place_cursor(iter)
201 # Tell 0launch to run the program
202 doc.documentElement.setAttribute('run-test', 'true')
203 payload = doc.toxml('utf-8')
204 sys.stdout.write(('Length:%8x\n' % len(payload)) + payload)
205 sys.stdout.flush()
207 reply = support.read_bytes(0, len('Length:') + 9)
208 assert reply.startswith('Length:')
209 test_output = support.read_bytes(0, int(reply.split(':', 1)[1], 16))
211 # Cope with invalid UTF-8
212 import codecs
213 decoder = codecs.getdecoder('utf-8')
214 data = decoder(test_output, 'replace')[0]
216 buffer.insert_at_cursor(data)
217 finally:
218 self.show()
220 def report_bug(self, title, text):
221 try:
222 import urllib
223 from urllib2 import urlopen
225 stream = urlopen('http://sourceforge.net/tracker/index.php',
226 urllib.urlencode({
227 'group_id': str(self.sf_group_id),
228 'atid': str(self.sf_artifact_id),
229 'func': 'postadd',
230 'is_private': '0',
231 'summary': title,
232 'details': text}))
233 stream.read()
234 stream.close()
235 except:
236 # Write to stderr in the hope that it doesn't get lost
237 print >>sys.stderr, "Error sending bug report: %s\n\n%s" % (title, text)
238 raise