Code cleanup: remove CompressionPrograms map.

This commit is contained in:
Bastian Kleineidam 2012-05-17 09:08:28 +02:00
parent 0f976ef02a
commit b3b48adf39
4 changed files with 32 additions and 34 deletions

View File

@ -23,8 +23,8 @@ ArchiveCommands = ('list', 'extract', 'test', 'create')
# 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 @@ ArchiveMimetypes = {
'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

View File

@ -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)

View File

@ -28,7 +28,8 @@ class TestArchives (ArchiveTest):
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 @@ class TestArchives (ArchiveTest):
# 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 @@ class TestArchives (ArchiveTest):
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 @@ class TestArchives (ArchiveTest):
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')

View File

@ -33,8 +33,8 @@ class TestConfiguration (unittest.TestCase):
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),