remove unnecessary debug and fix typo bug
[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.get().append(self.cmdFinished)
35                 self.cmd.dataAvail.get().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                 self.setCurrentCommand(cmd)
56         
57         def cmdFinished(self, retval):
58                 self.callCallbacks(self.EVENT_DONE)
59                 self.cmd.appClosed.get().remove(self.cmdFinished)
60                 self.cmd.dataAvail.get().remove(self.cmdData)
61
62         def cmdData(self, data):
63                 print "data:", data
64                 if self.cache is None:
65                         self.cache = data
66                 else:
67                         self.cache += data
68
69                 if '\n' in data:
70                         splitcache = self.cache.split('\n')
71                         if self.cache[-1] == '\n':
72                                 iteration = splitcache
73                                 self.cache = None
74                         else:
75                                 iteration = splitcache[:-1]
76                                 self.cache = splitcache[-1]
77                         for mydata in iteration:
78                                 if mydata != '':
79                                         self.parseLine(mydata)
80                 
81         def parseLine(self, data):
82                 if self.currentCommand == self.CMD_LIST:
83                         item = data.split(' - ', 2)
84                         self.fetchedList.append(item)
85                         self.callCallbacks(self.EVENT_LISTITEM, item)
86                 else:
87                         if data.find('Downloading') == 0:
88                                 self.callCallbacks(self.EVENT_DOWNLOAD, data.split(' ', 5)[1].strip())
89                         elif data.find('Upgrading') == 0:
90                                 self.callCallbacks(self.EVENT_UPGRADE, data.split('    ', 1)[1].split(' ')[0])
91                         elif data.find('Installing') == 0:
92                                 self.callCallbacks(self.EVENT_INSTALL, data.split(' ', 1)[1].split(' ')[0])
93                         elif data.find('Configuring') == 0:
94                                 self.callCallbacks(self.EVENT_CONFIGURING, data.split(' ', 1)[1].split(' ')[0])
95                         elif data.find('An error occurred') == 0:
96                                 self.callCallbacks(self.EVENT_ERROR, None)
97                         elif data.find('Failed to download') == 0:
98                                 self.callCallbacks(self.EVENT_ERROR, None)
99                         elif data.find('ipkg_download: ERROR:') == 0:
100                                 self.callCallbacks(self.EVENT_ERROR, None)
101                         elif data.find('    Configuration file \'') >= 0:
102                                 # Note: the config file update question doesn't end with a newline, so
103                                 # if we get multiple config file update questions, the next ones
104                                 # don't necessarily start at the beginning of a line
105                                 self.callCallbacks(self.EVENT_MODIFIED, data.split(' \'', 1)[1][:-1])
106
107         def callCallbacks(self, event, param = None):
108                 for callback in self.callbackList:
109                         callback(event, param)
110
111         def addCallback(self, callback):
112                 self.callbackList.append(callback)
113                 
114         def getFetchedList(self):
115                 return self.fetchedList
116         
117         def stop(self):
118                 self.cmd.kill()
119                 
120         def isRunning(self):
121                 return self.cmd.running()
122
123         def write(self, what):
124                 if what:
125                         # We except unterminated commands
126                         what += "\n"
127                         self.cmd.write(what, len(what))