home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / AppInstall / PackageWorker.py < prev    next >
Encoding:
Python Source  |  2006-08-02  |  3.3 KB  |  103 lines

  1. # (c) 2005 Canonical, GPL
  2.  
  3. import apt_pkg
  4. import subprocess
  5. import gtk
  6. import thread
  7. import time
  8. import os
  9. import tempfile
  10. from gettext import gettext as _
  11.  
  12. class PackageWorker:
  13.     """
  14.     A class which does the actual package installing/removing.
  15.     """
  16.  
  17.     # synaptic actions
  18.     (INSTALL, UPDATE) = range(2)
  19.     
  20.     def run_synaptic(self, id, lock, to_add=None,to_rm=None, action=INSTALL):
  21.         #apt_pkg.PkgSystemUnLock()
  22.         #print "run_synaptic(%s,%s,%s)" % (id, lock, selections)
  23.         cmd = ["/usr/bin/gksu",
  24.                "--desktop", "/usr/share/applications/synaptic.desktop",
  25.                "--",
  26.                "/usr/sbin/synaptic",
  27.                "--hide-main-window",
  28.                "--non-interactive",
  29.                "--parent-window-id", "%s" % (id) ]
  30.  
  31.         # create tempfile for install (here because it must survive
  32.         # durng the synaptic call
  33.         f = tempfile.NamedTemporaryFile()
  34.         if action == self.INSTALL:
  35.             for item in to_add:
  36.                 f.write("%s\tinstall\n" % item.pkgname)
  37.                 #print item.pkgname
  38.             for item in to_rm:
  39.                 f.write("%s\tuninstall\n" % item.pkgname)
  40.             cmd.append("--set-selections-file")
  41.             cmd.append("%s" % f.name)
  42.             f.flush()
  43.         elif action == self.UPDATE:
  44.             #print "Updating..."
  45.             cmd.append("--update-at-startup")
  46.         self.return_code = subprocess.call(cmd)
  47.         lock.release()
  48.         f.close()
  49.  
  50.     def plug_removed(self, w, (win,socket)):
  51.         # plug was removed, but we don't want to get it removed, only hiden
  52.         # unti we get more
  53.         win.hide()
  54.         return True
  55.  
  56.     def plug_added(self, sock, win):
  57.         while gtk.events_pending():
  58.             win.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
  59.             win.show()
  60.             #print "huhu"
  61.             gtk.main_iteration()
  62.  
  63.     def get_plugged_win(self, window_main):
  64.         win = gtk.Window()
  65.         win.realize()
  66.         win.window.set_functions(gtk.gdk.FUNC_MOVE)
  67.         win.set_border_width(6)
  68.         win.set_transient_for(window_main)
  69.         win.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
  70.         win.set_title("")
  71.         win.set_resizable(False)
  72.         win.set_property("skip-taskbar-hint", True)
  73.         win.set_property("skip-taskbar-hint", True)
  74.         # prevent the window from closing with the delete button (there is
  75.         # a cancel button in the window)
  76.         win.connect("delete_event", lambda e,w: True);
  77.     
  78.         # create the socket
  79.         socket = gtk.Socket()
  80.         socket.show()
  81.         win.add(socket)
  82.         
  83.         socket.connect("plug-added", self.plug_added, win)
  84.         socket.connect("plug-removed", self.plug_removed, (win,socket))
  85.  
  86.         
  87.         return win, socket
  88.     
  89.     def perform_action(self, window_main, to_add=None, to_rm=None, action=INSTALL):
  90.         window_main.set_sensitive(False)
  91.         #plug_win, socket = self.get_plugged_win(window_main)
  92.         
  93.         lock = thread.allocate_lock()
  94.         lock.acquire()
  95.         t = thread.start_new_thread(self.run_synaptic,(window_main.window.xid,lock,to_add, to_rm, action))
  96.         while lock.locked():
  97.             while gtk.events_pending():
  98.                 gtk.main_iteration()
  99.             time.sleep(0.05)
  100.         #plug_win.destroy()
  101.         window_main.set_sensitive(True)
  102.         return self.return_code
  103.