allow installing from multiple directories
[enigma2.git] / lib / python / Components / DreamInfoHandler.py
index 33b86bb..858b99a 100644 (file)
@@ -2,7 +2,8 @@ import xml.sax
 from Tools.Directories import crawlDirectory, resolveFilename, SCOPE_CONFIG, SCOPE_SKIN
 from Components.NimManager import nimmanager
 from Components.Ipkg import IpkgComponent
-from enigma import eConsoleAppContainer
+from Components.config import config, configfile
+from enigma import eConsoleAppContainer, eDVBDB
 import os
 
 class InfoHandlerParseError(Exception):
@@ -30,7 +31,7 @@ class InfoHandler(xml.sax.ContentHandler):
        def startElement(self, name, attrs):
                print name, ":", attrs.items()
                self.elements.append(name)
-               if name in ["hardware", "bcastsystem", "satellite"]:
+               if name in ["hardware", "bcastsystem", "satellite", "tag"]:
                        if not attrs.has_key("type"):
                                        self.printError(str(name) + " tag with no type attribute")
                        if self.elements[-3] == "default":
@@ -96,9 +97,17 @@ class DreamInfoHandler:
        STATUS_ERROR = 2
        STATUS_INIT = 4
        
-       def __init__(self, statusCallback):
+       def __init__(self, statusCallback, blocking = False, neededTag = None):
                self.directory = "/"
                
+               self.neededTag = neededTag
+               
+               # caution: blocking should only be used, if further execution in enigma2 depends on the outcome of
+               # the installer!
+               self.blocking = blocking
+               
+               self.currentlyInstallingMetaIndex = None
+               
                self.console = eConsoleAppContainer()
                self.console.appClosed.get().append(self.installNext)
                
@@ -106,7 +115,7 @@ class DreamInfoHandler:
                self.setStatus(self.STATUS_INIT)
                                
                self.packageslist = []
-       
+                       
        def readInfo(self, directory, file):
                print "Reading .info file", file
                handler = InfoHandler(self.prerequisiteMet, directory)
@@ -121,7 +130,13 @@ class DreamInfoHandler:
        # prerequisites = True: give only packages matching the prerequisites
        def fillPackagesList(self, prerequisites = True):
                self.packageslist = []
-               packages = crawlDirectory(self.directory, ".*\.info$")
+               packages = []
+               if not isinstance(self.directory, list):
+                       self.directory = [self.directory]
+               
+               for directory in self.directory:
+                       packages += crawlDirectory(directory, ".*\.info$")
+
                for package in packages:
                        self.readInfo(package[0] + "/", package[0] + "/" + package[1])
                        
@@ -135,6 +150,18 @@ class DreamInfoHandler:
                # TODO: we need to implement a hardware detection here...
                print "prerequisites:", prerequisites
                met = True
+               if self.neededTag is None:
+                       if prerequisites.has_key("tag"):
+                               return False
+               else:
+                       if prerequisites.has_key("tag"):
+                               if not self.neededTag in prerequisites["tag"]:
+                                       return False
+                               
+               if prerequisites.has_key("satellite"):
+                       for sat in prerequisites["satellite"]:
+                               if int(sat) not in nimmanager.getConfiguredSats():
+                                       return False                            
                if prerequisites.has_key("bcastsystem"):
                        for bcastsystem in prerequisites["bcastsystem"]:
                                if nimmanager.hasNimType(bcastsystem):
@@ -145,7 +172,17 @@ class DreamInfoHandler:
                                # TODO: hardware detection
                                met = True
                return True
-                       
+       
+       def installPackages(self, indexes):
+               print "installing packages", indexes
+               if len(indexes) == 0:
+                       self.setStatus(self.STATUS_DONE)
+                       return
+               self.installIndexes = indexes
+               print "+++++++++++++++++++++++bla"
+               self.currentlyInstallingMetaIndex = 0
+               self.installPackage(self.installIndexes[self.currentlyInstallingMetaIndex])
+
        def installPackage(self, index):
                print "installing package with index", index, "and name", self.packageslist[index][0]["attributes"]["name"]
                
@@ -163,10 +200,18 @@ class DreamInfoHandler:
        def installNext(self, *args, **kwargs):
                self.currentIndex += 1
                attributes = self.installingAttributes
+               #print "attributes:", attributes
                
-               if self.currentAttributeIndex >= len(self.attributeNames): # end reached
-                       self.setStatus(self.STATUS_DONE)
-                       return
+               if self.currentAttributeIndex >= len(self.attributeNames): # end of package reached
+                       print "end of package reached"
+                       if self.currentlyInstallingMetaIndex is None or self.currentlyInstallingMetaIndex >= len(self.installIndexes) - 1:
+                               print "set status to DONE"
+                               self.setStatus(self.STATUS_DONE)
+                               return
+                       else:
+                               print "increment meta index to install next package"
+                               self.currentlyInstallingMetaIndex += 1
+                               self.installPackage(self.installIndexes[self.currentlyInstallingMetaIndex])
                
                self.setStatus(self.STATUS_WORKING)             
                
@@ -181,6 +226,7 @@ class DreamInfoHandler:
                                self.installNext()
                                return
                else: # nothing to install here
+                       self.currentIndex = -1
                        self.currentAttributeIndex += 1
                        self.installNext()
                        return
@@ -205,6 +251,8 @@ class DreamInfoHandler:
                        self.mergeServices(service["directory"], service["name"])
                                
        def readfile(self, filename):
+               if not os.path.isfile(filename):
+                       return []
                fd = open(filename)
                lines = fd.readlines()
                fd.close()
@@ -212,41 +260,19 @@ class DreamInfoHandler:
                        
        def mergeConfig(self, directory, name, merge = True):
                print "merging config:", directory, " - ", name
-
-               newconfig = self.readfile(directory + name)
-               newconfig.sort()
-               print newconfig
-               
-               if merge:
-                       oldconfig = self.readfile(resolveFilename(SCOPE_CONFIG) + "settings")
-                       oldconfig.sort()
-                       print oldconfig
-               else:
-                       oldconfig = []
-               
-               # merge with duplicate removal through dictionary
-               mergeddict = {}
-               for list in oldconfig, newconfig:
-                       for entry in list:
-                               splitentry = entry.split("=")
-                               if len(splitentry) != 2: # wrong entry
-                                       continue
-                               mergeddict[splitentry[0]] = splitentry[1].strip()
-               
-               print "new:"
-               fd = open(resolveFilename(SCOPE_CONFIG) + "settings", "w")
-               for entry in mergeddict.keys():
-                       print entry + "=" + mergeddict[entry]
-                       fd.write(entry + "=" + mergeddict[entry] + '\n')
-               fd.close()
+               if os.path.isfile(directory + name):
+                       config.loadFromFile(directory + name)
+                       configfile.save()
                self.installNext()
-               #configfile.load()
-               
                
        def installIPK(self, directory, name):
-               self.ipkg = IpkgComponent()
-               self.ipkg.addCallback(self.ipkgCallback)
-               self.ipkg.startCmd(IpkgComponent.CMD_INSTALL, {'package': directory + name})
+               if self.blocking:
+                       os.system("ipkg install " + directory + name)
+                       self.installNext()
+               else:
+                       self.ipkg = IpkgComponent()
+                       self.ipkg.addCallback(self.ipkgCallback)
+                       self.ipkg.startCmd(IpkgComponent.CMD_INSTALL, {'package': directory + name})
                
        def ipkgCallback(self, event, param):
                print "ipkgCallback"
@@ -258,68 +284,30 @@ class DreamInfoHandler:
        def installSkin(self, directory, name):
                print "installing skin:", directory, " - ", name
                print "cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN))
-               if self.console.execute("cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN))):
-                       print "execute failed"
+               if self.blocking:
+                       os.system("cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN)))
                        self.installNext()
+               else:
+                       if self.console.execute("cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN))):
+                               print "execute failed"
+                               self.installNext()
 
-       def readServices(self, filename):
-               newservicesfile = self.readfile(filename)
-               
-               transponders = []
-               services = []
-               status = 0 # 0 = start, 1 = transponders, 2 = services
-               count = 0
-               while count < len(newservicesfile):
-                       if status == 0:
-                               if newservicesfile[count].strip() == "transponders":
-                                       status = 1
-                       elif status == 1: # reading transponders
-                               if newservicesfile[count].strip() == "end": # finished reading transponders
-                                       pass
-                               elif newservicesfile[count].strip() == "services": # start of services section
-                                       status = 2
-                               else:
-                                       transponders.append(''.join(newservicesfile[count:count + 3]))
-                                       count += 2
-                       elif status == 2: # reading services
-                               if newservicesfile[count].strip() == "end": # finished reading file
-                                       break
-                               else:
-                                       services.append(''.join(newservicesfile[count:count + 3]))
-                                       count += 2
-                       count += 1
-               return (transponders, services)
-       
        def mergeServices(self, directory, name, merge = False):
                print "merging services:", directory, " - ", name
-               
-               newtransponders, newservices = self.readServices(directory + name)
-               if merge:
-                       oldtransponders, oldservices = self.readServices(resolveFilename(SCOPE_CONFIG) + "lamedb")
-               else:
-                       oldtransponders, oldservices = [], []
-               
-               fp = open(resolveFilename(SCOPE_CONFIG) + "lamedb", "w")
-               fp.write("eDVB services /3/\n")
-               
-               fp.write("transponders\n")
-               for transponderlist in oldtransponders, newtransponders:
-                       for transponder in transponderlist:
-                               fp.write(transponder)
-               fp.write("end\n")
-               
-               fp.write("services\n")
-               for serviceslist in oldservices, newservices:
-                       for service in serviceslist:
-                               fp.write(service)
-               fp.write("end\n")
-               
-               fp.close()
+               if os.path.isfile(directory + name):
+                       db = eDVBDB.getInstance()
+                       db.reloadServicelist()
+                       db.loadServicelist(directory + name)
+                       db.saveServicelist()
                self.installNext()
 
        def installFavourites(self, directory, name):
                print "installing favourites:", directory, " - ", name
 
-               if self.console.execute("cp %s %s" % ((directory + name), resolveFilename(SCOPE_CONFIG))):
-                       print "execute failed"
+               if self.blocking:
+                       os.system("cp %s %s" % ((directory + name), resolveFilename(SCOPE_CONFIG)))
                        self.installNext()
+               else:
+                       if self.console.execute("cp %s %s" % ((directory + name), resolveFilename(SCOPE_CONFIG))):
+                               print "execute failed"
+                               self.installNext()