]> git.ipfire.org Git - pakfire.git/commitdiff
Move code for tarfile handling in an own file.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Dec 2012 15:45:23 +0000 (16:45 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Dec 2012 15:45:23 +0000 (16:45 +0100)
python/pakfire/packages/__init__.py
python/pakfire/packages/file.py
python/pakfire/packages/packager.py
python/pakfire/packages/tar.py [new file with mode: 0644]

index 9aabddccbb084ad8586615b597bbc3337f270e0d..414599e87f572769667fa4f305975afc5c0ab471 100644 (file)
@@ -22,7 +22,7 @@
 import tarfile
 
 from base import Package
-from file import BinaryPackage, FilePackage, InnerTarFile, SourcePackage
+from file import BinaryPackage, FilePackage, SourcePackage
 from installed import DatabasePackage, InstalledPackage
 from solv import SolvPackage
 
index 37f820e2b73ed5190fda26f0d49ba99f60360a22..26d91d3b925c7d4cb38ea0708777d0c57b7be698 100644 (file)
@@ -39,90 +39,7 @@ from pakfire.i18n import _
 import base
 import lexer
 import make
-
-class InnerTarFile(tarfile.TarFile):
-       def __init__(self, *args, **kwargs):
-               # Force the PAX format.
-               kwargs["format"] = tarfile.PAX_FORMAT
-
-               tarfile.TarFile.__init__(self, *args, **kwargs)
-
-       def add(self, name, arcname=None, recursive=None, exclude=None, filter=None):
-               """
-                       Emulate the add function with capability support.
-               """
-               tarinfo = self.gettarinfo(name, arcname)
-
-               if tarinfo.isreg():
-                       attrs = []
-
-                       # Save capabilities.
-                       caps = util.get_capabilities(name)
-                       if caps:
-                               log.debug("Saving capabilities for %s: %s" % (name, caps))
-                               tarinfo.pax_headers["PAKFIRE.capabilities"] = caps
-
-                       # Append the tar header and data to the archive.
-                       f = tarfile.bltn_open(name, "rb")
-                       self.addfile(tarinfo, f)
-                       f.close()
-
-               elif tarinfo.isdir():
-                       self.addfile(tarinfo)
-                       if recursive:
-                               for f in os.listdir(name):
-                                       self.add(os.path.join(name, f), os.path.join(arcname, f),
-                                                       recursive, exclude, filter)
-
-               else:
-                       self.addfile(tarinfo)
-
-               # Return the tar information about the file
-               return tarinfo
-
-       def extract(self, member, path=""):
-               target = os.path.join(path, member.name)
-
-               # Remove symlink targets, because tarfile cannot replace them.
-               if member.issym():
-                       try:
-                               os.unlink(target)
-                       except OSError:
-                               pass
-
-               # Extract file the normal way...
-               try:
-                       tarfile.TarFile.extract(self, member, path)
-               except OSError, e:
-                       log.warning(_("Could not extract file: /%(src)s - %(dst)s") \
-                               % { "src" : member.name, "dst" : e, })
-
-               if path:
-                       target = os.path.join(path, member.name)
-               else:
-                       target = "/%s" % member.name
-
-               # ...and then apply the capabilities.
-               caps = member.pax_headers.get("PAKFIRE.capabilities", None)
-               if caps:
-                       log.debug("Restoring capabilities for /%s: %s" % (member.name, caps))
-                       util.set_capabilities(target, caps)
-
-
-class InnerTarFileXz(InnerTarFile):
-       @classmethod
-       def open(cls, name=None, mode="r", fileobj=None, **kwargs):
-               fileobj = lzma.LZMAFile(name, mode, fileobj=fileobj)
-
-               try:
-                       t = cls.taropen(name, mode, fileobj, **kwargs)
-               except lzma.LZMAError:
-                       fileobj.close()
-                       raise tarfile.ReadError("not an lzma file")
-
-               t._extfileobj = False
-               return t
-
+import tar
 
 class FilePackage(base.Package):
        """
@@ -209,10 +126,10 @@ class FilePackage(base.Package):
 
                # Decompress the payload if needed.
                if self.payload_compression == "xz":
-                       payload_archive = InnerTarFileXz.open(fileobj=payload)
+                       payload_archive = tar.InnerTarFileXz.open(fileobj=payload)
 
                elif self.payload_compression == "none":
-                       payload_archive = InnerTarFile.open(fileobj=payload)
+                       payload_archive = tar.InnerTarFile.open(fileobj=payload)
 
                else:
                        raise Exception, "Unhandled payload compression type: %s" % payload_compression
index 1cf1ecf7e30bbf1b9746e73d0bc693aecb0073fc..a4566028e3ecd30f7260d8e2b8a31ab88be49331 100644 (file)
@@ -44,6 +44,7 @@ from pakfire.constants import *
 from pakfire.i18n import _
 
 import file
+import tar
 
 class Packager(object):
        payload_compression = None
@@ -143,9 +144,9 @@ class Packager(object):
                f = open(filelist, "w")
 
                if self.payload_compression == "xz":
-                       datafile = file.InnerTarFileXz.open(datafile)
+                       datafile = tar.InnerTarFileXz.open(datafile)
                else:
-                       datafile = file.InnerTarFile.open(datafile)
+                       datafile = tar.InnerTarFile.open(datafile)
 
                while True:
                        m = datafile.next()
@@ -201,18 +202,18 @@ class Packager(object):
                size = 0
 
                if self.payload_compression == "xz":
-                       tar = file.InnerTarFileXz.open(datafile)
+                       t = tar.InnerTarFileXz.open(datafile)
                else:
-                       tar = file.InnerTarFile.open(datafile)
+                       t = tar.InnerTarFile.open(datafile)
 
                while True:
-                       m = tar.next()
+                       m = t.next()
                        if not m:
                                break
 
                        size += m.size
 
-               tar.close()
+               t.close()
                return size
 
 
@@ -232,9 +233,9 @@ class BinaryPackager(Packager):
                tmpdir = self.mktemp(directory=True)
 
                if self.payload_compression == "xz":
-                       tarfile = file.InnerTarFileXz.open(datafile)
+                       tarfile = tar.InnerTarFileXz.open(datafile)
                else:
-                       tarfile = file.InnerTarFile.open(datafile)
+                       tarfile = tar.InnerTarFile.open(datafile)
 
                tarfile.extractall(path=tmpdir)
                tarfile.close()
@@ -401,9 +402,9 @@ class BinaryPackager(Packager):
 
                datafile = self.mktemp()
                if self.payload_compression == "xz":
-                       tar = file.InnerTarFileXz.open(datafile, mode="w")
+                       t = tar.InnerTarFileXz.open(datafile, mode="w")
                else:
-                       tar = file.InnerTarFile.open(datafile, mode="w")
+                       t = tar.InnerTarFile.open(datafile, mode="w")
 
                # All files in the tarball are relative to this directory.
                basedir = self.buildroot
@@ -422,7 +423,7 @@ class BinaryPackager(Packager):
                        arcname = "/%s" % os.path.relpath(file, basedir)
 
                        # Add file to tarball.
-                       tar.add(file, arcname=arcname, recursive=False)
+                       t.add(file, arcname=arcname, recursive=False)
 
                # Remove all packaged files.
                for file in reversed(files):
@@ -451,7 +452,7 @@ class BinaryPackager(Packager):
                                        break
 
                # Close the tarfile.
-               tar.close()
+               t.close()
 
                # Finish progressbar.
                if pb:
@@ -528,9 +529,9 @@ class BinaryPackager(Packager):
 
        def find_files(self, datafile, patterns):
                if self.payload_compression == "xz":
-                       datafile = file.InnerTarFileXz.open(datafile)
+                       datafile = tar.InnerTarFileXz.open(datafile)
                else:
-                       datafile = file.InnerTarFile.open(datafile)
+                       datafile = tar.InnerTarFile.open(datafile)
 
                members = datafile.getmembers()
 
@@ -715,9 +716,9 @@ class SourcePackager(Packager):
 
                filename = self.mktemp()
                if self.payload_compression == "xz":
-                       datafile = file.InnerTarFileXz.open(filename, mode="w")
+                       datafile = tar.InnerTarFileXz.open(filename, mode="w")
                else:
-                       datafile = file.InnerTarFile.open(filename, mode="w")
+                       datafile = tar.InnerTarFile.open(filename, mode="w")
 
                i = 0
                for arcname, file in files:
diff --git a/python/pakfire/packages/tar.py b/python/pakfire/packages/tar.py
new file mode 100644 (file)
index 0000000..2cdc1b0
--- /dev/null
@@ -0,0 +1,114 @@
+#!/usr/bin/python
+###############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2012 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import os
+import tarfile
+
+import logging
+log = logging.getLogger("pakfire")
+
+import pakfire.lzma as lzma
+import pakfire.util as util
+from pakfire.constants import *
+from pakfire.i18n import _
+
+class InnerTarFile(tarfile.TarFile):
+       def __init__(self, *args, **kwargs):
+               # Force the PAX format.
+               kwargs["format"] = tarfile.PAX_FORMAT
+
+               tarfile.TarFile.__init__(self, *args, **kwargs)
+
+       def add(self, name, arcname=None, recursive=None, exclude=None, filter=None):
+               """
+                       Emulate the add function with capability support.
+               """
+               tarinfo = self.gettarinfo(name, arcname)
+
+               if tarinfo.isreg():
+                       attrs = []
+
+                       # Save capabilities.
+                       caps = util.get_capabilities(name)
+                       if caps:
+                               log.debug("Saving capabilities for %s: %s" % (name, caps))
+                               tarinfo.pax_headers["PAKFIRE.capabilities"] = caps
+
+                       # Append the tar header and data to the archive.
+                       f = tarfile.bltn_open(name, "rb")
+                       self.addfile(tarinfo, f)
+                       f.close()
+
+               elif tarinfo.isdir():
+                       self.addfile(tarinfo)
+                       if recursive:
+                               for f in os.listdir(name):
+                                       self.add(os.path.join(name, f), os.path.join(arcname, f),
+                                                       recursive, exclude, filter)
+
+               else:
+                       self.addfile(tarinfo)
+
+               # Return the tar information about the file
+               return tarinfo
+
+       def extract(self, member, path=""):
+               target = os.path.join(path, member.name)
+
+               # Remove symlink targets, because tarfile cannot replace them.
+               if member.issym():
+                       try:
+                               os.unlink(target)
+                       except OSError:
+                               pass
+
+               # Extract file the normal way...
+               try:
+                       tarfile.TarFile.extract(self, member, path)
+               except OSError, e:
+                       log.warning(_("Could not extract file: /%(src)s - %(dst)s") \
+                               % { "src" : member.name, "dst" : e, })
+
+               if path:
+                       target = os.path.join(path, member.name)
+               else:
+                       target = "/%s" % member.name
+
+               # ...and then apply the capabilities.
+               caps = member.pax_headers.get("PAKFIRE.capabilities", None)
+               if caps:
+                       log.debug("Restoring capabilities for /%s: %s" % (member.name, caps))
+                       util.set_capabilities(target, caps)
+
+
+class InnerTarFileXz(InnerTarFile):
+       @classmethod
+       def open(cls, name=None, mode="r", fileobj=None, **kwargs):
+               fileobj = lzma.LZMAFile(name, mode, fileobj=fileobj)
+
+               try:
+                       t = cls.taropen(name, mode, fileobj, **kwargs)
+               except lzma.LZMAError:
+                       fileobj.close()
+                       raise tarfile.ReadError("not an lzma file")
+
+               t._extfileobj = False
+               return t