1
0
mirror of https://github.com/gryf/fs-uae-wrapper.git synced 2025-12-19 04:20:23 +01:00

Extracted some methods to utils module

This commit is contained in:
2016-12-25 16:21:57 +01:00
parent 8b51979dc5
commit 9bbe39b059
3 changed files with 70 additions and 5 deletions

61
fs_uae_wrapper/utils.py Normal file
View File

@@ -0,0 +1,61 @@
"""
Misc utilities
"""
import os
import subprocess
import sys
from fs_uae_wrapper.wrapper import WRAPPER_KEY
ARCHIVERS = {'.tar': ['tar', 'xf'],
'.tgz': ['tar', 'xf'],
'.tar.gz': ['tar', 'xf'],
'.tar.bz2': ['tar', 'xf'],
'.tar.xz': ['tar', 'xf'],
'.rar': ['unrar', 'x'],
'.7z': ['7z', 'x'],
'.zip': ['7z', 'x'],
'.lha': ['lha', 'x'],
'.lzx': ['unlzx']}
def extract_archive(arch_name):
"""
Extract provided archive to current directory
"""
if not os.path.exists(arch_name):
sys.stderr.write("Archive `%s' doesn't exists.\n" % arch_name)
return False
_, ext = os.path.splitext(arch_name)
cmd = ARCHIVERS.get(ext)
if cmd is None:
sys.stderr.write("Unable find archive type for `%s'.\n" % arch_name)
return False
try:
subprocess.check_call(cmd + [arch_name])
except subprocess.CalledProcessError:
sys.stderr.write("Error during extracting archive `%s'.\n" % arch_name)
return False
return True
def merge_options(configuration, wrapper_options):
"""
Merge dictionaries with wrapper options into one. Commandline options
have precedence.
"""
options = {}
for key, val in configuration:
if WRAPPER_KEY in key:
options[key] = val
options.update(wrapper_options)
return options