Exemplo n.º 1
0
    def _build_volume ( self, file_name ):
        """ Creates the image volume using the specified file name and the
            current user supplied information and input files.
        """
        # Set the current status:
        self.status = 'Saving to %s...' % file_name

        # Create an image volume to hold the information collected. Note that we
        # set a bogus time stamp to force the ImageLibrary to reload all of the
        # image information from the actual files so that we end up with the
        # correct image sizes:
        volume = ImageVolume(
            category   = self.category,
            keywords   = self.keywords,
            time_stamp = time_stamp_for( 0.0 ),
            info       = [ ImageVolumeInfo(
                               description = self.description,
                               copyright   = self.copyright,
                               license     = self.license ) ]
        )

        # Create an ImageInfo descriptor for each image file that will become
        # part of the volume:
        volume_name   = splitext( basename( file_name ) )[0]
        volume.images = [ ImageInfo(
                              name        = item.image_name,
                              image_name  = '@%s:%s' % ( volume_name,
                                                         item.image_name ),
                              description = self.image_description,
                              category    = self.image_category,
                              keywords    = self.image_keywords )
                          for item in self.build_files ]

        # Pre-compute the images code, because it can require a long time
        # to load all of the images so that we can determine their size, and we
        # don't want that time to interfere with the time stamp of the image
        # volume:
        images_code = volume.images_code

        # Create the new zip file:
        zf = ZipFile( file_name, 'w', ZIP_DEFLATED )

        # Assume that an error will occur:
        error = True
        fh    = None
        try:
            # Copy all of the image files from the file system to the new zip
            # file:
            for item in self.build_files:
                fh   = file( item.file_name, 'rb' )
                data = fh.read()
                fh.close()
                fh = None
                zf.writestr( item.image_name, data )

            # Write the volume manifest source code to the zip file:
            zf.writestr( 'image_volume.py', volume.image_volume_code )

            # Write the image info source code to the zip file:
            zf.writestr( 'image_info.py', images_code )

            # Done creating the new zip file:
            zf.close()
            zf = None

            # Add the new image volume to the image library:
            ImageLibrary.add_volume( file_name )

            # Now invoke the normal volume save so that it can add the correct
            # image info data and license file:
            volume = [ volume for volume in ImageLibrary.volumes
                       if volume.path == file_name ][0]
            volume.path     = file_name
            volume.zip_file = FastZipFile( path = file_name )

            volume.save()

            # Set the final status:
            self.status = '%s has been saved successfully.' % file_name

            # Indicate no errors occurred:
            error = False
        finally:
            if fh is not None:
                fh.close()

            if zf is not None:
                zf.close()

            if error:
                self.status = 'An error occurred trying to save %s.' % file_name
                remove( file_name )