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"]:
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):
103 # caution: blocking should only be used, if further execution in enigma2 depends on the outcome of
105 self.blocking = blocking
107 self.currentlyInstallingMetaIndex = None
109 self.console = eConsoleAppContainer()
110 self.console.appClosed.get().append(self.installNext)
112 self.statusCallback = statusCallback
113 self.setStatus(self.STATUS_INIT)
115 self.packageslist = []
117 def readInfo(self, directory, file):
118 print "Reading .info file", file
119 handler = InfoHandler(self.prerequisiteMet, directory)
121 xml.sax.parse(file, handler)
122 for entry in handler.list:
123 self.packageslist.append((entry,file))
124 except InfoHandlerParseError:
125 print "file", file, "ignored due to errors in the file"
128 # prerequisites = True: give only packages matching the prerequisites
129 def fillPackagesList(self, prerequisites = True):
130 self.packageslist = []
131 packages = crawlDirectory(self.directory, ".*\.info$")
132 for package in packages:
133 self.readInfo(package[0] + "/", package[0] + "/" + package[1])
136 for package in self.packageslist[:]:
137 if not self.prerequisiteMet(package[0]["prerequisites"]):
138 self.packageslist.remove(package)
141 def prerequisiteMet(self, prerequisites):
142 # TODO: we need to implement a hardware detection here...
143 print "prerequisites:", prerequisites
145 if prerequisites.has_key("bcastsystem"):
146 for bcastsystem in prerequisites["bcastsystem"]:
147 if nimmanager.hasNimType(bcastsystem):
150 if prerequisites.has_key("hardware"):
151 for hardware in prerequisites["hardware"]:
152 # TODO: hardware detection
156 def installPackages(self, indexes):
157 print "installing packages", indexes
158 if len(indexes) == 0:
159 self.setStatus(self.STATUS_DONE)
161 self.installIndexes = indexes
162 print "+++++++++++++++++++++++bla"
163 self.currentlyInstallingMetaIndex = 0
164 self.installPackage(self.installIndexes[self.currentlyInstallingMetaIndex])
166 def installPackage(self, index):
167 print "installing package with index", index, "and name", self.packageslist[index][0]["attributes"]["name"]
169 attributes = self.packageslist[index][0]["attributes"]
170 self.installingAttributes = attributes
171 self.attributeNames = ["skin", "config", "favourites", "package", "services"]
172 self.currentAttributeIndex = 0
173 self.currentIndex = -1
176 def setStatus(self, status):
178 self.statusCallback(self.status, None)
180 def installNext(self, *args, **kwargs):
181 self.currentIndex += 1
182 attributes = self.installingAttributes
183 #print "attributes:", attributes
185 if self.currentAttributeIndex >= len(self.attributeNames): # end of package reached
186 if self.currentlyInstallingMetaIndex is None or self.currentlyInstallingMetaIndex >= len(self.installIndexes) - 1:
187 self.setStatus(self.STATUS_DONE)
190 self.currentlyInstallingMetaIndex += 1
191 self.installPackage(self.installIndexes[self.currentlyInstallingMetaIndex])
193 self.setStatus(self.STATUS_WORKING)
195 currentAttribute = self.attributeNames[self.currentAttributeIndex]
197 print "installing", currentAttribute, "with index", self.currentIndex
199 if attributes.has_key(currentAttribute):
200 if self.currentIndex >= len(attributes[currentAttribute]): # all jobs done for current attribute
201 self.currentIndex = -1
202 self.currentAttributeIndex += 1
205 else: # nothing to install here
206 self.currentIndex = -1
207 self.currentAttributeIndex += 1
211 if currentAttribute == "skin":
212 skin = attributes["skin"][self.currentIndex]
213 self.installSkin(skin["directory"], skin["name"])
214 elif currentAttribute == "config":
215 if self.currentIndex == 0:
216 from Components.config import configfile
218 config = attributes["config"][self.currentIndex]
219 self.mergeConfig(config["directory"], config["name"])
220 elif currentAttribute == "favourites":
221 favourite = attributes["favourites"][self.currentIndex]
222 self.installFavourites(favourite["directory"], favourite["name"])
223 elif currentAttribute == "package":
224 package = attributes["package"][self.currentIndex]
225 self.installIPK(package["directory"], package["name"])
226 elif currentAttribute == "services":
227 service = attributes["services"][self.currentIndex]
228 self.mergeServices(service["directory"], service["name"])
230 def readfile(self, filename):
231 if not os.path.isfile(filename):
234 lines = fd.readlines()
238 def mergeConfig(self, directory, name, merge = True):
239 print "merging config:", directory, " - ", name
240 if os.path.isfile(directory + name):
241 config.loadFromFile(directory + name)
245 def installIPK(self, directory, name):
247 os.system("ipkg install " + directory + name)
250 self.ipkg = IpkgComponent()
251 self.ipkg.addCallback(self.ipkgCallback)
252 self.ipkg.startCmd(IpkgComponent.CMD_INSTALL, {'package': directory + name})
254 def ipkgCallback(self, event, param):
256 if event == IpkgComponent.EVENT_DONE:
258 elif event == IpkgComponent.EVENT_ERROR:
261 def installSkin(self, directory, name):
262 print "installing skin:", directory, " - ", name
263 print "cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN))
265 os.system("cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN)))
268 if self.console.execute("cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN))):
269 print "execute failed"
272 def mergeServices(self, directory, name, merge = False):
273 print "merging services:", directory, " - ", name
275 db = eDVBDB.getInstance()
276 db.reloadServicelist()
277 db.loadServicelist(directory + name)
281 def installFavourites(self, directory, name):
282 print "installing favourites:", directory, " - ", name
285 os.system("cp %s %s" % ((directory + name), resolveFilename(SCOPE_CONFIG)))
288 if self.console.execute("cp %s %s" % ((directory + name), resolveFilename(SCOPE_CONFIG))):
289 print "execute failed"