From ba8d14c4b533dd3aa4c9277bfe3d28060993b87d Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Fri, 22 Feb 2013 18:36:45 +0100 Subject: [PATCH] Fixed python lzma handling. --- patoolib/__init__.py | 2 ++ patoolib/programs/py_lzma.py | 28 ++++++++++++++++++++------ tests/archives/__init__.py | 2 ++ tests/archives/test_pylzma.py | 37 +++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 tests/archives/test_pylzma.py diff --git a/patoolib/__init__.py b/patoolib/__init__.py index 66c6d6d..103752c 100644 --- a/patoolib/__init__.py +++ b/patoolib/__init__.py @@ -235,6 +235,8 @@ ArchivePrograms = { }, 'xz': { None: ('xz',), + 'extract': py_lzma, + 'create': py_lzma, }, 'zoo': { None: ('zoo',), diff --git a/patoolib/programs/py_lzma.py b/patoolib/programs/py_lzma.py index c851934..d02a146 100644 --- a/patoolib/programs/py_lzma.py +++ b/patoolib/programs/py_lzma.py @@ -19,14 +19,14 @@ import lzma READ_SIZE_BYTES = 1024*1024 -def extract_lzma(archive, compression, cmd, **kwargs): - """Extract a LZMA archive with the lzma Python module.""" +def _extract(archive, compression, cmd, format, **kwargs): + """Extract an LZMA or XZ archive with the lzma Python module.""" verbose = kwargs['verbose'] outdir = kwargs['outdir'] if verbose: util.log_info('extracting %s...' % archive) targetname = util.get_single_outfile(outdir, archive) - lzmafile = lzma.LZMAFile(archive) + lzmafile = lzma.LZMAFile(archive, format=format) try: with open(targetname, 'wb') as targetfile: data = lzmafile.read(READ_SIZE_BYTES) @@ -39,15 +39,23 @@ def extract_lzma(archive, compression, cmd, **kwargs): util.log_info('... extracted to %s' % targetname) return None +def extract_lzma(archive, compression, cmd, **kwargs): + """Extract an LZMA archive with the lzma Python module.""" + return _extract(archive, compression, cmd, lzma.FORMAT_ALONE, **kwargs) -def create_lzma(archive, compression, cmd, *args, **kwargs): - """Create a LZMA archive with the lzma Python module.""" +def extract_xz(archive, compression, cmd, **kwargs): + """Extract an XZ archive with the lzma Python module.""" + return _extract(archive, compression, cmd, lzma.FORMAT_XZ, **kwargs) + + +def _create(archive, compression, cmd, format, *args, **kwargs): + """Create an LZMA or XZ archive with the lzma Python module.""" verbose = kwargs['verbose'] if verbose: util.log_info('creating %s...' % archive) if len(args) > 1: util.log_error('multi-file compression not supported in Python lzma') - lzmafile = lzma.LZMAFile(archive, 'wb') + lzmafile = lzma.LZMAFile(archive, 'wb', format) try: filename = args[0] with open(filename) as srcfile: @@ -60,3 +68,11 @@ def create_lzma(archive, compression, cmd, *args, **kwargs): finally: lzmafile.close() return None + +def create_lzma(archive, compression, cmd, *args, **kwargs): + """Create an LZMA archive with the lzma Python module.""" + return _create(archive, compression, cmd, lzma.FORMAT_ALONE, *args, **kwargs) + +def create_xz(archive, compression, cmd, *args, **kwargs): + """Create an XZ archive with the lzma Python module.""" + return _create(archive, compression, cmd, lzma.FORMAT_XZ, *args, **kwargs) diff --git a/tests/archives/__init__.py b/tests/archives/__init__.py index 721ab6f..ed16de3 100644 --- a/tests/archives/__init__.py +++ b/tests/archives/__init__.py @@ -163,6 +163,8 @@ class ArchiveTest (unittest.TestCase): program = 'gzip' elif self.program == 'py_bz2': program = 'bzip2' + elif self.program == 'py_lzma': + program = 'xz' elif self.program == 'zip': program = 'unzip' elif self.program in ('rzip', 'shorten'): diff --git a/tests/archives/test_pylzma.py b/tests/archives/test_pylzma.py new file mode 100644 index 0000000..f5803ed --- /dev/null +++ b/tests/archives/test_pylzma.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2013 Bastian Kleineidam +# +# 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 . +from . import ArchiveTest, Content +from .. import needs_program, needs_module + +class TestPylzma (ArchiveTest): + + program = 'py_lzma' + + @needs_program('xz') + @needs_module('lzma') + def test_py_lzma (self): + self.archive_extract('t.txt.lzma', check=Content.Singlefile) + self.archive_extract('t.txt.xz', check=Content.Singlefile) + # xz is used to test the created archive + self.archive_create('t.txt.lzma', check=Content.Singlefile) + self.archive_create('t.txt.xz', check=Content.Singlefile) + + @needs_program('file') + @needs_module('lzma') + def test_py_lzma_file (self): + self.archive_extract('t.txt.lzma.foo', check=Content.Singlefile) + self.archive_extract('t.txt.xz.foo', check=Content.Singlefile) +