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