diff --git a/fs_uae_wrapper/file_archive.py b/fs_uae_wrapper/file_archive.py index b9681dc..60007e0 100644 --- a/fs_uae_wrapper/file_archive.py +++ b/fs_uae_wrapper/file_archive.py @@ -6,23 +6,7 @@ import subprocess import sys import re - -def which(archivers): - """ - Check if there selected archiver is available in the system and place it - to the archiver attribute - """ - - if not isinstance(archivers, list): - archivers = [archivers] - - for fname in archivers: - for path in os.environ["PATH"].split(os.pathsep): - path = os.path.join(path.strip('"'), fname) - if os.path.isfile(path) and os.access(path, os.X_OK): - return fname - - return None +from fs_uae_wrapper import path class Archive(object): @@ -32,7 +16,7 @@ class Archive(object): ARCH = 'false' def __init__(self): - self.archiver = which(self.ARCH) + self.archiver = path.which(self.ARCH) self._compess = self.archiver self._decompess = self.archiver @@ -91,7 +75,7 @@ class ZipArchive(Archive): def __init__(self): super(ZipArchive, self).__init__() if self.archiver == 'zip': - self._decompess = which('unzip') + self._decompess = path.which('unzip') ZipArchive.ADD = ['-r'] ZipArchive.EXTRACT = [] diff --git a/fs_uae_wrapper/path.py b/fs_uae_wrapper/path.py new file mode 100644 index 0000000..25e5a83 --- /dev/null +++ b/fs_uae_wrapper/path.py @@ -0,0 +1,22 @@ +""" +Misc utilities +""" +import os + + +def which(executables): + """ + Check if there selected archiver is available in the system and place it + to the archiver attribute + """ + + if not isinstance(executables, list): + executables = [executables] + + for fname in executables: + for path in os.environ["PATH"].split(os.pathsep): + path = os.path.join(path.strip('"'), fname) + if os.path.isfile(path) and os.access(path, os.X_OK): + return fname + + return None diff --git a/tests/test_file_archive.py b/tests/test_file_archive.py index 7d600d8..da85170 100644 --- a/tests/test_file_archive.py +++ b/tests/test_file_archive.py @@ -60,13 +60,7 @@ class TestArchive(TestCase): self.assertFalse(arch.extract('foo')) call.assert_called_once_with(['false', 'x', 'foo']) - def test_archive_which(self): - self.assertEqual(file_archive.which('sh'), 'sh') - self.assertIsNone(file_archive.which('blahblahexec')) - self.assertEqual(file_archive.which(['blahblahexec', 'pip', 'sh']), - 'pip') - - @mock.patch('fs_uae_wrapper.file_archive.which') + @mock.patch('fs_uae_wrapper.path.which') @mock.patch('subprocess.call') def test_tar(self, call, which): with open('foo', 'w') as fobj: @@ -122,7 +116,7 @@ class TestArchive(TestCase): self.assertFalse(arch.extract('foo')) call.assert_called_once_with(['tar', 'xf', 'foo']) - @mock.patch('fs_uae_wrapper.file_archive.which') + @mock.patch('fs_uae_wrapper.path.which') @mock.patch('subprocess.call') def test_lha(self, call, which): with open('foo', 'w') as fobj: @@ -142,7 +136,7 @@ class TestArchive(TestCase): self.assertFalse(arch.extract('foo')) call.assert_called_once_with(['lha', 'x', 'foo']) - @mock.patch('fs_uae_wrapper.file_archive.which') + @mock.patch('fs_uae_wrapper.path.which') @mock.patch('subprocess.call') def test_lzx(self, call, which): with open('foo', 'w') as fobj: @@ -162,7 +156,7 @@ class TestArchive(TestCase): self.assertFalse(arch.extract('foo')) call.assert_called_once_with(['unlzx', '-x', 'foo']) - @mock.patch('fs_uae_wrapper.file_archive.which') + @mock.patch('fs_uae_wrapper.path.which') @mock.patch('subprocess.call') def test_7zip(self, call, which): with open('foo', 'w') as fobj: @@ -182,7 +176,7 @@ class TestArchive(TestCase): self.assertFalse(arch.extract('foo')) call.assert_called_once_with(['7z', 'x', 'foo']) - @mock.patch('fs_uae_wrapper.file_archive.which') + @mock.patch('fs_uae_wrapper.path.which') @mock.patch('subprocess.call') def test_zip(self, call, which): with open('foo', 'w') as fobj: @@ -202,7 +196,7 @@ class TestArchive(TestCase): self.assertFalse(arch.extract('foo')) call.assert_called_once_with(['7z', 'x', 'foo']) - @mock.patch('fs_uae_wrapper.file_archive.which') + @mock.patch('fs_uae_wrapper.path.which') @mock.patch('subprocess.call') def test_rar(self, call, which): diff --git a/tests/test_path.py b/tests/test_path.py new file mode 100644 index 0000000..459e8ac --- /dev/null +++ b/tests/test_path.py @@ -0,0 +1,12 @@ +from unittest import TestCase + +from fs_uae_wrapper import path + + +class TestPath(TestCase): + + def test_which(self): + self.assertEqual(path.which('sh'), 'sh') + self.assertIsNone(path.which('blahblahexec')) + self.assertEqual(path.which(['blahblahexec', 'pip', 'sh']), + 'pip')