diff --git a/patoolib/__init__.py b/patoolib/__init__.py index d1ec273..3cedd62 100644 --- a/patoolib/__init__.py +++ b/patoolib/__init__.py @@ -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 diff --git a/patoolib/programs/echo.py b/patoolib/programs/echo.py index b21e35f..e2165ae 100644 --- a/patoolib/programs/echo.py +++ b/patoolib/programs/echo.py @@ -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.""" diff --git a/patoolib/programs/lzma.py b/patoolib/programs/lzma.py new file mode 100644 index 0000000..c348704 --- /dev/null +++ b/patoolib/programs/lzma.py @@ -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 . +"""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}) + diff --git a/patoolib/util.py b/patoolib/util.py index 4bc090d..727c438 100644 --- a/patoolib/util.py +++ b/patoolib/util.py @@ -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): diff --git a/tests/data/t.lzma b/tests/data/t.lzma new file mode 100644 index 0000000..d22d003 Binary files /dev/null and b/tests/data/t.lzma differ diff --git a/tests/data/t.tar.lzma b/tests/data/t.tar.lzma new file mode 100644 index 0000000..462abec Binary files /dev/null and b/tests/data/t.tar.lzma differ diff --git a/tests/test_archives.py b/tests/test_archives.py index c53b1e8..b3938ee 100644 --- a/tests/test_archives.py +++ b/tests/test_archives.py @@ -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) +