refs bug #429
[enigma2.git] / lib / python / Components / Ipkg.py
old mode 100644 (file)
new mode 100755 (executable)
index dbc9965..7144777
@@ -1,4 +1,5 @@
 from enigma import eConsoleAppContainer
+from Tools.Directories import fileExists
 
 class IpkgComponent:
        EVENT_INSTALL = 0
@@ -10,6 +11,7 @@ class IpkgComponent:
        EVENT_LISTITEM = 9
        EVENT_DONE = 10
        EVENT_ERROR = 11
+       EVENT_MODIFIED = 12
        
        CMD_INSTALL = 0
        CMD_LIST = 1
@@ -19,12 +21,9 @@ class IpkgComponent:
        
        def __init__(self, ipkg = '/usr/bin/ipkg'):
                self.ipkg = ipkg
-               
+               self.opkgAvail = fileExists('/usr/bin/opkg')
                self.cmd = eConsoleAppContainer()
-               self.cmd.appClosed.get().append(self.cmdFinished)
-               self.cmd.dataAvail.get().append(self.cmdData)
                self.cache = None
-               
                self.callbackList = []
                self.setCurrentCommand()
                
@@ -33,8 +32,11 @@ class IpkgComponent:
                
        def runCmd(self, cmd):
                print "executing", self.ipkg, cmd
-               self.cmd.execute(self.ipkg + " " + cmd)
-               
+               self.cmd.appClosed.append(self.cmdFinished)
+               self.cmd.dataAvail.append(self.cmdData)
+               if self.cmd.execute(self.ipkg + " " + cmd):
+                       self.cmdFinished(-1)
+
        def startCmd(self, cmd, args = None):
                if cmd == self.CMD_UPDATE:
                        self.runCmd("update")
@@ -51,11 +53,15 @@ class IpkgComponent:
                                self.runCmd("list")
                elif cmd == self.CMD_INSTALL:
                        self.runCmd("install " + args['package'])
+               elif cmd == self.CMD_REMOVE:
+                       self.runCmd("remove " + args['package'])
                self.setCurrentCommand(cmd)
        
        def cmdFinished(self, retval):
                self.callCallbacks(self.EVENT_DONE)
-       
+               self.cmd.appClosed.remove(self.cmdFinished)
+               self.cmd.dataAvail.remove(self.cmdData)
+
        def cmdData(self, data):
                print "data:", data
                if self.cache is None:
@@ -84,9 +90,14 @@ class IpkgComponent:
                        if data.find('Downloading') == 0:
                                self.callCallbacks(self.EVENT_DOWNLOAD, data.split(' ', 5)[1].strip())
                        elif data.find('Upgrading') == 0:
-                               self.callCallbacks(self.EVENT_UPGRADE, data.split('    ', 1)[1].split(' ')[0])
+                               if self.opkgAvail:
+                                       self.callCallbacks(self.EVENT_UPGRADE, data.split(' ', 1)[1].split(' ')[0])
+                               else:
+                                       self.callCallbacks(self.EVENT_UPGRADE, data.split('    ', 1)[1].split(' ')[0])
                        elif data.find('Installing') == 0:
                                self.callCallbacks(self.EVENT_INSTALL, data.split(' ', 1)[1].split(' ')[0])
+                       elif data.find('Removing') == 0:
+                               self.callCallbacks(self.EVENT_REMOVE, data.split(' ', 1)[1].split(' ')[1])
                        elif data.find('Configuring') == 0:
                                self.callCallbacks(self.EVENT_CONFIGURING, data.split(' ', 1)[1].split(' ')[0])
                        elif data.find('An error occurred') == 0:
@@ -95,6 +106,12 @@ class IpkgComponent:
                                self.callCallbacks(self.EVENT_ERROR, None)
                        elif data.find('ipkg_download: ERROR:') == 0:
                                self.callCallbacks(self.EVENT_ERROR, None)
+                       elif data.find('    Configuration file \'') >= 0:
+                               # Note: the config file update question doesn't end with a newline, so
+                               # if we get multiple config file update questions, the next ones
+                               # don't necessarily start at the beginning of a line
+                               self.callCallbacks(self.EVENT_MODIFIED, data.split(' \'', 1)[1][:-1])
+
        def callCallbacks(self, event, param = None):
                for callback in self.callbackList:
                        callback(event, param)
@@ -110,3 +127,9 @@ class IpkgComponent:
                
        def isRunning(self):
                return self.cmd.running()
+
+       def write(self, what):
+               if what:
+                       # We except unterminated commands
+                       what += "\n"
+                       self.cmd.write(what, len(what))