1 from enigma import eConsoleAppContainer
2 from Tools.Directories import fileExists
22 def __init__(self, ipkg = '/usr/bin/ipkg'):
24 self.opkgAvail = fileExists('/usr/bin/opkg')
25 self.cmd = eConsoleAppContainer()
27 self.callbackList = []
28 self.setCurrentCommand()
30 def setCurrentCommand(self, command = None):
31 self.currentCommand = command
33 def runCmd(self, cmd):
34 print "executing", self.ipkg, cmd
35 self.cmd.appClosed.append(self.cmdFinished)
36 self.cmd.dataAvail.append(self.cmdData)
37 if self.cmd.execute(self.ipkg + " " + cmd):
40 def startCmd(self, cmd, args = None):
41 if cmd == self.CMD_UPDATE:
43 elif cmd == self.CMD_UPGRADE:
47 self.runCmd("upgrade" + append)
48 elif cmd == self.CMD_LIST:
50 if args['installed_only']:
51 self.runCmd("list_installed")
54 elif cmd == self.CMD_INSTALL:
55 self.runCmd("install " + args['package'])
56 elif cmd == self.CMD_REMOVE:
57 self.runCmd("remove " + args['package'])
58 self.setCurrentCommand(cmd)
60 def cmdFinished(self, retval):
61 self.callCallbacks(self.EVENT_DONE)
62 self.cmd.appClosed.remove(self.cmdFinished)
63 self.cmd.dataAvail.remove(self.cmdData)
65 def cmdData(self, data):
67 if self.cache is None:
73 splitcache = self.cache.split('\n')
74 if self.cache[-1] == '\n':
75 iteration = splitcache
78 iteration = splitcache[:-1]
79 self.cache = splitcache[-1]
80 for mydata in iteration:
82 self.parseLine(mydata)
84 def parseLine(self, data):
85 if self.currentCommand == self.CMD_LIST:
86 item = data.split(' - ', 2)
87 self.fetchedList.append(item)
88 self.callCallbacks(self.EVENT_LISTITEM, item)
90 if data.find('Downloading') == 0:
91 self.callCallbacks(self.EVENT_DOWNLOAD, data.split(' ', 5)[1].strip())
92 elif data.find('Upgrading') == 0:
94 self.callCallbacks(self.EVENT_UPGRADE, data.split(' ', 1)[1].split(' ')[0])
96 self.callCallbacks(self.EVENT_UPGRADE, data.split(' ', 1)[1].split(' ')[0])
97 elif data.find('Installing') == 0:
98 self.callCallbacks(self.EVENT_INSTALL, data.split(' ', 1)[1].split(' ')[0])
99 elif data.find('Removing') == 0:
100 self.callCallbacks(self.EVENT_REMOVE, data.split(' ', 1)[1].split(' ')[1])
101 elif data.find('Configuring') == 0:
102 self.callCallbacks(self.EVENT_CONFIGURING, data.split(' ', 1)[1].split(' ')[0])
103 elif data.find('An error occurred') == 0:
104 self.callCallbacks(self.EVENT_ERROR, None)
105 elif data.find('Failed to download') == 0:
106 self.callCallbacks(self.EVENT_ERROR, None)
107 elif data.find('ipkg_download: ERROR:') == 0:
108 self.callCallbacks(self.EVENT_ERROR, None)
109 elif data.find(' Configuration file \'') >= 0:
110 # Note: the config file update question doesn't end with a newline, so
111 # if we get multiple config file update questions, the next ones
112 # don't necessarily start at the beginning of a line
113 self.callCallbacks(self.EVENT_MODIFIED, data.split(' \'', 1)[1][:-1])
115 def callCallbacks(self, event, param = None):
116 for callback in self.callbackList:
117 callback(event, param)
119 def addCallback(self, callback):
120 self.callbackList.append(callback)
122 def getFetchedList(self):
123 return self.fetchedList
129 return self.cmd.running()
131 def write(self, what):
133 # We except unterminated commands
135 self.cmd.write(what, len(what))