Merge branch 'bug_262_no_more_ask_50hz' into experimental
[enigma2.git] / lib / python / Plugins / SystemPlugins / SoftwareManager / SoftwareTools.py
1 from enigma import eConsoleAppContainer
2 from Components.Console import Console
3 from Components.About import about
4 from Components.DreamInfoHandler import DreamInfoHandler
5 from Components.Language import language
6 from Components.Sources.List import List
7 from Components.Ipkg import IpkgComponent
8 from Components.Network import iNetwork
9 from Tools.Directories import pathExists, fileExists, resolveFilename, SCOPE_METADIR
10
11 from time import time
12
13 class SoftwareTools(DreamInfoHandler):
14         lastDownloadDate = None
15         NetworkConnectionAvailable = None
16         list_updating = False
17         available_updates = 0
18         available_updatelist  = []
19         available_packetlist  = []
20         installed_packetlist = {}
21
22         
23         def __init__(self):
24                 aboutInfo = about.getImageVersionString()
25                 if aboutInfo.startswith("dev-"):
26                         self.ImageVersion = 'Experimental'
27                 else:
28                         self.ImageVersion = 'Stable'
29                 self.language = language.getLanguage()[:2] # getLanguage returns e.g. "fi_FI" for "language_country"
30                 DreamInfoHandler.__init__(self, self.statusCallback, blocking = False, neededTag = 'ALL_TAGS', neededFlag = self.ImageVersion, language = self.language)
31                 self.directory = resolveFilename(SCOPE_METADIR)
32                 self.list = List([])
33                 self.NotifierCallback = None
34                 self.Console = Console()
35                 self.UpdateConsole = Console()
36                 self.cmdList = []
37                 self.unwanted_extensions = ('-dbg', '-dev', '-doc')
38                 self.ipkg = IpkgComponent()
39                 self.ipkg.addCallback(self.ipkgCallback)                
40
41         def statusCallback(self, status, progress):
42                 pass            
43
44         def startSoftwareTools(self, callback = None):
45                 if callback is not None:
46                         self.NotifierCallback = callback
47                 iNetwork.checkNetworkState(self.checkNetworkCB)
48                 
49         def checkNetworkCB(self,data):
50                 if data is not None:
51                         if data <= 2:
52                                 SoftwareTools.NetworkConnectionAvailable = True
53                                 self.getUpdates()
54                         else:
55                                 SoftwareTools.NetworkConnectionAvailable = False
56                                 self.getUpdates()
57
58         def getUpdates(self, callback = None):
59                 if SoftwareTools.NetworkConnectionAvailable == True:
60                         SoftwareTools.lastDownloadDate = time()
61                         if SoftwareTools.list_updating is False and callback is None:
62                                 SoftwareTools.list_updating = True
63                                 self.ipkg.startCmd(IpkgComponent.CMD_UPDATE)
64                         elif SoftwareTools.list_updating is False and callback is not None:
65                                 SoftwareTools.list_updating = True
66                                 self.NotifierCallback = callback
67                                 self.ipkg.startCmd(IpkgComponent.CMD_UPDATE)
68                         elif SoftwareTools.list_updating is True and callback is not None:
69                                 #update info collecting already in progress
70                                 self.NotifierCallback = callback
71                 else:
72                         SoftwareTools.list_updating = False
73                         if callback is not None:
74                                 callback(False)
75                         elif self.NotifierCallback is not None:
76                                 self.NotifierCallback(False)
77
78         def ipkgCallback(self, event, param):
79                 if event == IpkgComponent.EVENT_ERROR:
80                         SoftwareTools.list_updating = False
81                 elif event == IpkgComponent.EVENT_DONE:
82                         if SoftwareTools.list_updating:
83                                 self.startIpkgListAvailable()
84                 pass
85
86         def startIpkgListAvailable(self, callback = None):
87                 if callback is not None:
88                         SoftwareTools.list_updating = True
89                 if SoftwareTools.list_updating:
90                         if not self.UpdateConsole:
91                                 self.UpdateConsole = Console()
92                         cmd = "ipkg list"
93                         self.UpdateConsole.ePopen(cmd, self.IpkgListAvailableCB, callback)
94
95         def IpkgListAvailableCB(self, result, retval, extra_args = None):
96                 (callback) = extra_args
97                 if len(result):
98                         if SoftwareTools.list_updating:
99                                 SoftwareTools.available_packetlist = []
100                                 for x in result.splitlines():
101                                         split = x.split(' - ')
102                                         name = split[0].strip()
103                                         if not any(name.endswith(x) for x in self.unwanted_extensions):
104                                                 SoftwareTools.available_packetlist.append([name, split[1].strip(), split[2].strip()])
105                                 if callback is None:
106                                         self.startInstallMetaPackage()
107                                 else:
108                                         if self.UpdateConsole:
109                                                 if len(self.UpdateConsole.appContainers) == 0:
110                                                                 callback(True)
111                 else:
112                         SoftwareTools.list_updating = False
113                         if self.UpdateConsole:
114                                 if len(self.UpdateConsole.appContainers) == 0:
115                                         if callback is not None:
116                                                 callback(False)
117
118         def startInstallMetaPackage(self, callback = None):
119                 if callback is not None:
120                         SoftwareTools.list_updating = True
121                 if SoftwareTools.list_updating:
122                         if not self.UpdateConsole:
123                                 self.UpdateConsole = Console()
124                         cmd = "ipkg install enigma2-meta enigma2-plugins-meta enigma2-skins-meta"
125                         self.UpdateConsole.ePopen(cmd, self.InstallMetaPackageCB, callback)
126
127         def InstallMetaPackageCB(self, result, retval, extra_args = None):
128                 (callback) = extra_args
129                 if len(result):
130                         self.fillPackagesIndexList()
131                         if callback is None:
132                                 self.startIpkgListInstalled()
133                         else:
134                                 if self.UpdateConsole:
135                                         if len(self.UpdateConsole.appContainers) == 0:
136                                                         callback(True)
137                 else:
138                         SoftwareTools.list_updating = False
139                         if self.UpdateConsole:
140                                 if len(self.UpdateConsole.appContainers) == 0:
141                                         if callback is not None:
142                                                 callback(False)
143
144         def startIpkgListInstalled(self, callback = None):
145                 if callback is not None:
146                         SoftwareTools.list_updating = True
147                 if SoftwareTools.list_updating:
148                         if not self.UpdateConsole:
149                                 self.UpdateConsole = Console()
150                         cmd = "ipkg list_installed"
151                         self.UpdateConsole.ePopen(cmd, self.IpkgListInstalledCB, callback)
152
153         def IpkgListInstalledCB(self, result, retval, extra_args = None):
154                 (callback) = extra_args
155                 if len(result):
156                         SoftwareTools.installed_packetlist = {}
157                         for x in result.splitlines():
158                                 split = x.split(' - ')
159                                 name = split[0].strip()
160                                 if not any(name.endswith(x) for x in self.unwanted_extensions):
161                                         SoftwareTools.installed_packetlist[name] = split[1].strip()
162                         if callback is None:
163                                 self.countUpdates()
164                         else:
165                                 if self.UpdateConsole:
166                                         if len(self.UpdateConsole.appContainers) == 0:
167                                                         callback(True)
168                 else:
169                         SoftwareTools.list_updating = False
170                         if self.UpdateConsole:
171                                 if len(self.UpdateConsole.appContainers) == 0:
172                                         if callback is not None:
173                                                 callback(False)
174
175         def countUpdates(self, callback = None):
176                 SoftwareTools.available_updates = 0
177                 SoftwareTools.available_updatelist  = []
178                 for package in self.packagesIndexlist[:]:
179                         attributes = package[0]["attributes"]
180                         packagename = attributes["packagename"]
181                         for x in SoftwareTools.available_packetlist:
182                                 if x[0] == packagename:
183                                         if SoftwareTools.installed_packetlist.has_key(packagename):
184                                                 if SoftwareTools.installed_packetlist[packagename] != x[1]:
185                                                         SoftwareTools.available_updates +=1
186                                                         SoftwareTools.available_updatelist.append([packagename])
187
188                 SoftwareTools.list_updating = False
189                 if self.UpdateConsole:
190                         if len(self.UpdateConsole.appContainers) == 0:
191                                 if callback is not None:
192                                         callback(True)
193                                         callback = None
194                                 elif self.NotifierCallback is not None:
195                                         self.NotifierCallback(True)
196                                         self.NotifierCallback = None
197
198         def startIpkgUpdate(self, callback = None):
199                 if not self.Console:
200                         self.Console = Console()
201                 cmd = "ipkg update"
202                 self.Console.ePopen(cmd, self.IpkgUpdateCB, callback)
203
204         def IpkgUpdateCB(self, result, retval, extra_args = None):
205                 (callback) = extra_args
206                 if len(result):
207                         if self.Console:
208                                 if len(self.Console.appContainers) == 0:
209                                         if callback is not None:
210                                                 callback(True)
211                                                 callback = None
212
213         def cleanupSoftwareTools(self):
214                 if self.NotifierCallback is not None:
215                         self.NotifierCallback = None
216                 self.ipkg.stop()
217                 if self.Console is not None:
218                         if len(self.Console.appContainers):
219                                 for name in self.Console.appContainers.keys():
220                                         self.Console.kill(name)
221                 if self.UpdateConsole is not None:
222                         if len(self.UpdateConsole.appContainers):
223                                 for name in self.UpdateConsole.appContainers.keys():
224                                         self.UpdateConsole.kill(name)
225
226 iSoftwareTools = SoftwareTools()