Пример #1
0
    def lzmaopen(cls,
                 name=None,
                 mode="r",
                 fileobj=None,
                 compressformat="xz",
                 compresslevel=9,
                 **kwargs):
        """Open lzma/xz compressed tar archive name for reading or writing.
           Appending is not allowed.
        """

        if len(mode) > 1 or mode not in "rw":
            raise ValueError("mode must be 'r' or 'w'.")

        try:
            import lzma
        except ImportError:
            raise tarfile.CompressionError("lzma module is not available")

        if fileobj is not None:
            fileobj = _LZMAProxy(fileobj, mode)
        else:
            options = {"format":    compressformat,
                       "level":     compresslevel}
            fileobj = lzma.LZMAFile(name, mode, options=options)

        try:
            t = cls.taropen(name, mode, fileobj, **kwargs)
        except IOError:
            raise ReadError("not a lzma file")
        t._extfileobj = False
        return t
Пример #2
0
    def lzmaopen(cls,
                 name=None,
                 mode="r",
                 fileobj=None,
                 compresslevel=None,
                 **kwargs):
        """Open lzma/xz compressed tar archive name for reading or writing.
           Appending is not allowed.
        """

        try:
            import lzma
        except ImportError:
            try:
                from backports import lzma
            except ImportError:
                raise tarfile.CompressionError("Lzma module is not available")

        if not compresslevel:
            compresslevel = ctx.config.values.build.compressionlevel

        if len(mode) > 1 or mode not in "rw":
            raise ValueError("mode must be 'r' or 'w'.")

        if 'w' in mode:
            if fileobj is not None:
                fileobj = _LZMAProxy(fileobj, mode)
            else:
                fileobj = lzma.LZMAFile(name, mode, preset=compresslevel)

        else:
            if fileobj is not None:
                fileobj = _LZMAProxy(fileobj, mode)
            else:
                fileobj = lzma.LZMAFile(name, mode)

        try:
            t = cls.taropen(name, mode, fileobj, **kwargs)
        except IOError:
            raise tarfile.ReadError(
                _(" \"{}\" is not a lzma file.").format(name))
        t._extfileobj = False
        return t
Пример #3
0
    def xzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
        """Open lzma compressed tar archive name for reading or writing.
           Appending is not allowed.
        """
        if mode not in ("r", "w"):
            raise ValueError("mode must be 'r' or 'w'")

        try:
            from backports import lzma
            lzma.LZMAFile
        except (ImportError, AttributeError):
            raise tarfile.CompressionError("lzma module is not available")

        try:
            fileobj = lzma.LZMAFile(fileobj or name, mode)
        except (OSError, IOError):
            if mode == 'r':
                raise tarfile.ReadError("not an lzma file")
            raise

        try:
            fileobj.peek()
        except (lzma.LZMAError, EOFError):
            raise tarfile.ReadError("not an lzma file")

        try:
            t = cls.taropen(name, mode, fileobj, **kwargs)
        except IOError:
            fileobj.close()
            if mode == 'r':
                raise tarfile.ReadError("not an lzma file")
            raise
        except:
            fileobj.close()
            raise
        t._extfileobj = False
        return t