Esempio n. 1
0
 def load(filename):
     """
     Load package from disk, returns a package
     """
     if not zipfile.is_zipfile(filename):
         return None
     zippedFile = zipfile.ZipFile(filename, "r", zipfile.ZIP_DEFLATED)
     toDecode   = zippedFile.read(u"content.data")
     try:
         newPackage = decodeObjectRaw(toDecode)
         newPackage.afterUpgradeHandlers = []
         doUpgrade()
     except:
         import traceback
         traceback.print_exc()
         raise
     newPackage.filename = Path(filename)
     newPackage.resourceDir = TempDirPath()
     for filename in zippedFile.namelist():
         if unicode(filename, 'utf8') != u"content.data":
             outFile = open(newPackage.resourceDir/filename, "wb")
             outFile.write(zippedFile.read(filename))
     for handler in newPackage.afterUpgradeHandlers:
         handler()
     del newPackage.afterUpgradeHandlers
     return newPackage
Esempio n. 2
0
 def load(filename):
     """
     Load package from disk, returns a package.
     """
     if not zipfile.is_zipfile(filename):
         return None
     zippedFile = zipfile.ZipFile(filename, "r", zipfile.ZIP_DEFLATED)
     toDecode   = zippedFile.read(u"content.data")
     resourceDir = TempDirPath()
     for fn in zippedFile.namelist():
         if unicode(fn, 'utf8') != u"content.data":
             outFile = open(resourceDir/fn, "wb")
             outFile.write(zippedFile.read(fn))
     try:
         newPackage = decodeObjectRaw(toDecode)
         newPackage.afterUpgradeHandlers = []
         newPackage.resourceDir = resourceDir
         doUpgrade()
     except:
         import traceback
         traceback.print_exc()
         raise
     if newPackage.tempFile:
         newPackage.tempFile = False
     else:
         newPackage.filename = Path(filename)
     for handler in newPackage.afterUpgradeHandlers:
         handler()
     del newPackage.afterUpgradeHandlers
     newPackage.updateRecentDocuments(newPackage.filename)
     newPackage.isChanged = False
     return newPackage
    def load(filename, newLoad=True, destinationPackage=None):
        """
        Load package from disk, returns a package.
        """
        if not zipfile.is_zipfile(filename):
            return None

        zippedFile = zipfile.ZipFile(filename, "r")

        try:
            # Get the jellied package data
            toDecode = zippedFile.read(u"content.data")
        except KeyError:
            log.info("no content.data, trying Common Cartridge/Content Package")
            newPackage = loadCC(zippedFile, filename)
            newPackage.tempFile = False
            newPackage.isChanged = False
            newPackage.filename = Path(filename)

            return newPackage

        # Need to add a TempDirPath because it is a nonpersistant member
        resourceDir = TempDirPath()

        # Extract resource files from package to temporary directory
        for fn in zippedFile.namelist():
            if unicode(fn, "utf8") != u"content.data":
                outFile = open(resourceDir / fn, "wb")
                outFile.write(zippedFile.read(fn))
                outFile.flush()
                outFile.close()

        try:
            newPackage = decodeObjectRaw(toDecode)
            G.application.afterUpgradeHandlers = []
            newPackage.resourceDir = resourceDir
            G.application.afterUpgradeZombies2Delete = []

            if newLoad:
                # provide newPackage to doUpgrade's versionUpgrade() to
                # correct old corrupt extracted packages by setting the
                # any corrupt package references to the new package:

                log.debug('load() about to doUpgrade newPackage "' + newPackage._name + '" ' + repr(newPackage))
                if hasattr(newPackage, "resourceDir"):
                    log.debug("newPackage resourceDir = " + newPackage.resourceDir)
                else:
                    # even though it was just set above? should not get here:
                    log.error("newPackage resourceDir has NO resourceDir!")

                doUpgrade(newPackage)

                # after doUpgrade, compare the largest found field ID:
                if G.application.maxFieldId >= Field.nextId:
                    Field.nextId = G.application.maxFieldId + 1

            else:
                # and when merging, automatically set package references to
                # the destinationPackage, into which this is being merged:

                log.debug(
                    'load() about to merge doUpgrade newPackage "'
                    + newPackage._name
                    + '" '
                    + repr(newPackage)
                    + ' INTO destinationPackage "'
                    + destinationPackage._name
                    + '" '
                    + repr(destinationPackage)
                )

                log.debug("using their resourceDirs:")
                if hasattr(newPackage, "resourceDir"):
                    log.debug("   newPackage resourceDir = " + newPackage.resourceDir)
                else:
                    log.error("newPackage has NO resourceDir!")
                if hasattr(destinationPackage, "resourceDir"):
                    log.debug("   destinationPackage resourceDir = " + destinationPackage.resourceDir)
                else:
                    log.error("destinationPackage has NO resourceDir!")

                doUpgrade(destinationPackage, isMerge=True, preMergePackage=newPackage)

                # after doUpgrade, compare the largest found field ID:
                if G.application.maxFieldId >= Field.nextId:
                    Field.nextId = G.application.maxFieldId + 1

        except:
            import traceback

            traceback.print_exc()
            raise

        if newPackage.tempFile:
            # newPackage.filename was stored as it's original filename
            newPackage.tempFile = False
        else:
            # newPackage.filename is the name that the package was last loaded from
            # or saved to
            newPackage.filename = Path(filename)

        # Let idevices and nodes handle any resource upgrading they may need to
        # Note: Package afterUpgradeHandlers *must* be done after Resources'
        # and the package should be updated before everything else,
        # so, prioritize with a 3-pass, 3-level calling setup
        # in order of: 1) resources, 2) package, 3) anything other objects
        for handler_priority in range(3):
            for handler in G.application.afterUpgradeHandlers:

                if handler_priority == 0 and repr(handler.im_class) == "<class 'exe.engine.resource.Resource'>":
                    # level-0 handlers: Resource
                    handler()

                elif handler_priority == 1 and repr(handler.im_class) == "<class 'exe.engine.package.Package'>":
                    # level-1 handlers: Package (requires resources first)
                    if handler.im_self == newPackage:
                        handler()
                    else:
                        log.warn("Extra package object found, " + "ignoring its afterUpgradeHandler: " + repr(handler))

                elif (
                    handler_priority == 2
                    and repr(handler.im_class) != "<class 'exe.engine.resource.Resource'>"
                    and repr(handler.im_class) != "<class 'exe.engine.package.Package'>"
                ):
                    # level-2 handlers: all others
                    handler()

        G.application.afterUpgradeHandlers = []

        num_zombies = len(G.application.afterUpgradeZombies2Delete)
        for i in range(num_zombies - 1, -1, -1):
            zombie = G.application.afterUpgradeZombies2Delete[i]
            # now, the zombie list can contain nodes OR resources to delete.
            # if zombie is a node, then also pass in a pruning parameter..
            zombie_is_node = False
            if isinstance(zombie, Node):
                zombie_is_node = True

            if zombie_is_node:
                zombie.delete(pruningZombies=True)
            else:
                zombie.delete()
            del zombie
        G.application.afterUpgradeZombies2Delete = []

        newPackage.updateRecentDocuments(newPackage.filename)
        newPackage.isChanged = False
        return newPackage
    def load(filename, newLoad=True, destinationPackage=None):
        """
        Load package from disk, returns a package.
        """
        if not zipfile.is_zipfile(filename):
            return None

        zippedFile = zipfile.ZipFile(filename, "r")
        
        try:
            # Get the jellied package data
            toDecode   = zippedFile.read(u"content.data")
        except KeyError:
            log.info("no content.data, trying Common Cartridge/Content Package")
            newPackage = loadCC(zippedFile, filename)
            newPackage.tempFile = False
            newPackage.isChanged = False
            newPackage.filename = Path(filename)

            return newPackage
            
        # Need to add a TempDirPath because it is a nonpersistant member
        resourceDir = TempDirPath()

        # Extract resource files from package to temporary directory
        for fn in zippedFile.namelist():
            if unicode(fn, 'utf8') != u"content.data":
                outFile = open(resourceDir/fn, "wb")
                outFile.write(zippedFile.read(fn))
                outFile.flush()
                outFile.close()

        try:
            newPackage = decodeObjectRaw(toDecode)
            G.application.afterUpgradeHandlers = []
            newPackage.resourceDir = resourceDir
            G.application.afterUpgradeZombies2Delete = []

            if newLoad: 
                # provide newPackage to doUpgrade's versionUpgrade() to
                # correct old corrupt extracted packages by setting the
                # any corrupt package references to the new package:

                log.debug("load() about to doUpgrade newPackage \"" 
                        + newPackage._name + "\" " + repr(newPackage) )
                if hasattr(newPackage, 'resourceDir'):
                    log.debug("newPackage resourceDir = "
                            + newPackage.resourceDir)
                else:
                    # even though it was just set above? should not get here:
                    log.error("newPackage resourceDir has NO resourceDir!")

                doUpgrade(newPackage)

                # after doUpgrade, compare the largest found field ID:
                if G.application.maxFieldId >= Field.nextId:
                    Field.nextId = G.application.maxFieldId + 1

            else: 
                # and when merging, automatically set package references to
                # the destinationPackage, into which this is being merged:

                log.debug("load() about to merge doUpgrade newPackage \"" 
                        + newPackage._name + "\" " + repr(newPackage)
                        + " INTO destinationPackage \"" 
                        + destinationPackage._name + "\" " 
                        + repr(destinationPackage))
                
                log.debug("using their resourceDirs:")
                if hasattr(newPackage, 'resourceDir'):
                    log.debug("   newPackage resourceDir = " 
                            + newPackage.resourceDir)
                else:
                    log.error("newPackage has NO resourceDir!")
                if hasattr(destinationPackage, 'resourceDir'):
                    log.debug("   destinationPackage resourceDir = " 
                            + destinationPackage.resourceDir)
                else:
                    log.error("destinationPackage has NO resourceDir!")

                doUpgrade(destinationPackage, 
                        isMerge=True, preMergePackage=newPackage)

                # after doUpgrade, compare the largest found field ID:
                if G.application.maxFieldId >= Field.nextId:
                    Field.nextId = G.application.maxFieldId + 1

        except:
            import traceback
            traceback.print_exc()
            raise

        if newPackage.tempFile:
            # newPackage.filename was stored as it's original filename
            newPackage.tempFile = False
        else:
            # newPackage.filename is the name that the package was last loaded from
            # or saved to
            newPackage.filename = Path(filename)

        # Let idevices and nodes handle any resource upgrading they may need to
        # Note: Package afterUpgradeHandlers *must* be done after Resources'
        # and the package should be updated before everything else,
        # so, prioritize with a 3-pass, 3-level calling setup
        # in order of: 1) resources, 2) package, 3) anything other objects
        for handler_priority in range(3):
          for handler in G.application.afterUpgradeHandlers:

            if handler_priority == 0 and \
            repr(handler.im_class)=="<class 'exe.engine.resource.Resource'>":
                # level-0 handlers: Resource
                handler()

            elif handler_priority == 1 and \
            repr(handler.im_class)=="<class 'exe.engine.package.Package'>":
                # level-1 handlers: Package (requires resources first)
                if handler.im_self == newPackage: 
                    handler()
                else:
                    log.warn("Extra package object found, " \
                       + "ignoring its afterUpgradeHandler: " \
                       + repr(handler))

            elif handler_priority == 2 and \
            repr(handler.im_class)!="<class 'exe.engine.resource.Resource'>" \
            and \
            repr(handler.im_class)!="<class 'exe.engine.package.Package'>":
                # level-2 handlers: all others
                handler()

        G.application.afterUpgradeHandlers = []

        num_zombies = len(G.application.afterUpgradeZombies2Delete)
        for i in range(num_zombies-1, -1, -1):
            zombie = G.application.afterUpgradeZombies2Delete[i]
            # now, the zombie list can contain nodes OR resources to delete.
            # if zombie is a node, then also pass in a pruning parameter..
            zombie_is_node = False
            if isinstance(zombie, Node):
                zombie_is_node = True

            if zombie_is_node: 
                zombie.delete(pruningZombies=True) 
            else:
                zombie.delete() 
            del zombie
        G.application.afterUpgradeZombies2Delete = []

        newPackage.updateRecentDocuments(newPackage.filename)
        newPackage.isChanged = False
        return newPackage
Esempio n. 5
0
 def load(filename, newLoad=True, destinationPackage=None):
     """
     Load package from disk, returns a package.
     """
     if not zipfile.is_zipfile(filename):
         return None
     zippedFile = zipfile.ZipFile(filename, "r")
     toDecode   = zippedFile.read(u"content.data")
     resourceDir = TempDirPath()
     for fn in zippedFile.namelist():
         if unicode(fn, 'utf8') != u"content.data":
             outFile = open(resourceDir/fn, "wb")
             outFile.write(zippedFile.read(fn))
             outFile.flush()
             outFile.close()
     try:
         newPackage = decodeObjectRaw(toDecode)
         G.application.afterUpgradeHandlers = []
         newPackage.resourceDir = resourceDir
         G.application.afterUpgradeZombies2Delete = []
         if newLoad: 
             log.debug("load() about to doUpgrade newPackage \"" 
                     + newPackage._name + "\" " + repr(newPackage) )
             if hasattr(newPackage, 'resourceDir'):
                 log.debug("newPackage resourceDir = "
                         + newPackage.resourceDir)
             else:
                 log.error("newPackage resourceDir has NO resourceDir!")
             doUpgrade(newPackage)
         else: 
             log.debug("load() about to merge doUpgrade newPackage \"" 
                     + newPackage._name + "\" " + repr(newPackage)
                     + " INTO destinationPackage \"" 
                     + destinationPackage._name + "\" " 
                     + repr(destinationPackage))
             log.debug("using their resourceDirs:")
             if hasattr(newPackage, 'resourceDir'):
                 log.debug("   newPackage resourceDir = " 
                         + newPackage.resourceDir)
             else:
                 log.error("newPackage has NO resourceDir!")
             if hasattr(destinationPackage, 'resourceDir'):
                 log.debug("   destinationPackage resourceDir = " 
                         + destinationPackage.resourceDir)
             else:
                 log.error("destinationPackage has NO resourceDir!")
             doUpgrade(destinationPackage, 
                     isMerge=True, preMergePackage=newPackage)
     except:
         import traceback
         traceback.print_exc()
         raise
     if newPackage.tempFile:
         newPackage.tempFile = False
     else:
         newPackage.filename = Path(filename)
     for handler_priority in range(3):
       for handler in G.application.afterUpgradeHandlers:
         if handler_priority == 0 and \
         repr(handler.im_class)=="<class 'exe.engine.resource.Resource'>":
             handler()
         elif handler_priority == 1 and \
         repr(handler.im_class)=="<class 'exe.engine.package.Package'>":
             if handler.im_self == newPackage: 
                 handler()
             else:
                 log.warn("Extra package object found, " \
                    + "ignoring its afterUpgradeHandler: " \
                    + repr(handler))
         elif handler_priority == 2 and \
         repr(handler.im_class)!="<class 'exe.engine.resource.Resource'>" \
         and \
         repr(handler.im_class)!="<class 'exe.engine.package.Package'>":
             handler()
     G.application.afterUpgradeHandlers = []
     num_zombies = len(G.application.afterUpgradeZombies2Delete)
     for i in range(num_zombies-1, -1, -1):
         zombie = G.application.afterUpgradeZombies2Delete[i]
         zombie_is_node = False
         if isinstance(zombie, Node):
             zombie_is_node = True
         if zombie_is_node: 
             zombie.delete(pruningZombies=True) 
         else:
             zombie.delete() 
         del zombie
     G.application.afterUpgradeZombies2Delete = []
     newPackage.updateRecentDocuments(newPackage.filename)
     newPackage.isChanged = False
     return newPackage