ext_transform_feedback: document missing mode in usage
[piglit.git] / framework / backends / compression.py
blob7170a1a3028b1f964d4a5250f3b0bfb80149ef47
1 # coding=utf-8
2 # Copyright (c) 2015-2016, 2019 Intel Corporation
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
22 """Compression support for backends.
24 This includes both compression and decompression support.
26 This provides a low level interface of dictionaries, COMPRESSORS and
27 DECOMPRESSORS, which use compression modes ('bz2', 'gz', 'xz', 'none') to
28 provide open-like functions with correct mode settings for writing or reading,
29 respectively.
31 They should always take unicode (str in python 3.x) objects. It is up to the
32 caller to ensure that they're passing unicode and not bytes.
34 A helper, get_mode(), is provided to return the user selected mode (it will try
35 the PIGLIT_COMPRESSION environment variable, then the piglit.conf
36 [core]:compression key, and finally the value of compression.DEFAULT). This is
37 the best way to get a compressor.
39 """
41 import bz2
42 import contextlib
43 import errno
44 import functools
45 import gzip
46 import io
47 import lzma
48 import os
49 import subprocess
51 from framework import core
52 from framework import exceptions
54 __all__ = [
55 'UnsupportedCompressor',
56 'COMPRESSORS',
57 'DECOMPRESSORS',
58 'get_mode',
62 class UnsupportedCompressor(exceptions.PiglitInternalError):
63 def __init__(self, method, *args, **kwargs):
64 super(UnsupportedCompressor, self).__init__(*args, **kwargs)
65 self.__method = method
67 def __str__(self):
68 return 'unsupported compression method {}'.format(self.__method)
72 DEFAULT = 'bz2'
74 COMPRESSION_SUFFIXES = ['.gz', '.bz2', '.xz']
76 COMPRESSORS = {
77 'bz2': functools.partial(bz2.open, mode='wt'),
78 'gz': functools.partial(gzip.open, mode='wt'),
79 'none': functools.partial(open, mode='w'),
80 'xz': functools.partial(lzma.open, mode='wt'),
83 DECOMPRESSORS = {
84 'bz2': functools.partial(bz2.open, mode='rt'),
85 'gz': functools.partial(gzip.open, mode='rt'),
86 'none': functools.partial(open, mode='r'),
87 'xz': functools.partial(lzma.open, mode='rt'),
91 def get_mode():
92 """Return the key value of the correct compressor to use.
94 Try the environment variable PIGLIT_COMPRESSION; then check the
95 PIGLIT_CONFIG section 'core', option 'compression'; finally fall back to
96 DEFAULT.
98 This will raise an UnsupportedCompressionError if there isn't a compressor
99 for that mode. It is the job of the caller to handle this exceptions
102 # This is provided as a function rather than a constant because as a
103 # function it can honor changes to the PIGLIT_CONFIG instance, or the
104 # PIGLIT_COMPRESSION environment variable.
106 method = core.get_option('PIGLIT_COMPRESSION',
107 ('core', 'compression'),
108 default=DEFAULT)
110 if method not in COMPRESSORS:
111 raise UnsupportedCompressor(method)
113 return method