TransRead: uncompress .xz files on-the-fly
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 13 Aug 2013 10:12:33 +0000 (13:12 +0300)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 13 Aug 2013 10:50:09 +0000 (13:50 +0300)
Change-Id: I401e20224bc2d0050b7c6354e346e10f13e99024
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/TransRead.py

index ad38631..a3d34e8 100644 (file)
@@ -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