[bmap-tools] [PATCH 4/7] TransRead: uncompress .xz files on-the-fly

dedekind1 at gmail.com dedekind1 at gmail.com
Wed Aug 14 04:13:28 EDT 2013


From: Artem Bityutskiy <artem.bityutskiy at intel.com>

Change-Id: I401e20224bc2d0050b7c6354e346e10f13e99024
Signed-off-by: Artem Bityutskiy <artem.bityutskiy at intel.com>
---
 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
-- 
1.8.1.4




More information about the Bmap-tools mailing list