support for a final post-destroy value
[enigma2.git] / lib / python / Components / Ipkg.py
1 from enigma import eConsoleAppContainer
2
3 class IpkgComponent:
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         EVENT_MODIFIED = 12
14         
15         CMD_INSTALL = 0
16         CMD_LIST = 1
17         CMD_REMOVE = 2
18         CMD_UPDATE = 3
19         CMD_UPGRADE = 4
20         
21         def __init__(self, ipkg = '/usr/bin/ipkg'):
22                 self.ipkg = ipkg
23                 
24                 self.cmd = eConsoleAppContainer()
25                 self.cache = None
26                 self.callbackList = []
27                 self.setCurrentCommand()
28                 
29         def setCurrentCommand(self, command = None):
30                 self.currentCommand = command
31                 
32         def runCmd(self, cmd):
33                 print "executing", self.ipkg, cmd
34                 self.cmd.appClosed.append(self.cmdFinished)
35                 self.cmd.dataAvail.append(self.cmdData)
36                 if self.cmd.execute(self.ipkg + " " + cmd):
37                         self.cmdFinished(-1)
38
39         def startCmd(self, cmd, args = None):
40                 if cmd == self.CMD_UPDATE:
41                         self.runCmd("update")
42                 elif cmd == self.CMD_UPGRADE:
43                         append = ""
44                         if args["test_only"]:
45                                 append = " -test"
46                         self.runCmd("upgrade" + append)
47                 elif cmd == self.CMD_LIST:
48                         self.fetchedList = []
49                         if args['installed_only']:
50                                 self.runCmd("list_installed")
51                         else:
52                                 self.runCmd("list")
53                 elif cmd == self.CMD_INSTALL:
54                         self.runCmd("install " + args['package'])
55                 elif cmd == self.CMD_REMOVE:
56                         self.runCmd("remove " + args['package'])
57                 self.setCurrentCommand(cmd)
58         
59         def cmdFinished(self, retval):
60                 self.callCallbacks(self.EVENT_DONE)
61                 self.cmd.appClosed.remove(self.cmdFinished)
62                 self.cmd.dataAvail.remove(self.cmdData)
63
64         def cmdData(self, data):
65                 print "data:", data
66                 if self.cache is None:
67                         self.cache = data
68                 else:
69                         self.cache += data
70
71                 if '\n' in data:
72                         splitcache = self.cache.split('\n')
73                         if self.cache[-1] == '\n':
74                                 iteration = splitcache
75                                 self.cache = None
76                         else:
77                                 iteration = splitcache[:-1]
78                                 self.cache = splitcache[-1]
79                         for mydata in iteration:
80                                 if mydata != '':
81                                         self.parseLine(mydata)
82                 
83         def parseLine(self, data):
84                 if self.currentCommand == self.CMD_LIST:
85                         item = data.split(' - ', 2)
86                         self.fetchedList.append(item)
87                         self.callCallbacks(self.EVENT_LISTITEM, item)
88                 else:
89                         if data.find('Downloading') == 0:
90                                 self.callCallbacks(self.EVENT_DOWNLOAD, data.split(' ', 5)[1].strip())
91                         elif data.find('Upgrading') == 0:
92                                 self.callCallbacks(self.EVENT_UPGRADE, data.split('    ', 1)[1].split(' ')[0])
93                         elif data.find('Installing') == 0:
94                                 self.callCallbacks(self.EVENT_INSTALL, data.split(' ', 1)[1].split(' ')[0])
95                         elif data.find('Removing') == 0:
96                                 self.callCallbacks(self.EVENT_REMOVE, data.split(' ', 1)[1].split(' ')[1])
97                         elif data.find('Configuring') == 0:
98                                 self.callCallbacks(self.EVENT_CONFIGURING, data.split(' ', 1)[1].split(' ')[0])
99                         elif data.find('An error occurred') == 0:
100                                 self.callCallbacks(self.EVENT_ERROR, None)
101                         elif data.find('Failed to download') == 0:
102                                 self.callCallbacks(self.EVENT_ERROR, None)
103                         elif data.find('ipkg_download: ERROR:') == 0:
104                                 self.callCallbacks(self.EVENT_ERROR, None)
105                         elif data.find('    Configuration file \'') >= 0:
106                                 # Note: the config file update question doesn't end with a newline, so
107                                 # if we get multiple config file update questions, the next ones
108                                 # don't necessarily start at the beginning of a line
109                                 self.callCallbacks(self.EVENT_MODIFIED, data.split(' \'', 1)[1][:-1])
110
111         def callCallbacks(self, event, param = None):
112                 for callback in self.callbackList:
113                         callback(event, param)
114
115         def addCallback(self, callback):
116                 self.callbackList.append(callback)
117                 
118         def getFetchedList(self):
119                 return self.fetchedList
120         
121         def stop(self):
122                 self.cmd.kill()
123                 
124         def isRunning(self):
125                 return self.cmd.running()
126
127         def write(self, what):
128                 if what:
129                         # We except unterminated commands
130                         what += "\n"
131                         self.cmd.write(what, len(what))