summaryrefslogtreecommitdiff
path: root/apt/widgets.py
blob: 09236d21c1c28e29e00ea95e1324f2c5d4e433bd (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
367
368
369
370
371
372
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
widgets - GTK widgets to show the progress and status of apt

Copyright (c) 2004,2005 Canonical Ltd.

Authors: Michael Vogt <mvo@ubuntu.com>
         Sebastian Heinlein <glatzor@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.

his 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
"""

from gettext import gettext as _
import os
import time

import pygtk
pygtk.require('2.0')
import gtk
import gobject
import pango
import vte

import apt
import apt_pkg

# Seconds until a maintainer script will be regarded as hanging
INSTALL_TIMEOUT = 4 * 60

class GOpProgress(gobject.GObject, apt.progress.OpProgress):

    __gsignals__ = {"status-changed":(gobject.SIGNAL_RUN_FIRST,
                                      gobject.TYPE_NONE,
                                      (gobject.TYPE_STRING, gobject.TYPE_INT)),
                    "status-started":(gobject.SIGNAL_RUN_FIRST, 
                                      gobject.TYPE_NONE, ()),
                    "status-finished":(gobject.SIGNAL_RUN_FIRST, 
                                       gobject.TYPE_NONE, ())}

    def __init__(self):
        apt.progress.OpProgress.__init__(self)
        gobject.GObject.__init__(self)

    def update(self, percent):
        self.emit("status-changed", self.op, percent)
        while gtk.events_pending():
            gtk.main_iteration()

    def done(self):
        self.emit("status-finished")

class GInstallProgress(gobject.GObject, apt.progress.InstallProgress):

    __gsignals__ = {"status-changed":(gobject.SIGNAL_RUN_FIRST,
                                      gobject.TYPE_NONE,
                                      (gobject.TYPE_STRING, gobject.TYPE_INT)),
                    "status-started":(gobject.SIGNAL_RUN_FIRST, 
                                      gobject.TYPE_NONE, ()),
                    "status-timeout":(gobject.SIGNAL_RUN_FIRST, 
                                      gobject.TYPE_NONE, ()),
                    "status-error":(gobject.SIGNAL_RUN_FIRST, 
                                    gobject.TYPE_NONE, ()),
                    "status-conffile":(gobject.SIGNAL_RUN_FIRST, 
                                       gobject.TYPE_NONE, ()),
                    "status-finished":(gobject.SIGNAL_RUN_FIRST, 
                                       gobject.TYPE_NONE, ())}

    def __init__(self, term):
        apt.progress.InstallProgress.__init__(self)
        gobject.GObject.__init__(self)
        self.finished = False
        self.time_last_update = time.time()
        self.term = term
        reaper = vte.reaper_get()
        reaper.connect("child-exited", self.childExited)
        self.env = ["VTE_PTY_KEEP_FD=%s"% self.writefd,
                    "DEBIAN_FRONTEND=gnome",
                    "APT_LISTCHANGES_FRONTEND=gtk"]

    def childExited(self, term, pid, status):
        self.apt_status = os.WEXITSTATUS(status)
        self.finished = True

    def error(self, pkg, errormsg):
        self.emit("status-error")

    def conffile(self, current, new):
        self.emit("status-conffile")

    def startUpdate(self):
        self.emit("status-started")

    def finishUpdate(self):
        self.emit("status-finished")

    def statusChange(self, pkg, percent, status):
        self.time_last_update = time.time()
        self.emit("status-changed", status, percent)

    def updateInterface(self):
        apt.progress.InstallProgress.updateInterface(self)
        while gtk.events_pending():
            gtk.main_iteration()
        if self.time_last_update + INSTALL_TIMEOUT < time.time():
            self.emit("status-timeout")

    def fork(self):
        return self.term.forkpty(envv=self.env)

    def waitChild(self):
        while not self.finished:
            self.updateInterface()
        return self.apt_status


class GDpkgInstallProgress(apt.progress.DpkgInstallProgress,GInstallProgress):

    def run(self, debfile):
        apt.progress.DpkgInstallProgress.run(self, debfile)

    def updateInterface(self):
        apt.progress.DpkgInstallProgress.updateInterface(self)
        while gtk.events_pending():
            gtk.main_iteration()
        if self.time_last_update + INSTALL_TIMEOUT < time.time():
            self.emit("status-timeout")


class GFetchProgress(gobject.GObject, apt.progress.FetchProgress):

    __gsignals__ = {"status-changed":(gobject.SIGNAL_RUN_FIRST,
                                      gobject.TYPE_NONE,
                                      (gobject.TYPE_STRING, gobject.TYPE_INT)),
                    "status-started":(gobject.SIGNAL_RUN_FIRST, 
                                      gobject.TYPE_NONE, ()),
                    "status-finished":(gobject.SIGNAL_RUN_FIRST, 
                                       gobject.TYPE_NONE, ())}

    def __init__(self):
        apt.progress.FetchProgress.__init__(self)
        gobject.GObject.__init__(self)
        self._continue = True

    def start(self):
        self.emit("status-started")

    def stop(self):
        self.emit("status-finished")

    def cancel(self):
        self._continue = False

    def pulse(self):
        apt.progress.FetchProgress.pulse(self)
        currentItem = self.currentItems + 1
        if currentItem > self.totalItems:
          currentItem = self.totalItems
        if self.currentCPS > 0:
            text = (_("Downloading file %(current)li of %(total)li with "
                      "%(speed)s/s") % \
                      {"current" : currentItem,
                       "total" : self.totalItems,
                       "speed" : humanize_size(self.currentCPS)})
        else:
            text = (_("Downloading file %(current)li of %(total)li") % \
                      {"current" : currentItem,
                       "total" : self.totalItems })
        self.emit("status-changed", text, self.percent)
        while gtk.events_pending():
            gtk.main_iteration()
        return self._continue


class GtkAptProgress(gtk.VBox):
    """
    This widget provides a progress bar, a terminal and a status bar for
    showing the progress of package manipulation tasks.

    A simple example code snippet to install/remove a package:

    import pygtk
    pygtk.require('2.0')
    import gtk

    import apt.widgets

    win = gtk.Window()
    progress = apt.widgets.GtkAptProgress()
    win.set_title("GtkAptProgress Demo")
    win.add(progress)
    progress.show()
    win.show()
 
    cache = apt.cache.Cache(progress.get_open_progress())
    cache["xterm"].markDelete()
    progress.show_terminal(expanded=True)
    cache.commit(progress.get_fetch_progress(),
                 progress.get_install_progress())

    gtk.main()
    """
    def __init__(self):
        gtk.VBox.__init__(self)
        self.set_spacing(6)
        # Setup some child widgets
        self._expander = gtk.Expander(_("Details"))
        self._terminal = vte.Terminal()
        #self._terminal.set_font_from_string("monospace 10")
        self._expander.add(self._terminal)
        self._progressbar = gtk.ProgressBar()
        # Setup the always italic status label
        self._label = gtk.Label()
        attr_list =  pango.AttrList()
        attr_list.insert(pango.AttrStyle(pango.STYLE_ITALIC, 0, -1))
        self._label.set_attributes(attr_list)
        self._label.set_ellipsize(pango.ELLIPSIZE_END)
        self._label.set_alignment(0, 0)
        # add child widgets
        self.pack_start(self._progressbar, False)
        self.pack_start(self._label, False)
        self.pack_start(self._expander, False)
        # Setup the internal progress handlers
        self._progress_open = GOpProgress()
        self._progress_open.connect("status-changed", self._on_status_changed)
        self._progress_open.connect("status-started", self._on_status_started)
        self._progress_open.connect("status-finished", self._on_status_finished)
        self._progress_fetch = GFetchProgress()
        self._progress_fetch.connect("status-changed", self._on_status_changed)
        self._progress_fetch.connect("status-started", self._on_status_started)
        self._progress_fetch.connect("status-finished", 
                                     self._on_status_finished)
        self._progress_install = GInstallProgress(self._terminal)
        self._progress_install.connect("status-changed",
                                       self._on_status_changed)
        self._progress_install.connect("status-started", 
                                       self._on_status_started)
        self._progress_install.connect("status-finished", 
                                     self._on_status_finished)
        self._progress_install.connect("status-timeout", 
                                     self._on_status_timeout)
        self._progress_install.connect("status-error", 
                                     self._on_status_timeout)
        self._progress_install.connect("status-conffile", 
                                     self._on_status_timeout)
        self._progress_dpkg_install = GDpkgInstallProgress(self._terminal)
        self._progress_dpkg_install.connect("status-changed",
                                       self._on_status_changed)
        self._progress_dpkg_install.connect("status-started", 
                                       self._on_status_started)
        self._progress_dpkg_install.connect("status-finished", 
                                     self._on_status_finished)
        self._progress_dpkg_install.connect("status-timeout", 
                                     self._on_status_timeout)
        self._progress_dpkg_install.connect("status-error", 
                                     self._on_status_timeout)
        self._progress_dpkg_install.connect("status-conffile", 
                                     self._on_status_timeout)

    def clear(self):
        """
        Reset all status information
        """
        self._label.set_label("")
        self._progress.set_fraction(0)
        self._expander.set_expanded(False)

    def get_open_progress(self):
        """
        Return the cache opening progress handler.
        """
        return self._progress_open

    def get_install_progress(self):
        """
        Return the install progress handler
        """
        return self._progress_install

    def get_dpkg_install_progress(self):
        """
        Return the install progress handler for dpkg
        """
        return self._dpkg_progress_install

    def get_fetch_progress(self):
        """
        Return the fetch progress handler
        """
        return self._progress_fetch

    def _on_status_started(self, progress):
        self._on_status_changed(progress, _("Starting..."), 0)

    def _on_status_finished(self, progress):
        self._on_status_changed(progress, _("Complete"), 100)

    def _on_status_changed(self, progress, status, percent):
        self._label.set_text(status)
        self._progressbar.pulse()
        self._progressbar.set_fraction(percent/100.0)

    def _on_status_timeout(self, progress):
        selt._expander.set_expanded(True)

    def cancel_download(self):
        """
        Cancel a currently running download
        """
        self._progress_fetch.cancel()

    def show_terminal(self, expanded=False):
        """
        Show an expander with a terminal widget which provides a way
        to interact with dpkg
        """
        self._expander.show()
        self._terminal.show()
        self._expander.set_expanded(True)

    def hide_terminal(self):
        """
        Hide the expander with the terminal widget
        """
        self._expander.hide()

    def show(self):
        gtk.HBox.show(self)
        self._label.show()
        self._progressbar.show()

if __name__ == "__main__":
    import sys
    import debfile

    win = gtk.Window()
    apt_progress = GtkAptProgress()
    win.set_title("GtkAptProgress Demo")
    win.add(apt_progress)
    apt_progress.show()
    win.show()
    cache = apt.cache.Cache(apt_progress.get_open_progress())
    pkg = cache["xterm"]
    if pkg.isInstalled:
        pkg.markDelete()
    else:
        pkg.markInstall()
    apt_progress.show_terminal(True)
    try:
        cache.commit(apt_progress.get_fetch_progress(), 
                     apt_progress.get_install_progress())
    except:
        pass
    if len(sys.argv) > 1:
        deb = DebPackage(sys.argv[1], cache)
        deb.install(apt_progress.get_dpkg_install_progress())
    gtk.main()

# vim: ts=4 et sts=4