Fixed python lzma handling.

This commit is contained in:
Bastian Kleineidam 2013-02-22 18:36:45 +01:00
parent 20813f3520
commit ba8d14c4b5
4 changed files with 63 additions and 6 deletions

View File

@ -235,6 +235,8 @@ ArchivePrograms = {
}, },
'xz': { 'xz': {
None: ('xz',), None: ('xz',),
'extract': py_lzma,
'create': py_lzma,
}, },
'zoo': { 'zoo': {
None: ('zoo',), None: ('zoo',),

View File

@ -19,14 +19,14 @@ import lzma
READ_SIZE_BYTES = 1024*1024 READ_SIZE_BYTES = 1024*1024
def extract_lzma(archive, compression, cmd, **kwargs): def _extract(archive, compression, cmd, format, **kwargs):
"""Extract a LZMA archive with the lzma Python module.""" """Extract an LZMA or XZ archive with the lzma Python module."""
verbose = kwargs['verbose'] verbose = kwargs['verbose']
outdir = kwargs['outdir'] outdir = kwargs['outdir']
if verbose: if verbose:
util.log_info('extracting %s...' % archive) util.log_info('extracting %s...' % archive)
targetname = util.get_single_outfile(outdir, archive) targetname = util.get_single_outfile(outdir, archive)
lzmafile = lzma.LZMAFile(archive) lzmafile = lzma.LZMAFile(archive, format=format)
try: try:
with open(targetname, 'wb') as targetfile: with open(targetname, 'wb') as targetfile:
data = lzmafile.read(READ_SIZE_BYTES) data = lzmafile.read(READ_SIZE_BYTES)
@ -39,15 +39,23 @@ def extract_lzma(archive, compression, cmd, **kwargs):
util.log_info('... extracted to %s' % targetname) util.log_info('... extracted to %s' % targetname)
return None 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): def extract_xz(archive, compression, cmd, **kwargs):
"""Create a LZMA archive with the lzma Python module.""" """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'] verbose = kwargs['verbose']
if verbose: if verbose:
util.log_info('creating %s...' % archive) util.log_info('creating %s...' % archive)
if len(args) > 1: if len(args) > 1:
util.log_error('multi-file compression not supported in Python lzma') util.log_error('multi-file compression not supported in Python lzma')
lzmafile = lzma.LZMAFile(archive, 'wb') lzmafile = lzma.LZMAFile(archive, 'wb', format)
try: try:
filename = args[0] filename = args[0]
with open(filename) as srcfile: with open(filename) as srcfile:
@ -60,3 +68,11 @@ def create_lzma(archive, compression, cmd, *args, **kwargs):
finally: finally:
lzmafile.close() lzmafile.close()
return None 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)

View File

@ -163,6 +163,8 @@ class ArchiveTest (unittest.TestCase):
program = 'gzip' program = 'gzip'
elif self.program == 'py_bz2': elif self.program == 'py_bz2':
program = 'bzip2' program = 'bzip2'
elif self.program == 'py_lzma':
program = 'xz'
elif self.program == 'zip': elif self.program == 'zip':
program = 'unzip' program = 'unzip'
elif self.program in ('rzip', 'shorten'): elif self.program in ('rzip', 'shorten'):

View File

@ -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 <http://www.gnu.org/licenses/>.
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)