add IpkgInstaller Media Scanner plugin
[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         
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 startCmd(self, cmd, args = None):
39                 if cmd == self.CMD_UPDATE:
40                         self.runCmd("update")
41                 elif cmd == self.CMD_UPGRADE:
42                         append = ""
43                         if args["test_only"]:
44                                 append = " -test"
45                         self.runCmd("upgrade" + append)
46                 elif cmd == self.CMD_LIST:
47                         self.fetchedList = []
48                         if args['installed_only']:
49                                 self.runCmd("list_installed")
50                         else:
51                                 self.runCmd("list")
52                 elif cmd == self.CMD_INSTALL:
53                         self.runCmd("install " + args['package'])
54                 self.setCurrentCommand(cmd)
55         
56         def cmdFinished(self, retval):
57                 self.callCallbacks(self.EVENT_DONE)
58         
59         def cmdData(self, data):
60                 print "data:", data
61                 if self.cache is None:
62                         self.cache = data
63                 else:
64                         self.cache += data
65
66                 if '\n' in data:
67                         splitcache = self.cache.split('\n')
68                         if self.cache[-1] == '\n':
69                                 iteration = splitcache
70                                 self.cache = None
71                         else:
72                                 iteration = splitcache[:-1]
73                                 self.cache = splitcache[-1]
74                         for mydata in iteration:
75                                 if mydata != '':
76                                         self.parseLine(mydata)
77                 
78         def parseLine(self, data):
79                 if self.currentCommand == self.CMD_LIST:
80                         item = data.split(' - ', 2)
81                         self.fetchedList.append(item)
82                         self.callCallbacks(self.EVENT_LISTITEM, item)
83                 else:
84                         if data.find('Downloading') == 0:
85                                 self.callCallbacks(self.EVENT_DOWNLOAD, data.split(' ', 5)[1].strip())
86                         elif data.find('Upgrading') == 0:
87                                 self.callCallbacks(self.EVENT_UPGRADE, data.split('    ', 1)[1].split(' ')[0])
88                         elif data.find('Installing') == 0:
89                                 self.callCallbacks(self.EVENT_INSTALL, data.split(' ', 1)[1].split(' ')[0])
90                         elif data.find('Configuring') == 0:
91                                 self.callCallbacks(self.EVENT_CONFIGURING, data.split(' ', 1)[1].split(' ')[0])
92                         elif data.find('An error occurred') == 0:
93                                 self.callCallbacks(self.EVENT_ERROR, None)
94                         elif data.find('Failed to download') == 0:
95                                 self.callCallbacks(self.EVENT_ERROR, None)
96                         elif data.find('ipkg_download: ERROR:') == 0:
97                                 self.callCallbacks(self.EVENT_ERROR, None)
98         def callCallbacks(self, event, param = None):
99                 for callback in self.callbackList:
100                         callback(event, param)
101
102         def addCallback(self, callback):
103                 self.callbackList.append(callback)
104                 
105         def getFetchedList(self):
106                 return self.fetchedList
107         
108         def stop(self):
109                 self.cmd.kill()
110                 
111         def isRunning(self):
112                 return self.cmd.running()