diff --git a/patoolib/__init__.py b/patoolib/__init__.py index 112f905a..e7792afc 100644 --- a/patoolib/__init__.py +++ b/patoolib/__init__.py @@ -23,8 +23,8 @@ # Supported archive formats ArchiveFormats = ('7z', 'ace', 'alzip', 'ar', 'arc', 'arj', 'bzip2', - 'cab', 'compress', 'cpio', 'deb', 'dms', 'gzip', 'lrzip', 'lzh', 'lzip', 'lzma', - 'lzop', 'rar', 'rpm', 'rzip', 'tar', 'xz', 'zip', 'zoo') + 'cab', 'compress', 'cpio', 'deb', 'dms', 'gzip', 'lrzip', 'lzh', + 'lzip', 'lzma', 'lzop', 'rar', 'rpm', 'rzip', 'tar', 'xz', 'zip', 'zoo') # Supported compressions (used with tar for example) # Note that all compressions must also be archive formats @@ -65,17 +65,6 @@ 'application/x-dms': 'dms', } -# List of programs supporting the given compression - -CompressionPrograms = { - 'gzip': ('pigz', 'gzip'), - 'bzip2': ('pbzip2', 'lbzip2', 'bzip2'), - 'compress': ('compress',), - 'lzma': ('lzma',), - 'xz': ('xz',), - 'lzip': ('lzip', 'clzip', 'plzip', 'pdlzip'), -} - # List of programs supporting the given archive format and command. # If command is None, the program supports all commands (list, extract, ...) # Programs starting with "py_" are Python modules. @@ -268,17 +257,16 @@ def find_archive_program (format, command): raise util.PatoolError("could not find an executable program to %s format %s; candidates are (%s)," % (command, format, ",".join(programs))) -def find_compression_program (program, compression): - """Find suitable compression program and return it. Returns None if - no compression program could be found""" - if program in ('tar', 'star'): - for enc_program in CompressionPrograms[compression]: - found = util.find_program(enc_program) - if found: - return found - elif program == 'py_tarfile': +def program_supports_compression (program, compression): + """Decide if the given program supports the compression natively. + @return: True iff the program supports the given compression format + natively, else False. + """ + if program == 'py_tarfile': return compression in ('gzip', 'bzip2') - return None + if program in ('tar', 'star'): + return compression in ('gzip', 'bzip2', 'xz', 'lzma') + return False def list_formats (): @@ -339,10 +327,17 @@ def parse_config (archive, format, compression, command, **kwargs): value = program config[key] = value program = os.path.basename(config['program']) - if compression and not find_compression_program(program, compression): - msg = "cannot %s archive `%s': compression `%s' not supported by %s" % \ - (command, archive, compression, program) - raise util.PatoolError(msg) + if compression: + # check if compression is supported + if not program_supports_compression(program, compression): + if command == 'create': + comp_command = command + else: + comp_command = 'extract' + comp_prog = find_archive_program(compression, comp_command) + if not comp_prog: + msg = "cannot %s archive `%s': compression `%s' not supported" + raise util.PatoolError(msg % (command, archive, compression)) return config diff --git a/tests/__init__.py b/tests/__init__.py index 75288d09..997500b6 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -174,4 +174,4 @@ def has_codec (program, codec): """Test if program supports given codec.""" if program == '7z' and codec == 'rar': return patoolib.util.p7zip_supports_rar() - return patoolib.find_compression_program(program, codec) + return patoolib.program_supports_compression(program, codec) diff --git a/tests/test_archives.py b/tests/test_archives.py index 10b0fa7d..66c5d95a 100644 --- a/tests/test_archives.py +++ b/tests/test_archives.py @@ -28,7 +28,8 @@ def test_tar_gz (self): self.archive_commands('t.tar.gz') self.archive_commands('t.tgz') - @needs_codec('tar', 'compress') + @needs_program('tar') + @needs_program('compress') def test_tar_z (self): self.program = 'tar' self.archive_commands('t.tar.Z') @@ -47,8 +48,8 @@ def test_tar_lzma (self): # even though clzip would support extracting .lz files, the # file(1) --uncompress command does not use it for achive detection + @needs_program('tar') @needs_program('lzip') - @needs_codec('tar', 'lzip') def test_tar_lzip (self): self.program = 'tar' self.archive_commands('t.tar.lz') @@ -69,7 +70,8 @@ def test_star_gz (self): self.archive_commands('t.tar.gz') self.archive_commands('t.tgz') - @needs_codec('star', 'compress') + @needs_program('star') + @needs_program('compress') def test_star_z (self): self.program = 'star' self.archive_commands('t.tar.Z') @@ -86,7 +88,8 @@ def test_star_lzma (self): self.program = 'star' self.archive_commands('t.tar.lzma') - @needs_codec('star', 'lzip') + @needs_program('star') + @needs_program('lzip') def test_star_lzip (self): self.program = 'star' self.archive_commands('t.tar.lz') diff --git a/tests/test_config.py b/tests/test_config.py index dd19f915..50b29617 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -33,8 +33,8 @@ def test_archive_programs (self): self.assertTrue(command in patoolib.ArchiveCommands) def test_compression_programs (self): - self.assertEqual(set(patoolib.ArchiveCompressions), - set(patoolib.CompressionPrograms.keys())) + self.assertTrue(set(patoolib.ArchiveCompressions).issubset( + set(patoolib.ArchiveFormats))) def test_encoding_mimes (self): self.assertEqual(set(patoolib.ArchiveCompressions),