home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / GnomeCodecInstall / PackageWorker.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.4 KB  |  100 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import apt
  5. import subprocess
  6. import gtk
  7. import gtk.gdk as gtk
  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.  
  23. class PackageWorker(object):
  24.     '''
  25.     A class which does the actual package installing/removing.
  26.     '''
  27.     (INSTALL, UPDATE) = range(2)
  28.     
  29.     def run_synaptic(self, id, lock, to_add = None, to_rm = None, action = INSTALL):
  30.         cmd = []
  31.         if os.getuid() != 0:
  32.             cmd = [
  33.                 '/usr/bin/gksu',
  34.                 '--desktop',
  35.                 '/usr/share/applications/synaptic.desktop',
  36.                 '--']
  37.         
  38.         cmd += [
  39.             '/usr/sbin/synaptic',
  40.             '--hide-main-window',
  41.             '--non-interactive',
  42.             '-o',
  43.             'Synaptic::closeZvt=true',
  44.             '--parent-window-id',
  45.             '%s' % id]
  46.         f = tempfile.NamedTemporaryFile()
  47.         if action == self.INSTALL:
  48.             for item in to_add:
  49.                 f.write('%s\tinstall\n' % item)
  50.             
  51.             for item in to_rm:
  52.                 f.write('%s\tuninstall\n' % item)
  53.             
  54.             cmd.append('--set-selections-file')
  55.             cmd.append('%s' % f.name)
  56.             f.flush()
  57.         elif action == self.UPDATE:
  58.             cmd.append('--update-at-startup')
  59.         
  60.         self.return_code = subprocess.call(cmd)
  61.         lock.release()
  62.         f.close()
  63.  
  64.     
  65.     def perform_action(self, window_main, to_add = None, to_rm = None):
  66.         ''' 
  67.         install/remove the given set of packages 
  68.         
  69.         return True on success 
  70.                False if any of the actions could not be performed
  71.         '''
  72.         window_main.set_sensitive(False)
  73.         window_main.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
  74.         lock = thread.allocate_lock()
  75.         lock.acquire()
  76.         t = thread.start_new_thread(self.run_synaptic, (window_main.window.xid, lock, to_add, to_rm, self.INSTALL))
  77.         while lock.locked():
  78.             while gtk.events_pending():
  79.                 gtk.main_iteration()
  80.             time.sleep(0.05)
  81.         result = True
  82.         cache = apt.Cache(GtkOpProgress())
  83.         for pkgname in to_add:
  84.             if not cache[pkgname].isInstalled:
  85.                 result = False
  86.                 break
  87.                 continue
  88.         
  89.         for pkgname in to_rm:
  90.             if cache[pkgname].isInstalled:
  91.                 result = False
  92.                 break
  93.                 continue
  94.         
  95.         window_main.set_sensitive(True)
  96.         window_main.window.set_cursor(None)
  97.         return result
  98.  
  99.  
  100.