]> git.ipfire.org Git - pakfire.git/commitdiff
Add package compression.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Feb 2011 10:12:55 +0000 (11:12 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Feb 2011 10:12:55 +0000 (11:12 +0100)
pakfire/constants.py
pakfire/packages/file.py
pakfire/packages/packager.py

index af908f842024ce6a23eaa78a8dbf5d597621785c..89979eabcf29d6bb40e91dc7f7454fd8f6e58834 100644 (file)
@@ -88,7 +88,7 @@ PKG_DESCRIPTION="%(description)s"
 PKG_REQUIRES="%(requires)s"
 PKG_PROVIDES="%(provides)s"
 
-PKG_PAYLOAD_COMP="XXX"
+PKG_PAYLOAD_COMP="%(payload_comp)s"
 PKG_PAYLOAD_SIZE="107869"
 
 """
index 1f819fd1c2d1c75b9531e422105b52c042691a91..bf58f71d1e9501cb33a0b7b4b2acdb085faf13f2 100644 (file)
@@ -147,7 +147,17 @@ class FilePackage(Package):
                """
                        Return the compression type of the payload.
                """
-               return self.metadata.get("PKG_PAYLOAD_COMP")
+               comp = self.metadata.get("PKG_PAYLOAD_COMP", None)
+
+               # Remove triple X placeholder that was used some time.
+               if comp == "X"*3:
+                       comp = None
+
+               # XXX remove that later, because of compatibility for naoki.
+               elif not comp:
+                       comp = "xz"
+
+               return comp
 
        @property
        def signature(self):
index d9f7ec8d37cc9cf927f619becb0c12ec815d1c22..81f6de07578c1c4e609102cc4035c6b119610a14 100644 (file)
@@ -11,6 +11,7 @@ import tarfile
 import tempfile
 import uuid
 import xattr
+import zlib
 
 from pakfire.constants import *
 from pakfire.i18n import _
@@ -25,28 +26,30 @@ class Extractor(object):
                self.archive = None
                self._tempfile = None
 
-               if pkg.payload_compression == "XXX":
-                       self.archive = tarfile.open(fileobj=self.data)
+               if self.pkg.payload_compression:
+                       self.uncompress_payload()
                else:
-                       self._uncompress_data()
+                       self.archive = tarfile.open(fileobj=self.data)
 
        def cleanup(self):
                # XXX not called by anything
                if self._tempfile:
                        os.unlink(self._tempfile)
 
-       def _uncompress_data(self):
+       def uncompress_payload(self):
                # XXX this function uncompresses the data.img file
                # and saves the bare tarball to /tmp which takes a lot
                # of space.
 
-               self.data.seek(0)
-
                # Create a temporary file to save the content in
                f, self._tempfile = tempfile.mkstemp()
                f = open(self._tempfile, "w")
 
-               decompressor = lzma.LZMADecompressor()
+               if self.pkg.payload_compression == "xz":
+                       decompressor = lzma.LZMADecompressor()
+
+               elif self.pkg.payload_compression == "zlib":
+                       decompressor = zlib.decompressobj()
 
                buf = self.data.read(BUFFER_SIZE)
                while buf:
@@ -194,6 +197,7 @@ class Packager(object):
                self.info = {
                        "package_format" : PACKAGE_FORMAT,
                        "package_uuid" : uuid.uuid4(),
+                       "payload_comp" : None,
                }
                self.info.update(self.pkg.info)
                self.info.update(self.pakfire.distro.info)
@@ -243,7 +247,7 @@ class Packager(object):
 
                tar.close()
 
-       def create_tarball(self):
+       def create_tarball(self, compress="xz"):
                tar = InnerTarFile(self.archive_files["data.img"], mode="w", env=self.env)
 
                includes = []
@@ -336,7 +340,35 @@ class Packager(object):
                tar.close()
                filelist.close()
 
-               # XXX compress the tarball here
+               # compress the tarball here
+               if compress:
+                       # Save algorithm to metadata.
+                       self.info["payload_comp"] = compress
+
+                       logging.debug("Compressing package with %s algorithm." % compress or "no")
+
+                       filename = self.archive_files["data.img"]
+                       i = open(filename)
+                       os.unlink(filename)
+
+                       o = open(filename, "w")
+
+                       if compress == "xz":
+                               comp = lzma.LZMACompressor()
+
+                       elif compress == "zlib":
+                               comp = zlib.compressobj(9)
+
+                       buf = i.read(BUFFER_SIZE)
+                       while buf:
+                               o.write(comp.compress(buf))
+
+                               buf = i.read(BUFFER_SIZE)
+
+                       o.write(comp.flush())
+
+                       i.close()
+                       o.close()
 
        def create_info(self):
                f = open(self.archive_files["info"], "w")