1 from enigma import eConsoleAppContainer
2 from Tools.Directories import fileExists
22 def __init__(self, ipkg = 'opkg'):
24 self.cmd = eConsoleAppContainer()
26 self.callbackList = []
27 self.setCurrentCommand()
29 def setCurrentCommand(self, command = None):
30 self.currentCommand = command
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):
39 def startCmd(self, cmd, args = None):
40 if cmd == self.CMD_UPDATE:
42 elif cmd == self.CMD_UPGRADE:
46 self.runCmd("upgrade" + append)
47 elif cmd == self.CMD_LIST:
49 if args['installed_only']:
50 self.runCmd("list_installed")
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)
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)
64 def cmdData(self, data):
66 if self.cache is None:
72 splitcache = self.cache.split('\n')
73 if self.cache[-1] == '\n':
74 iteration = splitcache
77 iteration = splitcache[:-1]
78 self.cache = splitcache[-1]
79 for mydata in iteration:
81 self.parseLine(mydata)
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)
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])
111 def callCallbacks(self, event, param = None):
112 for callback in self.callbackList:
113 callback(event, param)
115 def addCallback(self, callback):
116 self.callbackList.append(callback)
118 def getFetchedList(self):
119 return self.fetchedList
125 return self.cmd.running()
127 def write(self, what):
129 # We except unterminated commands
131 self.cmd.write(what, len(what))