mirror of
https://github.com/gryf/fs-uae-wrapper.git
synced 2025-12-18 20:10:26 +01:00
Moved which function to another module
This commit is contained in:
@@ -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 = []
|
||||
|
||||
|
||||
22
fs_uae_wrapper/path.py
Normal file
22
fs_uae_wrapper/path.py
Normal file
@@ -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
|
||||
@@ -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):
|
||||
|
||||
|
||||
12
tests/test_path.py
Normal file
12
tests/test_path.py
Normal file
@@ -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')
|
||||
Reference in New Issue
Block a user