home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / GnomeCodecInstall / PackageWorker.py < prev   
Encoding:
Python Source  |  2009-03-17  |  3.0 KB  |  96 lines

  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2005-2007 Canonical, GPL
  3.  
  4. import apt
  5. import subprocess
  6. import gtk
  7. import gtk.gdk
  8. import thread
  9. import time
  10. import os
  11. import tempfile
  12. from gettext import gettext as _
  13.  
  14. class GtkOpProgress(apt.progress.OpProgress):
  15.   " a simple helper that keeps the GUI alive "
  16.  
  17.   def update(self, percent):
  18.     while gtk.events_pending():
  19.       gtk.main_iteration()
  20.  
  21.  
  22. class PackageWorker(object):
  23.     """
  24.     A class which does the actual package installing/removing.
  25.     """
  26.     # synaptic actions
  27.     (INSTALL, UPDATE) = range(2)
  28.  
  29.     def run_synaptic(self, id, lock, to_add=None,to_rm=None, action=INSTALL):
  30.         #print "run_synaptic(%s,%s,%s)" % (id, lock, selections)
  31.         cmd = []
  32.         if os.getuid() != 0:
  33.             cmd = ["/usr/bin/gksu",
  34.                    "--desktop", "/usr/share/applications/synaptic.desktop",
  35.                    "--"]
  36.         cmd += ["/usr/sbin/synaptic",
  37.                 "--hide-main-window",
  38.                 "--non-interactive",
  39.                 "-o", "Synaptic::closeZvt=true",
  40.                 "--parent-window-id", "%s" % (id) ]
  41.  
  42.         # create tempfile for install (here because it must survive
  43.         # durng the synaptic call
  44.         f = tempfile.NamedTemporaryFile()
  45.         if action == self.INSTALL:
  46.             # install the stuff
  47.             for item in to_add:
  48.                 f.write("%s\tinstall\n" % item)
  49.                 #print item.pkgname
  50.             for item in to_rm:
  51.                 f.write("%s\tuninstall\n" % item)
  52.             cmd.append("--set-selections-file")
  53.             cmd.append("%s" % f.name)
  54.             f.flush()
  55.         elif action == self.UPDATE:
  56.             #print "Updating..."
  57.             cmd.append("--update-at-startup")
  58.         self.return_code = subprocess.call(cmd)
  59.         lock.release()
  60.         f.close()
  61.  
  62.    
  63.     def perform_action(self, window_main, to_add=None, to_rm=None):
  64.         """ 
  65.         install/remove the given set of packages 
  66.         
  67.         return True on success 
  68.                False if any of the actions could not be performed
  69.         """
  70.         window_main.set_sensitive(False)
  71.         window_main.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
  72.         lock = thread.allocate_lock()
  73.         lock.acquire()
  74.         t = thread.start_new_thread(self.run_synaptic,(window_main.window.xid,lock,to_add, to_rm, self.INSTALL))
  75.         while lock.locked():
  76.             while gtk.events_pending():
  77.                 gtk.main_iteration()
  78.             time.sleep(0.05)
  79.  
  80.         # check if the requested package really got installed
  81.         # we can not use the exit code here because gksu does
  82.         # not transport the exit code over
  83.         result = True
  84.         cache = apt.Cache(GtkOpProgress())
  85.         for pkgname in to_add:
  86.             if not cache[pkgname].isInstalled:
  87.                 result = False
  88.                 break
  89.         for pkgname in to_rm:
  90.             if cache[pkgname].isInstalled:
  91.                 result = False
  92.                 break
  93.         window_main.set_sensitive(True)
  94.         window_main.window.set_cursor(None)
  95.         return result
  96.