Added test archive command.

This commit is contained in:
Bastian Kleineidam 2010-02-21 13:40:42 +01:00
parent c1ada5cc39
commit 2587bfda86
20 changed files with 204 additions and 30 deletions

View File

@ -15,9 +15,11 @@
.SH NAME
patool - simple manager for file archives of various types
.SH SYNOPSIS
\fBpatool\fP <\fIcommand\fP> [\fIoptions\fP] <\fIarchive-file\fP>
\fBpatool\fP (\fBextract\fP|\fBlist\fP|\fBtest\fP) [\fIoptions\fP] <\fIarchive-file\fP>
\fBpatool\fP \fBcreate\fP [\fIoptions\fP] <\fIarchive-file\fP> [\fIfiles..\fP]
\fBpatool\fP \fBformats\fP [\fIoptions\fP]
.SH DESCRIPTION
Various archive types can be created, extracted and listed by
Various archive types can be created, extracted, tested and listed by
\fBpatool\fP.
The archive format is determined by the archive file extension and
as a fallback with file(1).
@ -54,6 +56,14 @@ Verbose archive listing (if the helper application supports it).
.TP
\fB\-\-help\fP
Show help for this command.
.SS \fBtest\fP
Test files in an archive.
.TP
\fB\-\-verbose\fP
Verbose archive testing (if the helper application supports it).
.TP
\fB\-\-help\fP
Show help for this command.
.SS \fBformats\fP
Show all supported archive formats.
.TP
@ -81,7 +91,7 @@ Supported archive formats are listed by the \fBformats\fP command.
.TP
\fImode\fP\fB=/usr/bin/mycommand\fP
Set the application to handle the archive format for given mode.
\fImode\fP can be one of \fBextract\fP, \fBlist\fP.
\fImode\fP can be one of \fBextract\fP, \fBlist\fP, \fBtest\fP.
.SH FILES
\fB/etc/patool.conf\fP, \fB~/.patool.conf\fP -
configuration files

View File

@ -6,20 +6,23 @@ NAME
patool - simple manager for file archives of various types
SYNOPSIS
patool <command> [options] <archive-file>
patool (extract|list|test) [options] <archive-file>
patool create [options] <archive-file> [files..]
patool formats [options]
DESCRIPTION
Various archive types can be created, extracted and listed by patool. The archive format is determined by the archive file extension and as a fallback with file(1).
Various archive types can be created, extracted, tested and listed by patool. The archive format is determined by the archive file extension and as a fallback with
file(1).
patool supports 7z (.7z), ZIP (.zip, .jar), GZIP (.gz), compress (.Z), BZIP2 (.bz2), TAR (.tar), ARJ (.arj), CAB (.cab), CPIO (.cpio), RPM (.rpm), DEB (.deb), LZOP
patool supports 7z (.7z), ZIP (.zip, .jar), GZIP (.gz), compress (.Z), BZIP2 (.bz2), TAR (.tar), ARJ (.arj), CAB (.cab), CPIO (.cpio), RPM (.rpm), DEB (.deb), LZOP
(.lzo)and RAR (.rar) formats. It relies on helper applications to handle those archive formats (for example bzip2 for BZIP2 archives).
COMMANDS
Several commands and options are available.
extract
Extract files from an archive4. Often one wants to extract all files in an archive to a single subdirectory. However, some archives contain multiple files in their
root directories. The patool program overcomes this problem by first extracting files to a unique (temporary) directory, and then moving its contents back if possible.
Extract files from an archive4. Often one wants to extract all files in an archive to a single subdirectory. However, some archives contain multiple files in their
root directories. The patool program overcomes this problem by first extracting files to a unique (temporary) directory, and then moving its contents back if possible.
This also prevents local files from being overwritten by mistake.
--force
@ -38,6 +41,14 @@ COMMANDS
--help Show help for this command.
test
Test files in an archive.
--verbose
Verbose archive testing (if the helper application supports it).
--help Show help for this command.
formats
Show all supported archive formats.
@ -61,7 +72,7 @@ CONFIGURATION
Supported archive formats are listed by the formats command.
mode=/usr/bin/mycommand
Set the application to handle the archive format for given mode. mode can be one of extract, list.
Set the application to handle the archive format for given mode. mode can be one of extract, list, test.
FILES
/etc/patool.conf, ~/.patool.conf - configuration files

6
patool
View File

@ -35,6 +35,12 @@ def list (archive, verbose=False):
return patoolib.handle_archive(archive, 'list')
@patoolib.baker.command
def test (archive, verbose=False):
"""Test files in an archive."""
return patoolib.handle_archive(archive, 'test', verbose=verbose)
#@patoolib.baker.command
#def create (archive, *args):
# """Create an archive from given files."""

View File

@ -19,7 +19,7 @@ from distutils.spawn import find_executable
from . import util, baker
# Supported archive commands
ArchiveCommands = ('list', 'extract')
ArchiveCommands = ('list', 'extract', 'test')
# Supported archive formats
ArchiveFormats = ('gzip', 'bzip2', 'tar', 'zip', 'compress', '7z', 'rar',
@ -55,6 +55,7 @@ ArchiveMimetypes = {
ArchivePrograms = {
'bzip2': {
'extract': ('pbzip2', 'bzip2', '7z'),
'test': ('pbzip2', 'bzip2', '7z'),
'list': ('7z', 'echo',),
},
'tar': {
@ -63,6 +64,7 @@ ArchivePrograms = {
'zip': {
'extract': ('unzip', '7z'),
'list': ('unzip', '7z'),
'test': ('unzip', '7z'),
},
'gzip': {
None: ('gzip', '7z'),
@ -70,6 +72,7 @@ ArchivePrograms = {
'compress': {
'extract': ('gzip', '7z', 'uncompress.real'),
'list': ('7z', 'echo',),
'test': ('gzip', '7z'),
},
'7z': {
None: ('7z',),
@ -78,27 +81,33 @@ ArchivePrograms = {
None: ('rar',),
'extract': ('unrar', '7z'),
'list': ('unrar', '7z'),
'test': ('unrar', '7z'),
},
'cab': {
'extract': ('cabextract', '7z'),
'list': ('cabextract', '7z'),
'test': ('cabextract', '7z'),
},
'arj': {
'extract': ('arj', '7z'),
'list': ('arj', '7z'),
'test': ('arj', '7z'),
},
'cpio': {
'extract': ('cpio', '7z'),
'list': ('cpio', '7z'),
'test': ('7z',),
},
'rpm': {
# XXX rpm2cpio depends on cpio which is not checked
'extract': ('rpm2cpio', '7z'),
'list': ('rpm', '7z'),
'test': ('rpm', '7z'),
},
'deb': {
'extract': ('dpkg-deb', '7z'),
'list': ('dpkg-deb', '7z'),
'test': ('dpkg-deb', '7z'),
},
'lzop': {
None: ('lzop',),

View File

@ -21,8 +21,8 @@ def extract_arj (archive, encoding, cmd, **kwargs):
cmdlist.append('x')
cmdlist.append('-r')
cmdlist.append('-y')
if kwargs['verbose']:
cmdlist.append('-v')
if not kwargs['verbose']:
cmdlist.append('-i-')
cmdlist.extend([archive, kwargs['outdir']])
return cmdlist
@ -33,6 +33,18 @@ def list_arj (archive, encoding, cmd, **kwargs):
cmdlist.append('v')
else:
cmdlist.append('l')
cmdlist.append('-i-')
cmdlist.append('-r')
cmdlist.append('-y')
cmdlist.extend([archive])
return cmdlist
def test_arj (archive, encoding, cmd, **kwargs):
"""Test a ARJ archive."""
cmdlist = [cmd]
cmdlist.append('t')
if not kwargs['verbose']:
cmdlist.append('-i-')
cmdlist.append('-r')
cmdlist.append('-y')
cmdlist.extend([archive])

View File

@ -30,3 +30,12 @@ def extract_bzip2 (archive, encoding, cmd, **kwargs):
# 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_bzip2 (archive, encoding, cmd, **kwargs):
cmdlist = [cmd]
if kwargs['verbose']:
cmdlist.append('-v')
cmdlist.extend(['-t', '--'])
cmdlist.extend([archive])
return cmdlist

View File

@ -32,3 +32,10 @@ def list_cab (archive, encoding, cmd, **kwargs):
cmdlist.append('-v')
cmdlist.extend([archive])
return cmdlist
def test_cab (archive, encoding, cmd, **kwargs):
"""Test a CAB archive."""
cmdlist = [cmd]
cmdlist.append('-t')
cmdlist.extend([archive])
return cmdlist

View File

@ -33,3 +33,6 @@ def list_deb (archive, encoding, cmd, **kwargs):
cmdlist.append('--')
cmdlist.extend([archive])
return cmdlist
test_deb = list_deb

View File

@ -43,3 +43,15 @@ def list_gzip (archive, encoding, cmd, **kwargs):
cmdlist.append(archive)
return cmdlist
def test_gzip (archive, encoding, cmd, **kwargs):
"""Test a GZIP archive."""
cmdlist = [cmd]
if kwargs['verbose']:
cmdlist.append('-v')
cmdlist.append('-t')
cmdlist.append('--')
cmdlist.append(archive)
return cmdlist
test_compress = test_gzip

View File

@ -32,3 +32,12 @@ def list_lzop (archive, encoding, cmd, **kwargs):
cmdlist.append('--verbose')
cmdlist.extend(['--', archive])
return cmdlist
def test_lzop (archive, encoding, cmd, **kwargs):
"""Test a LZOP archive."""
cmdlist = [cmd]
cmdlist.append('--test')
if kwargs['verbose']:
cmdlist.append('--verbose')
cmdlist.extend(['--', archive])
return cmdlist

View File

@ -59,3 +59,26 @@ list_bzip2 = \
list_rpm = \
list_deb = \
list_7z
def test_7z (archive, encoding, cmd, **kwargs):
"""Test a 7z archive."""
cmdlist = [cmd]
cmdlist.append('t')
if not kwargs['verbose']:
cmdlist.append('-bd')
cmdlist.append('--')
cmdlist.append(archive)
return cmdlist
test_bzip2 = \
test_gzip = \
test_zip = \
test_compress = \
test_rar = \
test_cab = \
test_arj = \
test_cpio = \
test_rpm = \
test_deb = \
test_7z

View File

@ -15,4 +15,4 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Archive commands for the pbzip2 program."""
# bzip2 and pbzip2 are compatible
from .bzip2 import extract_bzip2
from .bzip2 import extract_bzip2, test_bzip2

View File

@ -34,3 +34,12 @@ def list_rar (archive, encoding, cmd, **kwargs):
cmdlist.append('-c-')
cmdlist.extend(['--', archive])
return cmdlist
def test_rar (archive, encoding, cmd, **kwargs):
"""Test a RAR archive."""
cmdlist = [cmd]
cmdlist.append('t')
if not kwargs['verbose']:
cmdlist.append('-c-')
cmdlist.extend(['--', archive])
return cmdlist

View File

@ -23,3 +23,12 @@ def list_rpm (archive, encoding, cmd, **kwargs):
cmdlist.append('-v')
cmdlist.extend(['-p', '--', archive])
return cmdlist
def test_rpm (archive, encoding, cmd, **kwargs):
"""Test a RPM archive."""
cmdlist = [cmd]
cmdlist.append('-V')
if kwargs['verbose']:
cmdlist.append('-v')
cmdlist.extend(['-p', '--', archive])
return cmdlist

View File

@ -44,3 +44,5 @@ def list_tar (archive, encoding, cmd, **kwargs):
cmdlist.append('-v')
cmdlist.append("file=%s" % archive)
return cmdlist
test_tar = list_tar

View File

@ -34,3 +34,5 @@ def list_tar (archive, encoding, cmd, **kwargs):
cmdlist.append('--verbose')
cmdlist.extend(["--file", archive])
return cmdlist
test_tar = list_tar

View File

@ -34,3 +34,12 @@ def list_rar (archive, encoding, cmd, **kwargs):
cmdlist.append('-c-')
cmdlist.extend(['--', archive])
return cmdlist
def test_rar (archive, encoding, cmd, **kwargs):
"""Test a RAR archive."""
cmdlist = [cmd]
cmdlist.append('t')
if not kwargs['verbose']:
cmdlist.append('-c-')
cmdlist.extend(['--', archive])
return cmdlist

View File

@ -31,3 +31,12 @@ def list_zip (archive, encoding, cmd, **kwargs):
cmdlist.append('-v')
cmdlist.extend(['--', archive])
return cmdlist
def test_zip (archive, encoding, cmd, **kwargs):
"""Test a ZIP archive."""
cmdlist = [cmd]
cmdlist.append('-t')
if kwargs['verbose']:
cmdlist.append('-v')
cmdlist.extend(['--', archive])
return cmdlist

View File

@ -26,8 +26,9 @@ datadir = os.path.join(basedir, 'data')
class ArchiveTest (unittest.TestCase):
"""Helper class for achive tests."""
def archive_test (self, filename, cmd):
def archive_commands (self, filename, cmd):
self.archive_list(filename, cmd)
self.archive_test(filename, cmd)
self.archive_extract(filename, cmd)
def archive_extract (self, filename, cmd):
@ -36,16 +37,21 @@ class ArchiveTest (unittest.TestCase):
tmpdir = patoolib.util.tmpdir(dir=basedir)
os.chdir(tmpdir)
try:
patoolib._handle_archive(archive, 'extract', cmd=cmd)
patoolib._handle_archive(archive, 'extract', cmd=cmd, force=True)
patoolib._handle_archive(archive, 'extract', program=cmd)
patoolib._handle_archive(archive, 'extract', program=cmd, force=True)
finally:
os.chdir(basedir)
shutil.rmtree(tmpdir)
def archive_list (self, filename, cmd):
archive = os.path.join(datadir, filename)
patoolib._handle_archive(archive, 'list', cmd=cmd)
patoolib._handle_archive(archive, 'list', cmd=cmd, verbose=True)
patoolib._handle_archive(archive, 'list', program=cmd)
patoolib._handle_archive(archive, 'list', program=cmd, verbose=True)
def archive_test (self, filename, cmd):
archive = os.path.join(datadir, filename)
patoolib._handle_archive(archive, 'test', program=cmd)
patoolib._handle_archive(archive, 'test', program=cmd, verbose=True)
def needs_cmd (cmd):

View File

@ -26,19 +26,21 @@ class TestArchives (ArchiveTest):
self.tar_test('star')
def tar_test (self, cmd):
self.archive_test('t.tar', cmd)
self.archive_test('t.tar.gz', cmd)
self.archive_test('t.tar.Z', cmd)
self.archive_test('t.tar.bz2', cmd)
self.archive_test('t.tbz2', cmd)
self.archive_commands('t.tar', cmd)
self.archive_commands('t.tar.gz', cmd)
self.archive_commands('t.tar.Z', cmd)
self.archive_commands('t.tar.bz2', cmd)
self.archive_commands('t.tbz2', cmd)
@needs_cmd('bzip2')
def test_bzip2 (self):
self.archive_extract('t.bz2', 'bzip2')
self.archive_test('t.bz2', 'bzip2')
@needs_cmd('pbzip2')
def test_pbzip2 (self):
self.archive_extract('t.bz2', 'pbzip2')
self.archive_test('t.bz2', 'pbzip2')
@needs_cmd('echo')
def test_echo (self):
@ -47,13 +49,13 @@ class TestArchives (ArchiveTest):
@needs_cmd('unzip')
def test_unzip (self):
self.archive_test('t.zip', 'unzip')
self.archive_test('t.jar', 'unzip')
self.archive_commands('t.zip', 'unzip')
self.archive_commands('t.jar', 'unzip')
@needs_cmd('gzip')
def test_gzip (self):
self.archive_test('t.gz', 'gzip')
self.archive_test('t.txt.gz', 'gzip')
self.archive_commands('t.gz', 'gzip')
self.archive_commands('t.txt.gz', 'gzip')
self.archive_extract('t.Z', 'gzip')
@needs_cmd('uncompress.real')
@ -62,7 +64,7 @@ class TestArchives (ArchiveTest):
@needs_cmd('7z')
def test_p7zip (self):
self.archive_test('t.7z', '7z')
self.archive_commands('t.7z', '7z')
self.archive_list('t.gz', '7z')
self.archive_list('t.bz2', '7z')
self.archive_list('t.zip', '7z')
@ -85,6 +87,17 @@ class TestArchives (ArchiveTest):
self.archive_extract('t.cpio', '7z')
self.archive_extract('t.rpm', '7z')
self.archive_extract('t.deb', '7z')
self.archive_test('t.gz', '7z')
self.archive_test('t.bz2', '7z')
self.archive_test('t.zip', '7z')
self.archive_test('t.jar', '7z')
self.archive_test('t.Z', '7z')
self.archive_test('t.rar', '7z')
self.archive_test('t.cab', '7z')
self.archive_test('t.arj', '7z')
self.archive_test('t.cpio', '7z')
self.archive_test('t.rpm', '7z')
self.archive_test('t.deb', '7z')
@needs_cmd('unrar')
def test_unrar (self):
@ -93,7 +106,7 @@ class TestArchives (ArchiveTest):
@needs_cmd('rar')
def test_rar (self):
self.archive_test('t.rar', 'rar')
self.archive_commands('t.rar', 'rar')
@needs_cmd('cabextract')
def test_capextract (self):
@ -103,6 +116,7 @@ class TestArchives (ArchiveTest):
@needs_cmd('arj')
def test_arj (self):
self.archive_list('t.arj', 'arj')
self.archive_test('t.arj', 'arj')
self.archive_extract('t.arj', 'arj')
@needs_cmd('cpio')
@ -113,6 +127,8 @@ class TestArchives (ArchiveTest):
@needs_cmd('rpm')
def test_rpm (self):
self.archive_list('t.rpm', 'rpm')
# the rpm test fails on non-rpm system with missing dependencies
#self.archive_test('t.rpm', 'rpm')
@needs_cmd('rpm2cpio')
@needs_cmd('cpio')
@ -123,8 +139,9 @@ class TestArchives (ArchiveTest):
def test_dpkg (self):
self.archive_list('t.deb', 'dpkg')
self.archive_extract('t.deb', 'dpkg')
self.archive_test('t.deb', 'dpkg')
@needs_cmd('lzop')
def test_lzop (self):
self.archive_test('t.lzo', 'lzop')
self.archive_commands('t.lzo', 'lzop')