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