2010-02-21 11:14:57 +00:00
|
|
|
# -*- 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/>.
|
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import nose
|
|
|
|
import patoolib
|
|
|
|
|
|
|
|
basedir = os.path.dirname(__file__)
|
|
|
|
datadir = os.path.join(basedir, 'data')
|
|
|
|
|
|
|
|
class ArchiveTest (unittest.TestCase):
|
|
|
|
"""Helper class for achive tests."""
|
|
|
|
|
2010-02-22 20:30:47 +00:00
|
|
|
def __init__ (self, *args):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Initialize this archive test."""
|
2010-02-22 20:30:47 +00:00
|
|
|
super(ArchiveTest, self).__init__(*args)
|
|
|
|
# set program to use for archive handling
|
|
|
|
self.program = None
|
2010-02-21 11:14:57 +00:00
|
|
|
|
2010-03-03 19:13:03 +00:00
|
|
|
def archive_commands (self, filename, **kwargs):
|
|
|
|
"""Run archive commands list, test, extract and create.
|
|
|
|
All keyword arguments are delegated to the create test function."""
|
2010-02-22 20:30:47 +00:00
|
|
|
self.archive_list(filename)
|
|
|
|
self.archive_test(filename)
|
|
|
|
self.archive_extract(filename)
|
2010-03-03 19:13:03 +00:00
|
|
|
self.archive_create(filename, **kwargs)
|
2010-02-22 20:30:47 +00:00
|
|
|
|
|
|
|
def archive_extract (self, filename):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test archive extraction."""
|
2010-02-21 11:14:57 +00:00
|
|
|
archive = os.path.join(datadir, filename)
|
|
|
|
# create a temporary directory for extraction
|
|
|
|
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
2010-02-22 20:30:47 +00:00
|
|
|
patoolib._handle_archive(archive, 'extract', program=self.program)
|
2010-03-06 13:01:02 +00:00
|
|
|
patoolib._handle_archive(archive, 'extract', program=self.program, verbose=True)
|
2010-02-21 11:14:57 +00:00
|
|
|
finally:
|
|
|
|
os.chdir(basedir)
|
|
|
|
shutil.rmtree(tmpdir)
|
|
|
|
|
2010-02-22 20:30:47 +00:00
|
|
|
def archive_list (self, filename):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test archive listing."""
|
2010-02-21 11:14:57 +00:00
|
|
|
archive = os.path.join(datadir, filename)
|
2010-02-22 20:30:47 +00:00
|
|
|
patoolib._handle_archive(archive, 'list', program=self.program)
|
|
|
|
patoolib._handle_archive(archive, 'list', program=self.program, verbose=True)
|
2010-02-21 12:40:42 +00:00
|
|
|
|
2010-02-22 20:30:47 +00:00
|
|
|
def archive_test (self, filename):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test archive testing."""
|
2010-02-21 12:40:42 +00:00
|
|
|
archive = os.path.join(datadir, filename)
|
2010-02-22 20:30:47 +00:00
|
|
|
patoolib._handle_archive(archive, 'test', program=self.program)
|
|
|
|
patoolib._handle_archive(archive, 'test', program=self.program, verbose=True)
|
2010-02-21 11:14:57 +00:00
|
|
|
|
2010-03-03 19:13:03 +00:00
|
|
|
def archive_create (self, filename, singlefile=False, format=None, encoding=None):
|
|
|
|
"""Test archive creation."""
|
2010-02-21 14:48:52 +00:00
|
|
|
# the file or directory to pack
|
|
|
|
if singlefile:
|
|
|
|
topack = os.path.join(datadir, 'foo.txt')
|
|
|
|
else:
|
|
|
|
topack = os.path.join(datadir, 'foo')
|
2010-03-03 19:13:03 +00:00
|
|
|
# The format and encoding arguments are needed for creating
|
|
|
|
# archives with unusual file extensions.
|
|
|
|
kwargs = dict(
|
|
|
|
program=self.program,
|
|
|
|
format=format,
|
2010-03-06 15:52:13 +00:00
|
|
|
encoding=encoding,
|
2010-03-03 19:13:03 +00:00
|
|
|
)
|
2010-03-06 13:01:02 +00:00
|
|
|
self._archive_create(filename, topack, kwargs)
|
|
|
|
kwargs['verbose'] = True
|
|
|
|
self._archive_create(filename, topack, kwargs)
|
|
|
|
|
|
|
|
def _archive_create (self, filename, topack, kwargs):
|
|
|
|
"""Create archive from filename."""
|
|
|
|
# create a temporary directory for creation
|
|
|
|
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
|
|
|
archive = os.path.join(tmpdir, filename)
|
|
|
|
os.chdir(tmpdir)
|
2010-02-21 14:48:52 +00:00
|
|
|
try:
|
2010-03-03 19:13:03 +00:00
|
|
|
patoolib._handle_archive(archive, 'create', topack, **kwargs)
|
2010-03-06 18:21:20 +00:00
|
|
|
self.assertTrue(os.path.isfile(archive))
|
2010-02-21 14:48:52 +00:00
|
|
|
# not all programs can test what they create
|
2010-02-22 20:30:47 +00:00
|
|
|
if self.program == 'compress':
|
|
|
|
program = 'gzip'
|
2010-03-03 21:04:06 +00:00
|
|
|
elif self.program == 'zip':
|
|
|
|
program = 'unzip'
|
2010-02-22 20:30:47 +00:00
|
|
|
else:
|
|
|
|
program = self.program
|
|
|
|
patoolib._handle_archive(archive, 'test', program=program)
|
2010-02-21 14:48:52 +00:00
|
|
|
finally:
|
|
|
|
os.chdir(basedir)
|
|
|
|
shutil.rmtree(tmpdir)
|
|
|
|
|
2010-02-21 11:14:57 +00:00
|
|
|
|
2010-02-22 20:30:47 +00:00
|
|
|
def needs_program (program):
|
|
|
|
"""Decorator skipping test if given program is not available."""
|
2010-02-21 11:14:57 +00:00
|
|
|
def check_prog (f):
|
|
|
|
def newfunc (*args, **kwargs):
|
2010-03-01 15:10:17 +00:00
|
|
|
if not patoolib.util.find_program(program):
|
2010-02-22 20:30:47 +00:00
|
|
|
raise nose.SkipTest("program `%s' not available" % program)
|
2010-02-21 11:14:57 +00:00
|
|
|
return f(*args, **kwargs)
|
|
|
|
newfunc.func_name = f.func_name
|
|
|
|
return newfunc
|
|
|
|
return check_prog
|
2010-02-22 20:55:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def needs_codec (program, codec):
|
|
|
|
"""Decorator skipping test if given program codec is not available."""
|
|
|
|
def check_prog (f):
|
|
|
|
def newfunc (*args, **kwargs):
|
2010-03-01 15:10:17 +00:00
|
|
|
if not patoolib.util.find_program(program):
|
2010-02-23 06:32:13 +00:00
|
|
|
raise nose.SkipTest("program `%s' not available" % program)
|
2010-03-01 15:18:36 +00:00
|
|
|
if not has_codec(program, codec):
|
2010-02-22 20:55:29 +00:00
|
|
|
raise nose.SkipTest("codec `%s' for program `%s' not available" % (codec, program))
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
newfunc.func_name = f.func_name
|
|
|
|
return newfunc
|
|
|
|
return check_prog
|
|
|
|
|
|
|
|
|
2010-03-01 15:18:36 +00:00
|
|
|
def has_codec (program, codec):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test if program supports given codec."""
|
2010-02-22 20:55:29 +00:00
|
|
|
if program == '7z' and codec == 'rar':
|
2010-03-01 15:05:32 +00:00
|
|
|
return patoolib.util.p7zip_supports_rar()
|
2010-02-23 18:31:14 +00:00
|
|
|
return patoolib.find_encoding_program(program, codec)
|