summaryrefslogtreecommitdiff
path: root/framework/backends/compression.py
blob: ab85a17ff28840b5e224f0469b9cf95fe4e3db9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Copyright (c) 2015 Intel Corporation

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Compression support for backends.

This includes both compression and decompression support.

This provides a low level interface of dictionaries, COMPRESSORS and
DECOMPRESSORS, which use compression modes ('bz2', 'gz', 'xz', 'none') to
provide open-like functions with correct mode settings for writing or reading,
respectively.

They should always take unicode objects. It is up to the caller to ensure that
they're passing unicode and not bytes.

A helper, get_mode(), is provided to return the user selected mode (it will try
the PIGLIT_COMPRESSION environment variable, then the piglit.conf
[core]:compression key, and finally the value of compression.DEFAULT). This is
the best way to get a compressor.

"""

from __future__ import print_function, absolute_import, division
import bz2
import errno
import functools
import gzip
import os
import subprocess
import contextlib

from six.moves import cStringIO as StringIO

from framework import exceptions
from framework.core import PIGLIT_CONFIG

__all__ = [
    'UnsupportedCompressor',
    'COMPRESSORS',
    'DECOMPRESSORS',
    'get_mode',
]


class UnsupportedCompressor(exceptions.PiglitInternalError):
    def __init__(self, method, *args, **kwargs):
        super(UnsupportedCompressor, self).__init__(*args, **kwargs)
        self.__method = method

    def __str__(self):
        return 'unsupported compression method {}'.format(self.__method)


# TODO: in python3 the bz2 module has an open function
COMPRESSION_SUFFIXES = ['.gz', '.bz2']

DEFAULT = 'bz2'

COMPRESSORS = {
    'bz2': functools.partial(bz2.BZ2File, mode='w'),
    'gz': functools.partial(gzip.open, mode='w'),
    'none': functools.partial(open, mode='w'),
}

DECOMPRESSORS = {
    'bz2': functools.partial(bz2.BZ2File, mode='r'),
    'gz': functools.partial(gzip.open, mode='r'),
    'none': functools.partial(open, mode='r'),
}

# TODO: in python3 there is builtin xz support, and doesn't need this madness
# First try to use backports.lzma, that's the easiest solution. If that fails
# then go to trying the shell. If that fails then piglit won't have xz support,
# and will raise an error if xz is used
try:
    import backports.lzma

    COMPRESSORS['xz'] = functools.partial(backports.lzma.open, mode='w')
    DECOMPRESSORS['xz'] = functools.partial(backports.lzma.open, mode='r')
    COMPRESSION_SUFFIXES += ['.xz']
except ImportError:
    try:
        with open(os.devnull, 'w') as d:
            subprocess.check_call(['xz', '--help'], stdout=d, stderr=d)
    except OSError:
        pass
    else:

        @contextlib.contextmanager
        def _compress_xz(filename):
            """Emulates an open function in write mode for xz.

            Python 2.x doesn't support xz, but it's dang useful. This
            function calls out to the shell and tries to use xz from the
            environment to get xz compression.

            This obviously won't work without a working xz binary.

            This function tries to emulate the default values of the lzma
            module in python3 as much as possible

            """
            if filename.endswith('.xz'):
                filename = filename[:-3]

            with open(filename, 'w') as f:
                yield f

            try:
                with open(os.devnull, 'w') as null:
                    subprocess.check_call(
                        ['xz', '--compress', '-9', '--force', filename],
                        stderr=null)
            except OSError as e:
                if e.errno == errno.ENOENT:
                    raise exceptions.PiglitFatalError(
                        'No xz binary available')
                raise

        @contextlib.contextmanager
        def _decompress_xz(filename):
            """Eumlates an option function in read mode for xz.

            See the comment in _compress_xz for more information.

            This function tries to emulate the lzma module as much as
            possible

            """
            if not filename.endswith('.xz'):
                filename = '{}.xz'.format(filename)

            try:
                with open(os.devnull, 'w') as null:
                    string = subprocess.check_output(
                        ['xz', '--decompress', '--stdout', filename],
                        stderr=null)
            except OSError as e:
                if e.errno == errno.ENOENT:
                    raise exceptions.PiglitFatalError(
                        'No xz binary available')
                raise

            # We need a file-like object, so the contents must be placed in
            # a StringIO object.
            io = StringIO()
            io.write(string)
            io.seek(0)

            yield io

            io.close()

        COMPRESSORS['xz'] = _compress_xz
        DECOMPRESSORS['xz'] = _decompress_xz
        COMPRESSION_SUFFIXES += ['.xz']


def get_mode():
    """Return the key value of the correct compressor to use.

    Try the environment variable PIGLIT_COMPRESSION; then check the
    PIGLIT_CONFIG section 'core', option 'compression'; finally fall back to
    DEFAULT.

    This will raise an UnsupportedCompressionError if there isn't a compressor
    for that mode. It is the job of the caller to handle this exceptions

    """
    # This is provided as a function rather than a constant because as a
    # function it can honor changes to the PIGLIT_CONFIG instance, or the
    # PIGLIT_COMPRESSION environment variable.

    method = (os.environ.get('PIGLIT_COMPRESSION') or
              PIGLIT_CONFIG.safe_get('core', 'compression') or
              DEFAULT)

    if method not in COMPRESSORS:
        raise UnsupportedCompressor(method)

    return method