From cd808413dd61c930aacab707a0f7844fd61359e7 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 15 Dec 2012 16:45:23 +0100 Subject: [PATCH] Move code for tarfile handling in an own file. --- python/pakfire/packages/__init__.py | 2 +- python/pakfire/packages/file.py | 89 +--------------------- python/pakfire/packages/packager.py | 33 ++++---- python/pakfire/packages/tar.py | 114 ++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 103 deletions(-) create mode 100644 python/pakfire/packages/tar.py diff --git a/python/pakfire/packages/__init__.py b/python/pakfire/packages/__init__.py index 9aabddccb..414599e87 100644 --- a/python/pakfire/packages/__init__.py +++ b/python/pakfire/packages/__init__.py @@ -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 diff --git a/python/pakfire/packages/file.py b/python/pakfire/packages/file.py index 37f820e2b..26d91d3b9 100644 --- a/python/pakfire/packages/file.py +++ b/python/pakfire/packages/file.py @@ -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 diff --git a/python/pakfire/packages/packager.py b/python/pakfire/packages/packager.py index 1cf1ecf7e..a4566028e 100644 --- a/python/pakfire/packages/packager.py +++ b/python/pakfire/packages/packager.py @@ -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 index 000000000..2cdc1b0c9 --- /dev/null +++ b/python/pakfire/packages/tar.py @@ -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 . # +# # +############################################################################### + +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 -- 2.39.5