mirror of
https://github.com/gryf/fs-uae-wrapper.git
synced 2025-12-19 12:28:12 +01:00
Non existed archive will not be fatal
Moved logic which check for archive file explicitly to extract method. That way get_archiver will return archiver for not (yet) existed archives.
This commit is contained in:
@@ -21,16 +21,26 @@ class Archive(object):
|
||||
Create archive using self.archiver and parameters in self.ADD
|
||||
attribute
|
||||
"""
|
||||
return subprocess.call([self.archiver] + self.ADD +
|
||||
[arch_name, '.']) == 0
|
||||
result = subprocess.call([self.archiver] + self.ADD + [arch_name, '.'])
|
||||
if result != 0:
|
||||
sys.stderr.write("Unable to create archive `%s'\n" % arch_name)
|
||||
return False
|
||||
return True
|
||||
|
||||
def extract(self, arch_name):
|
||||
"""
|
||||
Create archive using self.archiver and parameters in self.ADD
|
||||
attribute
|
||||
"""
|
||||
return subprocess.call([self.archiver] + self.EXTRACT +
|
||||
[arch_name]) == 0
|
||||
if not os.path.exists(arch_name):
|
||||
sys.stderr.write("Archive `%s' doesn't exists.\n" % arch_name)
|
||||
return False
|
||||
|
||||
result = subprocess.call([self.archiver] + self.EXTRACT + [arch_name])
|
||||
if result != 0:
|
||||
sys.stderr.write("Unable to extract archive `%s'\n" % arch_name)
|
||||
return False
|
||||
return True
|
||||
|
||||
def which(self):
|
||||
"""
|
||||
@@ -103,18 +113,18 @@ class RarArchive(Archive):
|
||||
'supported by unrar.\n')
|
||||
return False
|
||||
|
||||
return subprocess.call([self.archiver] + self.ADD + [arch_name] +
|
||||
sorted(os.listdir('.'))) == 0
|
||||
result = subprocess.call([self.archiver] + self.ADD + [arch_name] +
|
||||
sorted(os.listdir('.')))
|
||||
if result != 0:
|
||||
sys.stderr.write("Unable to create archive `%s'\n" % arch_name)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def get_archiver(filename):
|
||||
"""Return right class for provided archive filename"""
|
||||
def get_archiver(arch_name):
|
||||
"""Return right class for provided archive file name"""
|
||||
|
||||
if not os.path.exists(filename):
|
||||
sys.stderr.write("Archive `%s' doesn't exists.\n" % filename)
|
||||
return None
|
||||
|
||||
_, ext = os.path.splitext(filename)
|
||||
_, ext = os.path.splitext(arch_name)
|
||||
|
||||
archivers = {'.tar': TarArchive,
|
||||
'.tgz': TarGzipArchive,
|
||||
@@ -129,10 +139,13 @@ def get_archiver(filename):
|
||||
|
||||
archiver = archivers.get(ext)
|
||||
if not archiver:
|
||||
sys.stderr.write("Unable find archive type for `%s'\n" % arch_name)
|
||||
return None
|
||||
|
||||
archobj = archiver()
|
||||
if archobj.archiver is None:
|
||||
sys.stderr.write("Unable find executable for operating on files"
|
||||
" `*%s'\n" % ext)
|
||||
return None
|
||||
|
||||
return archobj
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
from tempfile import mkstemp, mkdtemp
|
||||
from tempfile import mkdtemp
|
||||
from unittest import TestCase
|
||||
|
||||
try:
|
||||
@@ -10,7 +9,6 @@ except ImportError:
|
||||
import mock
|
||||
|
||||
from fs_uae_wrapper import file_archive
|
||||
from fs_uae_wrapper import utils
|
||||
|
||||
|
||||
class TestArchive(TestCase):
|
||||
@@ -30,10 +28,6 @@ class TestArchive(TestCase):
|
||||
def test_get_archiver(self):
|
||||
arch = file_archive.get_archiver('foobarbaz.cab')
|
||||
self.assertIsNone(arch)
|
||||
with open('foobarbaz.cab', 'w') as fobj:
|
||||
fobj.write('\n')
|
||||
arch = file_archive.get_archiver('foobarbaz.cab')
|
||||
self.assertIsNone(arch)
|
||||
|
||||
with open('foobarbaz.tar', 'w') as fobj:
|
||||
fobj.write('\n')
|
||||
@@ -56,6 +50,8 @@ class TestArchive(TestCase):
|
||||
|
||||
call.reset_mock()
|
||||
call.return_value = 1
|
||||
with open('foo', 'w') as fobj:
|
||||
fobj.write('\n')
|
||||
self.assertFalse(arch.extract('foo'))
|
||||
call.assert_called_once_with(['false', 'x', 'foo'])
|
||||
|
||||
@@ -78,6 +74,9 @@ class TestArchive(TestCase):
|
||||
@mock.patch('fs_uae_wrapper.file_archive.Archive.which')
|
||||
@mock.patch('subprocess.call')
|
||||
def test_tar(self, call, which):
|
||||
with open('foo', 'w') as fobj:
|
||||
fobj.write('\n')
|
||||
|
||||
arch = file_archive.TarArchive()
|
||||
arch.archiver = 'tar'
|
||||
call.return_value = 0
|
||||
@@ -129,6 +128,9 @@ class TestArchive(TestCase):
|
||||
@mock.patch('fs_uae_wrapper.file_archive.Archive.which')
|
||||
@mock.patch('subprocess.call')
|
||||
def test_lha(self, call, which):
|
||||
with open('foo', 'w') as fobj:
|
||||
fobj.write('\n')
|
||||
|
||||
arch = file_archive.LhaArchive()
|
||||
arch.archiver = 'lha'
|
||||
call.return_value = 0
|
||||
@@ -144,6 +146,9 @@ class TestArchive(TestCase):
|
||||
@mock.patch('fs_uae_wrapper.file_archive.Archive.which')
|
||||
@mock.patch('subprocess.call')
|
||||
def test_lzx(self, call, which):
|
||||
with open('foo', 'w') as fobj:
|
||||
fobj.write('\n')
|
||||
|
||||
arch = file_archive.LzxArchive()
|
||||
arch.archiver = 'unlzx'
|
||||
call.return_value = 0
|
||||
@@ -159,6 +164,9 @@ class TestArchive(TestCase):
|
||||
@mock.patch('fs_uae_wrapper.file_archive.Archive.which')
|
||||
@mock.patch('subprocess.call')
|
||||
def test_7zip(self, call, which):
|
||||
with open('foo', 'w') as fobj:
|
||||
fobj.write('\n')
|
||||
|
||||
arch = file_archive.SevenZArchive()
|
||||
arch.archiver = '7z'
|
||||
call.return_value = 0
|
||||
@@ -174,6 +182,9 @@ class TestArchive(TestCase):
|
||||
@mock.patch('fs_uae_wrapper.file_archive.Archive.which')
|
||||
@mock.patch('subprocess.call')
|
||||
def test_zip(self, call, which):
|
||||
with open('foo', 'w') as fobj:
|
||||
fobj.write('\n')
|
||||
|
||||
arch = file_archive.ZipArchive()
|
||||
arch.archiver = '7z'
|
||||
call.return_value = 0
|
||||
@@ -189,6 +200,7 @@ class TestArchive(TestCase):
|
||||
@mock.patch('fs_uae_wrapper.file_archive.Archive.which')
|
||||
@mock.patch('subprocess.call')
|
||||
def test_rar(self, call, which):
|
||||
|
||||
arch = file_archive.RarArchive()
|
||||
arch.archiver = 'rar'
|
||||
call.return_value = 0
|
||||
@@ -206,6 +218,8 @@ class TestArchive(TestCase):
|
||||
self.assertTrue(arch.create('foo.rar'))
|
||||
call.assert_called_once_with(['rar', 'a', 'foo.rar', 'bar', 'baz',
|
||||
'directory', 'foo'])
|
||||
with open('foo', 'w') as fobj:
|
||||
fobj.write('\n')
|
||||
|
||||
call.reset_mock()
|
||||
call.return_value = 1
|
||||
|
||||
Reference in New Issue
Block a user