Added support for LZMA archives

This commit is contained in:
Bastian Kleineidam 2010-02-22 17:21:55 +01:00
parent ed10997ce3
commit bc7e99b35b
7 changed files with 79 additions and 3 deletions

View File

@ -23,11 +23,11 @@ ArchiveCommands = ('list', 'extract', 'test', 'create')
# Supported archive formats
ArchiveFormats = ('gzip', 'bzip2', 'tar', 'zip', 'compress', '7z', 'rar',
'cab', 'arj', 'cpio', 'rpm', 'deb', 'lzop')
'cab', 'arj', 'cpio', 'rpm', 'deb', 'lzop', 'lzma')
# Supported encodings (used with tar for example)
# Note that all encodings must also be archive formats
ArchiveEncodings = ('gzip', 'bzip2', 'compress')
ArchiveEncodings = ('gzip', 'bzip2', 'compress', 'lzma')
# Map MIME types to archive format
ArchiveMimetypes = {
@ -48,6 +48,7 @@ ArchiveMimetypes = {
'application/x-rpm': 'rpm',
'application/x-debian-package': 'deb',
'application/x-lzop': 'lzop',
'application/xxx': 'lzma',
}
# List of programs supporting the given archive format and command.
@ -116,6 +117,12 @@ ArchivePrograms = {
'lzop': {
None: ('lzop',),
},
'lzma': {
'extract': ('lzma',),
'list': ('echo',),
'test': ('lzma',),
'create': ('lzma',),
},
}
# only list those programs that have different python module names

View File

@ -26,6 +26,9 @@ def list_compress (archive, encoding, cmd, **kwargs):
"""List a compress archive."""
return stripext(cmd, archive)
def list_lzma (archive, encoding, cmd, **kwargs):
"""List a LZMA archive."""
return stripext(cmd, archive)
def stripext (cmd, archive):
"""Echo the name without suffix."""

56
patoolib/programs/lzma.py Normal file
View File

@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2010 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/>.
"""Archive commands for the lzma program."""
import os
from .. import util
def extract_lzma (archive, encoding, cmd, **kwargs):
"""Extract a LZMA archive."""
cmdlist = [cmd]
if kwargs['verbose']:
cmdlist.append('-v')
cmdlist.extend(['-c', '-d'])
cmdlist.append('--')
outfile = os.path.join(kwargs['outdir'], util.stripext(archive))
cmdlist.extend([archive, '>', outfile])
# note that for shell calls the command must be a string
cmd = " ".join([util.shell_quote(x) for x in cmdlist])
return (cmd, {'shell': True})
def test_lzma (archive, encoding, cmd, **kwargs):
"""Test a LZMA archive."""
cmdlist = [cmd]
if kwargs['verbose']:
cmdlist.append('-v')
cmdlist.append('-t')
cmdlist.append('--')
cmdlist.append(archive)
return cmdlist
def create_lzma (archive, encoding, cmd, *args, **kwargs):
"""Create a LZMA archive."""
cmdlist = [cmd]
if kwargs['verbose']:
cmdlist.append('-v')
cmdlist.append('-c')
cmdlist.append('--')
cmdlist.extend(args)
cmdlist.extend(['>', archive])
# note that for shell calls the command must be a string
cmd = " ".join([util.shell_quote(x) for x in cmdlist])
return (cmd, {'shell': True})

View File

@ -22,11 +22,13 @@ import tempfile
import traceback
mimedb = mimetypes.MimeTypes(strict=False)
# add missing encodings for Python <=2.5
# add missing encodings and mimetypes
mimedb.encodings_map['.bz2'] = 'bzip2'
mimedb.encodings_map['.lzma'] = 'lzma'
mimedb.suffix_map['.tbz2'] = '.tar.bz2'
mimedb.add_type('application/x-lzop', '.lzo', strict=False)
mimedb.add_type('application/x-arj', '.arj', strict=False)
mimedb.add_type('application/x-lzma', '.lzma', strict=False)
class PatoolError (StandardError):

BIN
tests/data/t.lzma Normal file

Binary file not shown.

BIN
tests/data/t.tar.lzma Normal file

Binary file not shown.

View File

@ -31,6 +31,7 @@ class TestArchives (ArchiveTest):
self.archive_commands('t.tar.Z', cmd)
self.archive_commands('t.tar.bz2', cmd)
self.archive_commands('t.tbz2', cmd)
# XXXself.archive_commands('t.tar.lzma', cmd)
@needs_cmd('bzip2')
def test_bzip2 (self):
@ -48,6 +49,7 @@ class TestArchives (ArchiveTest):
def test_echo (self):
self.archive_list('t.bz2', 'echo')
self.archive_list('t.Z', 'echo')
self.archive_list('t.lzma', 'echo')
@needs_cmd('unzip')
def test_unzip (self):
@ -154,3 +156,9 @@ class TestArchives (ArchiveTest):
def test_lzop (self):
self.archive_commands('t.lzo', 'lzop', singlefile=True)
@needs_cmd('lzma')
def test_lzma (self):
self.archive_test('t.lzma', 'lzma')
self.archive_extract('t.lzma', 'lzma')
self.archive_create('t.lzma', 'lzma', singlefile=True)