From d93fb5b7c23879d5bcb6c4412c0d89ff7d68b9c5 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Wed, 15 Jan 2014 16:39:00 -0500 Subject: [PATCH] * Added support for .xz file extension. * Using backported lzma module (on python <= 3.2) to eliminate subprocess, if possible. --- file/file_utils.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/file/file_utils.py b/file/file_utils.py index b2300a6..ab021d1 100644 --- a/file/file_utils.py +++ b/file/file_utils.py @@ -31,6 +31,17 @@ try: except: has_subprocess = False +try: + import lzma + has_lzma = True +except: + try: + from backports import lzma + has_lzma = True + except: + has_lzma = False + + from wpylib.sugar import is_iterable class super_file(object): @@ -66,11 +77,29 @@ def open_input_file(fname, superize=0): fobj = gzip.GzipFile(fname, "r") elif fname.endswith(".lzma"): # until lzma has a "standard" python module, we use "lzma" executable: - if has_subprocess: - px = subprocess.Popen(("lzma", "-dc", fname), stdout=subprocess.PIPE) + if has_lzma: + fobj = lzma.LZMAFile(fname, "r") + else: + from wpylib.shell_tools import is_executable_file + lzma_exe = path_search(os.environ["PATH"].split(os.pathsep), + ("lzma", "xz"), + filetest=is_executable_file) + if lzma_exe == None: + raise IOError, "Cannot find lzma or xz executable file." + if has_subprocess: + px = subprocess.Popen((lzma_exe, "-dc", fname), stdout=subprocess.PIPE) + fobj = px.stdout + else: + fobj = os.popen('" -dc "' + fname + '"', "r") + elif fname.endswith(".xz"): + # until lzma has a "standard" python module, we use "lzma" executable: + if has_lzma: + fobj = lzma.LZMAFile(fname, "r") + elif has_subprocess: + px = subprocess.Popen(("xz", "-dc", fname), stdout=subprocess.PIPE) fobj = px.stdout else: - px = os.popen('lzma -dc "' + fname + '"', "r") + fobj = os.popen('xz -dc "' + fname + '"', "r") else: fobj = open(fname, "r")