From 6dbb07683c6c585fcad4108fe92c04965c0adf36 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Tue, 13 Aug 2013 13:12:33 +0300 Subject: [PATCH] TransRead: uncompress .xz files on-the-fly Change-Id: I401e20224bc2d0050b7c6354e346e10f13e99024 Signed-off-by: Artem Bityutskiy --- bmaptools/TransRead.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/bmaptools/TransRead.py b/bmaptools/TransRead.py index ad38631..a3d34e8 100644 --- a/bmaptools/TransRead.py +++ b/bmaptools/TransRead.py @@ -13,7 +13,7 @@ This module allows opening and reading local and remote files and decompress them on-the-fly if needed. Remote files are read using urllib2 (except of "ssh://" URLs, which are handled differently). Supported compression types are: -'bz2', 'gz', 'tar.gz', 'tgz', 'tar.bz2'. +'bz2', 'gz', 'xz', 'tar.gz', 'tgz', 'tar.bz2'. """ import os @@ -29,7 +29,7 @@ import urlparse # pylint: disable=R0902 # A list of supported compression types -SUPPORTED_COMPRESSION_TYPES = ('bz2', 'gz', 'tar.gz', 'tgz', 'tar.bz2') +SUPPORTED_COMPRESSION_TYPES = ('bz2', 'gz', 'xz', 'tar.gz', 'tgz', 'tar.bz2') def _fake_seek_forward(file_obj, cur_pos, offset, whence=os.SEEK_SET): """ @@ -307,6 +307,19 @@ class TransRead: self._transfile_obj = _CompressedFile(self._file_obj, bz2.BZ2Decompressor().decompress, 128) + elif self.name.endswith('.xz'): + try: + import lzma + except ImportError: + try: + from backports import lzma + except ImportError: + raise Error("cannot import the \"lzma\" python module, " + "it is required for decompressing .xz files") + + self._transfile_obj = _CompressedFile(self._file_obj, + lzma.LZMADecompressor().decompress, + 128) else: self.is_compressed = False self._transfile_obj = self._file_obj -- 2.7.4