2 from Tools.Directories import crawlDirectory, resolveFilename, SCOPE_CONFIG, SCOPE_SKIN
3 from Components.NimManager import nimmanager
4 from Components.Ipkg import IpkgComponent
5 from Components.config import config, configfile
6 from enigma import eConsoleAppContainer, eDVBDB
9 class InfoHandlerParseError(Exception):
10 def __init__(self, value):
13 return repr(self.value)
15 class InfoHandler(xml.sax.ContentHandler):
16 def __init__(self, prerequisiteMet, directory):
18 self.directory = directory
20 self.globalprerequisites = {}
21 self.prerequisites = {}
23 self.validFileTypes = ["skin", "config", "services", "favourites", "package"]
24 self.prerequisitesMet = prerequisiteMet
27 def printError(self, error):
28 print "Error in defaults xml files:", error
29 raise InfoHandlerParseError, error
31 def startElement(self, name, attrs):
32 #print name, ":", attrs.items()
33 self.elements.append(name)
34 if name in ["hardware", "bcastsystem", "satellite", "tag"]:
35 if not attrs.has_key("type"):
36 self.printError(str(name) + " tag with no type attribute")
37 if self.elements[-3] == "default":
38 prerequisites = self.globalprerequisites
40 prerequisites = self.prerequisites
41 if not prerequisites.has_key(name):
42 prerequisites[name] = []
43 prerequisites[name].append(str(attrs["type"]))
45 if attrs.has_key("type"):
46 if attrs["type"] == "directories":
47 self.attributes["filestype"] = "directories"
48 # TODO add a compressed archive type
50 self.prerequisites = {}
51 if not attrs.has_key("type"):
52 self.printError("file tag with no type attribute")
54 if not attrs.has_key("name"):
55 self.printError("file tag with no name attribute")
57 if not attrs.has_key("directory"):
58 directory = self.directory
60 if not type in self.validFileTypes:
61 self.printError("file tag with invalid type attribute")
64 self.fileattrs = attrs
65 def endElement(self, name):
67 #print "self.elements:", self.elements
70 #print "prerequisites:", self.prerequisites
71 if len(self.prerequisites) == 0 or self.prerequisitesMet(self.prerequisites):
72 if not self.attributes.has_key(self.filetype):
73 self.attributes[self.filetype] = []
74 if self.fileattrs.has_key("directory"):
75 directory = str(self.fileattrs["directory"])
76 if len(directory) < 1 or directory[0] != "/":
77 directory = self.directory + directory
79 directory = self.directory
80 self.attributes[self.filetype].append({ "name": str(self.fileattrs["name"]), "directory": directory })
83 self.list.append({"attributes": self.attributes, 'prerequisites': self.globalprerequisites})
85 self.globalprerequisites = {}
87 def characters(self, data):
88 if self.elements[-1] == "author":
89 self.attributes["author"] = str(data)
90 if self.elements[-1] == "name":
91 self.attributes["name"] = str(data)
92 #print "characters", data
94 class DreamInfoHandler:
100 def __init__(self, statusCallback, blocking = False, neededTag = None):
103 self.neededTag = neededTag
105 # caution: blocking should only be used, if further execution in enigma2 depends on the outcome of
107 self.blocking = blocking
109 self.currentlyInstallingMetaIndex = None
111 self.console = eConsoleAppContainer()
112 self.console.appClosed.get().append(self.installNext)
114 self.statusCallback = statusCallback
115 self.setStatus(self.STATUS_INIT)
117 self.packageslist = []
119 def readInfo(self, directory, file):
120 print "Reading .info file", file
121 handler = InfoHandler(self.prerequisiteMet, directory)
123 xml.sax.parse(file, handler)
124 for entry in handler.list:
125 self.packageslist.append((entry,file))
126 except InfoHandlerParseError:
127 print "file", file, "ignored due to errors in the file"
130 # prerequisites = True: give only packages matching the prerequisites
131 def fillPackagesList(self, prerequisites = True):
132 self.packageslist = []
134 if not isinstance(self.directory, list):
135 self.directory = [self.directory]
137 for directory in self.directory:
138 packages += crawlDirectory(directory, ".*\.info$")
140 for package in packages:
141 self.readInfo(package[0] + "/", package[0] + "/" + package[1])
144 for package in self.packageslist[:]:
145 if not self.prerequisiteMet(package[0]["prerequisites"]):
146 self.packageslist.remove(package)
147 return self.packageslist
149 def prerequisiteMet(self, prerequisites):
150 # TODO: we need to implement a hardware detection here...
151 print "prerequisites:", prerequisites
153 if self.neededTag is None:
154 if prerequisites.has_key("tag"):
157 if prerequisites.has_key("tag"):
158 if not self.neededTag in prerequisites["tag"]:
161 if prerequisites.has_key("satellite"):
162 for sat in prerequisites["satellite"]:
163 if int(sat) not in nimmanager.getConfiguredSats():
165 if prerequisites.has_key("bcastsystem"):
166 for bcastsystem in prerequisites["bcastsystem"]:
167 if nimmanager.hasNimType(bcastsystem):
170 if prerequisites.has_key("hardware"):
171 for hardware in prerequisites["hardware"]:
172 # TODO: hardware detection
176 def installPackages(self, indexes):
177 print "installing packages", indexes
178 if len(indexes) == 0:
179 self.setStatus(self.STATUS_DONE)
181 self.installIndexes = indexes
182 print "+++++++++++++++++++++++bla"
183 self.currentlyInstallingMetaIndex = 0
184 self.installPackage(self.installIndexes[self.currentlyInstallingMetaIndex])
186 def installPackage(self, index):
187 print "installing package with index", index, "and name", self.packageslist[index][0]["attributes"]["name"]
189 attributes = self.packageslist[index][0]["attributes"]
190 self.installingAttributes = attributes
191 self.attributeNames = ["skin", "config", "favourites", "package", "services"]
192 self.currentAttributeIndex = 0
193 self.currentIndex = -1
196 def setStatus(self, status):
198 self.statusCallback(self.status, None)
200 def installNext(self, *args, **kwargs):
201 self.currentIndex += 1
202 attributes = self.installingAttributes
203 #print "attributes:", attributes
205 if self.currentAttributeIndex >= len(self.attributeNames): # end of package reached
206 print "end of package reached"
207 if self.currentlyInstallingMetaIndex is None or self.currentlyInstallingMetaIndex >= len(self.installIndexes) - 1:
208 print "set status to DONE"
209 self.setStatus(self.STATUS_DONE)
212 print "increment meta index to install next package"
213 self.currentlyInstallingMetaIndex += 1
214 self.installPackage(self.installIndexes[self.currentlyInstallingMetaIndex])
216 self.setStatus(self.STATUS_WORKING)
218 currentAttribute = self.attributeNames[self.currentAttributeIndex]
220 print "installing", currentAttribute, "with index", self.currentIndex
222 if attributes.has_key(currentAttribute):
223 if self.currentIndex >= len(attributes[currentAttribute]): # all jobs done for current attribute
224 self.currentIndex = -1
225 self.currentAttributeIndex += 1
228 else: # nothing to install here
229 self.currentIndex = -1
230 self.currentAttributeIndex += 1
234 if currentAttribute == "skin":
235 skin = attributes["skin"][self.currentIndex]
236 self.installSkin(skin["directory"], skin["name"])
237 elif currentAttribute == "config":
238 if self.currentIndex == 0:
239 from Components.config import configfile
241 config = attributes["config"][self.currentIndex]
242 self.mergeConfig(config["directory"], config["name"])
243 elif currentAttribute == "favourites":
244 favourite = attributes["favourites"][self.currentIndex]
245 self.installFavourites(favourite["directory"], favourite["name"])
246 elif currentAttribute == "package":
247 package = attributes["package"][self.currentIndex]
248 self.installIPK(package["directory"], package["name"])
249 elif currentAttribute == "services":
250 service = attributes["services"][self.currentIndex]
251 self.mergeServices(service["directory"], service["name"])
253 def readfile(self, filename):
254 if not os.path.isfile(filename):
257 lines = fd.readlines()
261 def mergeConfig(self, directory, name, merge = True):
262 print "merging config:", directory, " - ", name
263 if os.path.isfile(directory + name):
264 config.loadFromFile(directory + name)
268 def installIPK(self, directory, name):
270 os.system("ipkg install " + directory + name)
273 self.ipkg = IpkgComponent()
274 self.ipkg.addCallback(self.ipkgCallback)
275 self.ipkg.startCmd(IpkgComponent.CMD_INSTALL, {'package': directory + name})
277 def ipkgCallback(self, event, param):
279 if event == IpkgComponent.EVENT_DONE:
281 elif event == IpkgComponent.EVENT_ERROR:
284 def installSkin(self, directory, name):
285 print "installing skin:", directory, " - ", name
286 print "cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN))
288 os.system("cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN)))
291 if self.console.execute("cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN))):
292 print "execute failed"
295 def mergeServices(self, directory, name, merge = False):
296 print "merging services:", directory, " - ", name
297 if os.path.isfile(directory + name):
298 db = eDVBDB.getInstance()
299 db.reloadServicelist()
300 db.loadServicelist(directory + name)
304 def installFavourites(self, directory, name):
305 print "installing favourites:", directory, " - ", name
308 os.system("cp %s %s" % ((directory + name), resolveFilename(SCOPE_CONFIG)))
311 if self.console.execute("cp %s %s" % ((directory + name), resolveFilename(SCOPE_CONFIG))):
312 print "execute failed"