replace software update plugin with a more user friendly approach
[vuplus_dvbapp] / lib / python / Components / Ipkg.py
1 from enigma import eConsoleAppContainer
2
3 class Ipkg:
4         EVENT_INSTALL = 0
5         EVENT_DOWNLOAD = 1
6         EVENT_INFLATING = 2
7         EVENT_CONFIGURING = 3
8         EVENT_REMOVE = 4
9         EVENT_UPGRADE = 5
10         EVENT_LISTITEM = 9
11         EVENT_DONE = 10
12         EVENT_ERROR = 11
13         
14         CMD_INSTALL = 0
15         CMD_LIST = 1
16         CMD_REMOVE = 2
17         CMD_UPDATE = 3
18         CMD_UPGRADE = 4
19         
20         def __init__(self, ipkg = '/usr/bin/ipkg'):
21                 self.ipkg = ipkg
22                 
23                 self.cmd = eConsoleAppContainer()
24                 self.cmd.appClosed.get().append(self.cmdFinished)
25                 self.cmd.dataAvail.get().append(self.cmdData)
26                 self.cache = None
27                 
28                 self.callbackList = []
29                 self.setCurrentCommand()
30                 
31         def setCurrentCommand(self, command = None):
32                 self.currentCommand = command
33                 
34         def runCmd(self, cmd):
35                 print "executing", self.ipkg, cmd
36                 self.cmd.execute(self.ipkg + " " + cmd)
37                 
38         def cmdFetchList(self, installed_only = False):
39                 self.fetchedList = []
40                 if installed_only:
41                         self.runCmd("list_installed")
42                 else:
43                         self.runCmd("list")
44                 self.setCurrentCommand(self.CMD_LIST)
45                 
46         def cmdUpgrade(self, test_only = False):
47                 append = ""
48                 if test_only:
49                         append = " -test"
50                 self.runCmd("upgrade" + append)
51                 self.setCurrentCommand(self.CMD_UPGRADE)
52                 
53         def cmdUpdate(self):
54                 self.runCmd("update")
55                 self.setCurrentCommand(self.CMD_UPDATE)
56                 
57         def cmdFinished(self, retval):
58                 self.callCallbacks(self.EVENT_DONE)
59         
60         def cmdData(self, data):
61                 print "data:", data
62                 if self.cache is None:
63                         self.cache = data
64                 else:
65                         self.cache += data
66
67                 if '\n' in data:
68                         splitcache = self.cache.split('\n')
69                         if self.cache[-1] == '\n':
70                                 iteration = splitcache
71                                 self.cache = None
72                         else:
73                                 iteration = splitcache[:-1]
74                                 self.cache = splitcache[-1]
75                         for mydata in iteration:
76                                 if mydata != '':
77                                         self.parseLine(mydata)
78                 
79         def parseLine(self, data):
80                 if self.currentCommand == self.CMD_LIST:
81                         item = data.split(' - ', 2)
82                         self.fetchedList.append(item)
83                         self.callCallbacks(self.EVENT_LISTITEM, item)
84                 else:
85                         if data.find('Downloading') == 0:
86                                 self.callCallbacks(self.EVENT_DOWNLOAD, data.split(' ', 5)[1].strip())
87                         elif data.find('Upgrading') == 0:
88                                 self.callCallbacks(self.EVENT_UPGRADE, data.split('    ', 1)[1].split(' ')[0])
89                         elif data.find('Installing') == 0:
90                                 self.callCallbacks(self.EVENT_INSTALL, data.split(' ', 1)[1].split(' ')[0])
91                         elif data.find('Configuring') == 0:
92                                 self.callCallbacks(self.EVENT_CONFIGURING, data.split(' ', 1)[1].split(' ')[0])
93                         elif data.find('An error occurred') == 0:
94                                 self.callCallbacks(self.EVENT_ERROR, None)
95                         elif data.find('Failed to download') == 0:
96                                 self.callCallbacks(self.EVENT_ERROR, None)
97                         elif data.find('ipkg_download: ERROR:') == 0:
98                                 self.callCallbacks(self.EVENT_ERROR, None)
99         def callCallbacks(self, event, param = None):
100                 for callback in self.callbackList:
101                         callback(event, param)
102
103         def addCallback(self, callback):
104                 self.callbackList.append(callback)
105                 
106         def getFetchedList(self):
107                 return self.fetchedList