Need to check how proxies interact with pooling
[zeroinstall.git] / zeroinstall / 0launch-gui / bugs.py
blob31cef8469681b1bca87ea50c190a113ec78f08bc
1 # Copyright (C) 2009, Thomas Leonard
2 # See http://0install.net/0compile.html
4 from __future__ import print_function
6 import sys, os
7 import gtk, pango
8 import dialog
10 import zeroinstall
11 from zeroinstall import _
12 from zeroinstall import support
14 def report_bug(policy, iface):
15 assert iface
17 # TODO: Check the interface to decide where to send bug reports
19 issue_file = '/etc/issue'
20 if os.path.exists(issue_file):
21 issue = open(issue_file).read().strip()
22 else:
23 issue = "(file '%s' not found)" % issue_file
25 text = 'Problem with %s\n' % iface.uri
26 if iface.uri != policy.root:
27 text = ' (while attempting to run %s)\n' % policy.root
28 text += '\n'
30 text += 'Zero Install: Version %s, with Python %s\n' % (zeroinstall.version, sys.version)
32 text += '\nChosen implementations:\n'
34 if not policy.ready:
35 text += ' Failed to select all required implementations\n'
37 for chosen_iface_uri, impl in policy.solver.selections.selections.iteritems():
38 text += '\n Interface: %s\n' % chosen_iface_uri
39 if impl:
40 text += ' Version: %s\n' % impl.version
41 feed_url = impl.attrs['from-feed']
42 if feed_url != chosen_iface_uri:
43 text += ' From feed: %s\n' % feed_url
44 text += ' ID: %s\n' % impl.id
45 else:
46 chosen_iface = policy.config.iface_cache.get_interface(chosen_iface_uri)
47 impls = policy.solver.details.get(chosen_iface, None)
48 if impls:
49 best, reason = impls[0]
50 note = 'best was %s, but: %s' % (best, reason)
51 else:
52 note = 'not considered; %d available' % len(chosen_iface.implementations)
54 text += ' No implementation selected (%s)\n' % note
56 if hasattr(os, 'uname'):
57 text += '\nSystem:\n %s\n\nIssue:\n %s\n' % ('\n '.join(os.uname()), issue)
58 else:
59 text += '\nSystem without uname()\n'
61 if policy.solver.ready:
62 sels = policy.solver.selections
63 text += "\n" + sels.toDOM().toprettyxml(encoding = 'utf-8')
65 reporter = BugReporter(policy, iface, text)
66 reporter.show()
68 class BugReporter(dialog.Dialog):
69 def __init__(self, policy, iface, env):
70 dialog.Dialog.__init__(self)
72 self.sf_group_id = 76468
73 self.sf_artifact_id = 929902
75 self.set_title(_('Report a Bug'))
76 self.set_modal(True)
77 self.set_has_separator(False)
78 self.policy = policy
79 self.frames = []
81 vbox = gtk.VBox(False, 4)
82 vbox.set_border_width(10)
83 self.vbox.pack_start(vbox, True, True, 0)
85 self.set_default_size(gtk.gdk.screen_width() / 2, -1)
87 def frame(title, contents, buffer):
88 fr = gtk.Frame()
89 label = gtk.Label('')
90 label.set_markup('<b>%s</b>' % title)
91 fr.set_label_widget(label)
92 fr.set_shadow_type(gtk.SHADOW_NONE)
93 vbox.pack_start(fr, True, True, 0)
95 align = gtk.Alignment(0, 0, 1, 1)
96 align.set_padding(0, 0, 16, 0)
97 fr.add(align)
98 align.add(contents)
100 self.frames.append((title, buffer))
102 def text_area(text = None, mono = False):
103 swin = gtk.ScrolledWindow()
104 swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
105 swin.set_shadow_type(gtk.SHADOW_IN)
107 tv = gtk.TextView()
108 tv.set_wrap_mode(gtk.WRAP_WORD)
109 swin.add(tv)
110 if text:
111 tv.get_buffer().insert_at_cursor(text)
113 if mono:
114 tv.modify_font(pango.FontDescription('mono'))
116 tv.set_accepts_tab(False)
118 return swin, tv.get_buffer()
120 actual = text_area()
121 frame(_("What doesn't work?"), *actual)
123 expected = text_area()
124 frame(_('What did you expect to happen?'), *expected)
126 errors_box = gtk.VBox(False, 0)
127 errors_swin, errors_buffer = text_area(mono = True)
128 errors_box.pack_start(errors_swin, True, True, 0)
129 buttons = gtk.HButtonBox()
130 buttons.set_layout(gtk.BUTTONBOX_START)
131 errors_box.pack_start(buttons, False, True, 4)
132 get_errors = gtk.Button(_('Run it now and record the output'))
133 get_errors.connect('clicked', lambda button: self.collect_output(errors_buffer))
134 buttons.add(get_errors)
136 frame(_('Are any errors or warnings displayed?'), errors_box, errors_buffer)
138 if dialog.last_error:
139 errors_buffer.insert_at_cursor(str(dialog.last_error))
141 environ = text_area(env, mono = True)
142 frame(_('Information about your setup'), *environ)
144 browse_url = 'http://sourceforge.net/tracker/?group_id=%d&atid=%d' % (self.sf_group_id, self.sf_artifact_id)
145 location_hbox = gtk.HBox(False, 4)
146 location_hbox.pack_start(gtk.Label(_('Bugs reports will be sent to:')), False, True, 0)
147 if hasattr(gtk, 'LinkButton'):
148 import browser
149 url_box = gtk.LinkButton(browse_url)
150 url_box.connect('clicked', lambda button: browser.open_in_browser(browse_url))
151 else:
152 url_box = gtk.Label(browse_url)
153 url_box.set_selectable(True)
154 location_hbox.pack_start(url_box, False, True, 0)
155 vbox.pack_start(location_hbox, False, True, 0)
157 self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
158 self.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
159 self.set_default_response(gtk.RESPONSE_OK)
161 def resp(box, r):
162 if r == gtk.RESPONSE_OK:
163 text = ''
164 for title, buffer in self.frames:
165 start = buffer.get_start_iter()
166 end = buffer.get_end_iter()
167 text += '%s\n\n%s\n\n' % (title, buffer.get_text(start, end).strip())
168 title = _('Bug for %s') % iface.get_name()
169 self.report_bug(title, text)
170 self.destroy()
171 dialog.alert(self, _("Your bug report has been sent. Thank you."),
172 type = gtk.MESSAGE_INFO)
173 else:
174 self.destroy()
175 self.connect('response', resp)
177 self.show_all()
179 def collect_output(self, buffer):
180 iter = buffer.get_end_iter()
181 buffer.place_cursor(iter)
183 if not self.policy.ready:
184 missing = [iface.uri for iface in self.policy.implementation if self.policy.implementation[iface] is None]
185 buffer.insert_at_cursor("Can't run: no version has been selected for:\n- " +
186 "\n- ".join(missing))
187 return
188 uncached = self.policy.get_uncached_implementations()
189 if uncached:
190 buffer.insert_at_cursor("Can't run: the chosen versions have not been downloaded yet. I need:\n\n- " +
191 "\n\n- " . join(['%s version %s\n (%s)' %(x[0].uri, x[1].get_version(), x[1].id) for x in uncached]))
192 return
194 sels = self.policy.solver.selections
195 doc = sels.toDOM()
197 self.hide()
198 try:
199 gtk.gdk.flush()
200 iter = buffer.get_end_iter()
201 buffer.place_cursor(iter)
203 # Tell 0launch to run the program
204 doc.documentElement.setAttribute('run-test', 'true')
205 payload = doc.toxml('utf-8')
206 sys.stdout.write(('Length:%8x\n' % len(payload)) + payload)
207 sys.stdout.flush()
209 reply = support.read_bytes(0, len('Length:') + 9)
210 assert reply.startswith('Length:')
211 test_output = support.read_bytes(0, int(reply.split(':', 1)[1], 16))
213 # Cope with invalid UTF-8
214 import codecs
215 decoder = codecs.getdecoder('utf-8')
216 data = decoder(test_output, 'replace')[0]
218 buffer.insert_at_cursor(data)
219 finally:
220 self.show()
222 def report_bug(self, title, text):
223 try:
224 import urllib
225 from urllib2 import urlopen
227 stream = urlopen('http://sourceforge.net/tracker/index.php',
228 urllib.urlencode({
229 'group_id': str(self.sf_group_id),
230 'atid': str(self.sf_artifact_id),
231 'func': 'postadd',
232 'is_private': '0',
233 'summary': title,
234 'details': text}))
235 stream.read()
236 stream.close()
237 except:
238 # Write to stderr in the hope that it doesn't get lost
239 print("Error sending bug report: %s\n\n%s" % (title, text), file=sys.stderr)
240 raise