summaryrefslogtreecommitdiff
path: root/DistUpgrade/DistUpgradeViewGtk.py
blob: eff02471451d8e8ef9b2246a2f9ccd1b493e33cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# DistUpgradeViewGtk.py 
#  
#  Copyright (c) 2004,2005 Canonical
#  
#  Author: Michael Vogt <michael.vogt@ubuntu.com>
# 
#  This program is free software; you can redistribute it and/or 
#  modify it under the terms of the GNU General Public License as 
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import pygtk
pygtk.require('2.0')
import gtk
import gtk.gdk
import gtk.glade
import vte
import gobject
import pango
import sys
import logging

import apt
import apt_pkg
import os

from apt.progress import InstallProgress
from DistUpgradeView import DistUpgradeView
from UpdateManager.Common.SimpleGladeApp import SimpleGladeApp, bindtextdomain

from gettext import gettext as _

class GtkOpProgress(apt.progress.OpProgress):
  def __init__(self, progressbar):
      self.progressbar = progressbar
  def update(self, percent):
      #self._progressbar.show()
      #self.progressbar.set_text(self.op)
      self.progressbar.set_fraction(percent/100.0)
      while gtk.events_pending():
          gtk.main_iteration()
  def done(self):
      #self.progressbar.hide()
      self.progressbar.set_text(" ")


class GtkFetchProgressAdapter(apt.progress.FetchProgress):
    # FIXME: we really should have some sort of "we are at step"
    # xy in the gui
    # FIXME2: we need to thing about mediaCheck here too
    def __init__(self, parent):
        # if this is set to false the download will cancel
        self.status = parent.label_status
        self.progress = parent.progressbar_cache
    def mediaChange(self, medium, drive):
      #print "mediaChange %s %s" % (medium, drive)
      msg = _("Please insert '%s' into the drive '%s'" % (medium,drive))
      dialog = gtk.MessageDialog(parent=self.main,
                                 flags=gtk.DIALOG_MODAL,
                                 type=gtk.MESSAGE_QUESTION,
                                 buttons=gtk.BUTTONS_OK_CANCEL)
      dialog.set_markup(msg)
      res = dialog.run()
      #print res
      dialog.destroy()
      if  res == gtk.RESPONSE_OK:
        return True
      return False
    def start(self):
        #self.progress.show()
        self.progress.set_fraction(0)
        self.status.show()
    def stop(self):
        #self.progress.hide()
        self.progress.set_text(" ")
        self.status.set_text("")
    def pulse(self):
        # FIXME: move the status_str and progress_str into python-apt
        # (python-apt need i18n first for this)
        apt.progress.FetchProgress.pulse(self)
        self.progress.set_fraction(self.percent/100.0)
        currentItem = self.currentItems + 1
        if currentItem > self.totalItems:
            currentItem = self.totalItems

        if self.currentCPS > 0:
            self.status.set_text(_("Downloading file %li of %li with %s/s" % (currentItem, self.totalItems, apt_pkg.SizeToStr(self.currentCPS))))
            self.progress.set_text(_("%s remaining" % apt_pkg.TimeToStr(self.eta)))
        else:
            self.status.set_text(_("Downloading file %li of %li with unknown speed" % (currentItem, self.totalItems)))
            self.progress.set_text("  ")

        while gtk.events_pending():
            gtk.main_iteration()
        return True

class GtkInstallProgressAdapter(InstallProgress):
    def __init__(self,parent):
        InstallProgress.__init__(self)
        self.label_status = parent.label_status
        self.progress = parent.progressbar_cache
        self.expander = parent.expander_terminal
        self.term = parent._term
        self.parent = parent
        # setup the child waiting
        reaper = vte.reaper_get()
        reaper.connect("child-exited", self.child_exited)
    def startUpdate(self):
        self.finished = False
        # FIXME: add support for the timeout
        # of the terminal (to display something useful then)
        # -> longer term, move this code into python-apt 
        self.label_status.set_text(_("Installing updates ..."))
        self.progress.set_fraction(0.0)
        self.progress.set_text(" ")
        self.expander.set_sensitive(True)
        self.term.show()
        self.env = ["VTE_PTY_KEEP_FD=%s"% self.writefd,
                    "DEBIAN_FRONTEND=gnome",
                    "APT_LISTCHANGES_FRONTEND=none"]
    def error(self, pkg, errormsg):
        logging.error("got a error from dpkg for pkg: '%s': '%s'" % (pkg, errormsg))
        dialog = gtk.MessageDialog(self.parent.window_main, 0,
                                   gtk.MESSAGE_ERROR,
                                   gtk.BUTTONS_OK,"")
        summary = _("Error installing '%s'" % pkg)
        msg = _("During the install a error occured. This is usually a bug "
                "in the packages, please report it. See the message below "
                "for more information. ")
        msg="<big><b>%s</b></big>\n\n%s" % (summary,msg)
        dialog.set_markup(msg)
        dialog.vbox.set_spacing(6)
        if errormsg != None:
          scroll = gtk.ScrolledWindow()
          scroll.set_size_request(400,200)
          textview = gtk.TextView()
          textview.set_cursor_visible(False)
          textview.set_editable(False)
          textview.get_buffer().set_text(errormsg)
          textview.show()
          scroll.add(textview)
          scroll.show()
          dialog.vbox.pack_end(scroll)
        dialog.run()
        dialog.destroy()
    def conffile(self, current, new):
        logging.debug("got a conffile-prompt from dpkg for pkg: '%s'" % current)
        self.expander.set_expanded(True)
        pass
    def fork(self):
        pid = self.term.forkpty(envv=self.env)
        return pid
    def child_exited(self, term, pid, status):
        self.apt_status = os.WEXITSTATUS(status)
        self.finished = True
    def waitChild(self):
        while not self.finished:
            self.updateInterface()
        return self.apt_status
    def finishUpdate(self):
        #self.progress.hide()
        self.label_status.set_text("")
    def updateInterface(self):
        InstallProgress.updateInterface(self)
        self.progress.set_fraction(self.percent/100.0)
        self.label_status.set_text(self.status)
        while gtk.events_pending():
            gtk.main_iteration()


class GtkDistUpgradeView(DistUpgradeView,SimpleGladeApp):
    " gtk frontend of the distUpgrade tool "

      
    
    def __init__(self):
        # FIXME: i18n must be somewhere relative do this dir
        bindtextdomain("update-manager",os.path.join(os.getcwd(),"mo"))

        SimpleGladeApp.__init__(self, "DistUpgrade.glade",
                                None, domain="update-manager")
        self.window_main.set_keep_above(True)
        self._opCacheProgress = GtkOpProgress(self.progressbar_cache)
        self._fetchProgress = GtkFetchProgressAdapter(self)
        self._installProgress = GtkInstallProgressAdapter(self)
        # details dialog
        self.details_list = gtk.ListStore(gobject.TYPE_STRING)
        column = gtk.TreeViewColumn("")
        render = gtk.CellRendererText()
        column.pack_start(render, True)
        column.add_attribute(render, "markup", 0)
        self.treeview_details.append_column(column)
        self.treeview_details.set_model(self.details_list)
        self.vscrollbar_terminal.set_adjustment(self._term.get_adjustment())
        # work around bug in VteTerminal here
        self._term.realize()

        # Use italic style in the status labels
        attrlist=pango.AttrList()
        attr = pango.AttrStyle(pango.STYLE_ITALIC, 0, -1)
        attrlist.insert(attr)
        self.label_status.set_property("attributes", attrlist)

        # reasonable fault handler
        sys.excepthook = self._handleException

    def _handleException(self, type, value, tb):
      import traceback
      lines = traceback.format_exception(type, value, tb)
      logging.error("not handled expection:\n%s" % "\n".join(lines))
      self.error(_("A fatal error occured"),
                 _("During the operation a fatal error occured. "
                   "Please report this as a bug and include the "
                   "files ~/dist-upgrade.log and ~/dist-upgrade-apt.log "
                   "in your report. The upgrade will abort now. "),
                 "\n".join(lines))

    def create_terminal(self, arg1,arg2,arg3,arg4):
        " helper to create a vte terminal "
        self._term = vte.Terminal()
        self._term.set_font_from_string("monospace 10")
        self._term.connect("contents-changed", self._term_content_changed)
        self._terminal_lines = []
        self._terminal_log = open(os.path.expanduser("~/dist-upgrade-term.log"),"w")
        return self._term
    def _term_content_changed(self, term):
        " called when the *visible* part of the terminal changes "

        # get the current visible text, 
        current_text = self._term.get_text(lambda a,b,c,d: True)
        # see what we have currently and only print stuff that wasn't
        # visible last time
        new_lines = []
        for line in current_text.split("\n"):
          new_lines.append(line)
          if not line in self._terminal_lines:
            self._terminal_log.write(line+"\n")
            self._terminal_log.flush()
        self._terminal_lines = new_lines
    def getFetchProgress(self):
        return self._fetchProgress
    def getInstallProgress(self):
        return self._installProgress
    def getOpCacheProgress(self):
        return self._opCacheProgress
    def updateStatus(self, msg):
        self.label_status.set_text("%s" % msg)
    def setStep(self, step):
        # first update the "last" step as completed
        size = gtk.ICON_SIZE_MENU
        attrlist=pango.AttrList()
        if step > 1:
            image = getattr(self,"image_step%i" % (step-1))
            label = getattr(self,"label_step%i" % (step-1))
            image.set_from_stock(gtk.STOCK_APPLY, size)
            label.set_property("attributes",attrlist)
        image = getattr(self,"image_step%i" % step)
        label = getattr(self,"label_step%i" % step)
        image.set_from_stock(gtk.STOCK_YES, size)
        attr = pango.AttrWeight(pango.WEIGHT_BOLD, 0, -1)
        # we can't make it bold here without layout changes in the view :(
        #attr = pango.AttrStyle(pango.STYLE_ITALIC, 0, -1)
        attrlist.insert(attr)
        label.set_property("attributes",attrlist)
                                
    def error(self, summary, msg, extended_msg=None):
        dialog = gtk.MessageDialog(self.window_main, 0, gtk.MESSAGE_ERROR,
                                   gtk.BUTTONS_OK,"")
        msg="<big><b>%s</b></big>\n\n%s" % (summary,msg)
        dialog.set_markup(msg)
        dialog.vbox.set_spacing(6)
        if extended_msg != None:
          scroll = gtk.ScrolledWindow()
          scroll.set_size_request(400,200)
          textview = gtk.TextView()
          textview.set_cursor_visible(False)
          textview.set_editable(False)
          textview.get_buffer().set_text(extended_msg)
          textview.show()
          scroll.add(textview)
          scroll.show()
          dialog.vbox.pack_end(scroll)
        dialog.run()
        dialog.destroy()
        return False

    def confirmChanges(self, summary, changes, downloadSize):
        # FIXME: add a whitelist here for packages that we expect to be
        # removed (how to calc this automatically?)
        DistUpgradeView.confirmChanges(self, summary, changes, downloadSize)
        self.label_summary.set_markup("<big><b>%s</b></big>" % summary)
        msg = _("%s packages are going to be removed.\n"
                "%s packages are going to be newly installed.\n"
                "%s packages are going to be upgraded.\n\n"
                "%s needs to be fetched" % (len(self.toRemove),
                                            len(self.toInstall),
                                            len(self.toUpgrade),
                                            apt_pkg.SizeToStr(downloadSize)))
        self.label_changes.set_text(msg)
        # fill in the details
        self.details_list.clear()
        for rm in self.toRemove:
            self.details_list.append([_("<b>To be removed: %s</b>" % rm)])
        for inst in self.toInstall:
            self.details_list.append([_("To be installed: %s" % inst)])
        for up in self.toUpgrade:
            self.details_list.append([_("To be upgraded: %s" % up)])
        self.dialog_changes.set_transient_for(self.window_main)
        res = self.dialog_changes.run()
        self.dialog_changes.hide()
        if res == gtk.RESPONSE_YES:
            return True
        return False

    def askYesNoQuestion(self, summary, msg):
        msg = "<big><b>%s</b></big>\n\n%s" % (summary,msg)
        dialog = gtk.MessageDialog(parent=self.window_main,
                                   flags=gtk.DIALOG_MODAL,
                                   type=gtk.MESSAGE_QUESTION,
                                   buttons=gtk.BUTTONS_YES_NO)
        dialog.set_markup(msg)
        res = dialog.run()
        dialog.destroy()
        if res == gtk.RESPONSE_YES:
            return True
        return False
    
    def confirmRestart(self):
        self.dialog_restart.set_transient_for(self.window_main)
        res = self.dialog_restart.run()
        self.dialog_restart.hide()
        if res == gtk.RESPONSE_YES:
            return True
        return False

    def on_window_main_delete_event(self, widget, event):
      #print "on_window_main_delete_event()"
      summary = _("Are you sure you want cancel?")
      msg = _("Canceling during a upgrade can leave the system in a "
              "unstable state. It is strongly adviced to continue "
              "the operation. ")
      if self.askYesNoQuestion(summary, msg):
        self.exit(1)
      return True

if __name__ == "__main__":
  view = GtkDistUpgradeView()
  view.error("short","long",
             "asfds afsdj af asdf asdf asf dsa fadsf asdf as fasf sextended\n"
             "asfds afsdj af asdf asdf asf dsa fadsf asdf as fasf sextended\n"
             "asfds afsdj af asdf asdf asf dsa fadsf asdf as fasf sextended\n"
             "asfds afsdj af asdf asdf asf dsa fadsf asdf as fasf sextended\n"
             "asfds afsdj af asdf asdf asf dsa fadsf asdf as fasf sextended\n"
             "asfds afsdj af asdf asdf asf dsa fadsf asdf as fasf sextended\n"
             "asfds afsdj af asdf asdf asf dsa fadsf asdf as fasf sextended\n"
             )