MINI SHELL

Server : Apache/2.2.2 (Fedora)
System : Linux App1.pathumtani.go.th 2.6.20-1.2320.fc5smp #1 SMP Tue Jun 12 19:40:16 EDT 2007 i686
User : apache ( 48)
PHP Version : 5.2.9
Disable Function : NONE
Directory :  /proc/self/root/usr/lib/python2.4/site-packages/pirut/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //proc/self/root/usr/lib/python2.4/site-packages/pirut/Progress.py
#!/usr/bin/python -tt
#
# Copyright 2005  Red Hat, Inc.
#
# Jeremy Katz <katzj@redhat.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; version 2 only
#
# 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 Library 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 os, sys
import rpm

import gtk
import gtk.glade
import gtk.gdk as gdk

from rhpl.translate import _, N_

from yum.constants import *

if os.access("data/yumhelpers.glade", os.R_OK):
    gygladefn = "data/yumhelpers.glade"
else:
    gygladefn = "/usr/share/pirut/ui/yumhelpers.glade"

# lame main loop runner... this should probably just be where we need it
def _runGtkMain(*args):
    while gtk.events_pending():
        gtk.main_iteration()

class PirutTransactionProgress:
    # FIXME: get the gtkmain crap out of this...
    def __init__(self, progress):
        self.progress = progress

        self.rpmFD = None
        self.total = None
        self.num = 0

        # stuff so that we can do the logging to the yum logfile
        self.filelog = None
        self.installed_pkg_names = []
        self.tsInfo = None

    def callback(self, what, amount, total, h, user):
        # this is the amount of the progress bar we use for the preparing step
        PREP = 0.25
        logmsg = ""
        
        if what == rpm.RPMCALLBACK_TRANS_START:
            if amount == 6:
                self.total = float(total)
                self.progress.set_markup("<i>%s</i>"
                                         %(_("Preparing transaction")))
        elif what == rpm.RPMCALLBACK_TRANS_PROGRESS:
            self.progress.set_fraction(amount * PREP / total)
        elif what == rpm.RPMCALLBACK_TRANS_STOP:
            self.progress.set_fraction(PREP) # arbitrary...
            self.progress.set_markup("")
        elif what == rpm.RPMCALLBACK_INST_OPEN_FILE:
            if h is not None:
                hdr, rpmloc = h
                self.rpmFD = os.open(rpmloc, os.O_RDONLY)
                self.progress.set_markup("<i>%s</i>"
                                         %(_("Updating %s") %(hdr['name'])))
                self.installed_pkg_names.append(hdr['name'])
                _runGtkMain()
                return self.rpmFD
        elif what == rpm.RPMCALLBACK_INST_CLOSE_FILE:
            os.close(self.rpmFD)
            self.progress.set_markup("")
            self.num += 1
            self.progress.set_fraction(self.num / self.total * (1 - PREP) + PREP)
            self.rpmFD = None

            hdr, rpmloc = h
            # logging shenanigans
            if hdr['epoch'] is not None:
                epoch = "%s" %(hdr['epoch'],)
            else:
                epoch = "0"
            (n,a,e,v,r) = hdr['name'], hdr['arch'], epoch, hdr['version'], hdr['release']
            pkg = "%s.%s %s-%s" %(n,a,v,r)
            if self.tsInfo:
                txmbr = self.tsInfo.getMembers(pkgtup = (n,a,e,v,r))[0]
                if txmbr.output_state == TS_UPDATE:
                    logmsg = _("Updated: %s") %(pkg,)
                else:
                    logmsg = _("Installed: %s") %(pkg,)                
            
        elif what == rpm.RPMCALLBACK_INST_PROGRESS:
            cur = self.progress.get_fraction()
            perpkg = 1 / self.total * (1 - PREP)
            pct = amount/float(total)
            new = self.num / self.total * (1 - PREP) + PREP + (perpkg * pct)
            if new - cur > 0.05:
                self.progress.set_fraction(new)
        elif what == rpm.RPMCALLBACK_UNINST_START:
            self.progress.set_markup("<i>%s</i>"
                                          %(_("Cleanup %s") %(h,),))
        elif what == rpm.RPMCALLBACK_UNINST_PROGRESS:
            cur = self.progress.get_fraction()
            perpkg = 1 / self.total * (1 - PREP)
            pct = amount/float(total)
            new = self.num / self.total * (1 - PREP) + PREP + (perpkg * pct)
            if new - cur > 0.05:
                self.progress.set_fraction(new)
        elif what == rpm.RPMCALLBACK_UNINST_STOP:
            self.num += 1
            self.progress.set_fraction(self.num / self.total * (1 - PREP) + PREP)
            if h not in self.installed_pkg_names:
                logmsg = _("Erased: %s") %(h,)
                
        _runGtkMain()
        if len(logmsg) > 0 and self.filelog:
            self.filelog(0, logmsg)

class PirutProgress:
    def __init__(self, title, parent = None):
        self.xml = gtk.glade.XML(gygladefn, domain="pirut")
        self.dialog = self.xml.get_widget("graphicalYumProgressDialog")
        if parent:
            self.dialog.set_modal(True)
            self.dialog.set_transient_for(parent)
            self.dialog.set_position(gtk.WIN_POS_CENTER_ON_PARENT)

        self.dialog.set_title(title)
        t = self.xml.get_widget("graphicalYumProgressTitle")
        t.set_markup("<span weight=\"bold\" size=\"x-large\">" + title +
                     "</span>")

        self.pbar = self.xml.get_widget("graphicalYumProgressBar")
        self.set_fraction(0.0)

        self.label = self.xml.get_widget("graphicalYumProgressLabel")
        self.set_markup("")

    def show(self):
        self.dialog.show()
        self.dialog.window.set_cursor(gdk.Cursor(gdk.WATCH))

    def destroy(self):
        if self.dialog.window is not None:
            self.dialog.window.set_cursor(None)
        self.dialog.destroy()

    def set_fraction(self, fract):
        self.pbar.set_fraction(fract)

    def get_fraction(self):
        return self.pbar.get_fraction()

    def set_markup(self, txt):
        self.label.set_markup(txt)

    def set_pbar_text(self, txt):
        self.pbar.set_text(txt)

class PirutProgressCallback(PirutProgress):
    def __init__(self, title, parent = None, log = None, num_tasks = 1):
        PirutProgress.__init__(self, title, parent)
        
        if log:
            self.log = log
        else:
            self.log = (lambda x: x)

        self.num_tasks = float(num_tasks)
        self.cur_task = 0
        self.this_task = 1

    def progressbar(self, current, total, name = None):
        pct = float(current) / total
        curval = self.pbar.get_fraction()
        newval = (pct * 1/self.num_tasks) * self.this_task + \
                 (self.cur_task / self.num_tasks)
        if newval > curval + 0.001:
            self.set_fraction(newval)
            _runGtkMain()            

    def next_task(self, incr = 1, next = 1):
        self.cur_task += incr
        self.this_task = next
        self.set_pbar_text("")
        self.set_fraction(self.cur_task / self.num_tasks)
        _runGtkMain()

Anon7 - 2021