summaryrefslogtreecommitdiff
path: root/framework/replay/backends/__init__.py
blob: 9c9ed678df89a92dc221dcbddf7d9e0e3095cb3a (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
# coding=utf-8
#
# Copyright (c) 2014-2016, 2019 Intel Corporation
# Copyright © 2019-2020 Valve 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.
#
# SPDX-License-Identifier: MIT

"""Provides a module like interface for dump backends.

This package provides an abstract interface for working with dump backends,
which implement various functions as provided in the Registry class, and then
provide a Registry instance as REGISTRY, which maps individual objects to
objects that piglit expects to use. This module then provides a thin
abstraction layer so that piglit is never aware of what backend it's using, it
just asks for an object and receives one.

Most consumers will want to import framework.replay.backends and work directly
with the helper functions here. For some more detailed uses (test cases
especially) the modules themselves may be used.

When this module is loaded it will search through framework/replay/backends for
python modules (those ending in .py), and attempt to import them. Then it will
look for an attribute REGISTRY in those modules, and if it as a
framework.replay.register.Registry instance, it will add the name of that
module (sans .py) as a key, and the instance as a value to the DUMPBACKENDS
dictionary. Each of the helper functions in this module uses that dictionary to
find the function that a user actually wants.

"""

import importlib
import os
from os import path

from .register import Registry

__all__ = [
    'DUMPBACKENDS',
    'DumpBackendError',
    'DumpBackendNotImplementedError',
    'dump',
]


class DumpBackendError(Exception):
    pass


class DumpBackendNotImplementedError(NotImplementedError):
    pass


def _register():
    """Register backends.

    Walk through the list of backends and register them to a name in a
    dictionary, so that they can be referenced from helper functions.

    """
    registry = {}

    for module in os.listdir(path.dirname(path.abspath(__file__))):
        module, extension = path.splitext(module)
        if extension == '.py':
            mod = importlib.import_module(
                'framework.replay.backends.{}'.format(module))
            if hasattr(mod, 'REGISTRY') and isinstance(mod.REGISTRY, Registry):
                registry[module] = mod.REGISTRY

    return registry


DUMPBACKENDS = _register()


def dump(trace_path, output_dir=None, calls=None):
    """Wrapper for dumping traces.

    This function will attempt to determine how to dump the trace file (based
    on file extension), and then pass the trace file path, output_dir and calls
    into the appropriate instance, and then call dump in such instance.

    """
    name, extension = path.splitext(trace_path)

    for dump_backend in DUMPBACKENDS.values():
        if extension in dump_backend.extensions:
            backend = dump_backend.backend

            if backend is None:
                raise DumpBackendNotImplementedError(
                    'DumpBackend for "{}" is not implemented'.format(extension))

            instance = backend(trace_path, output_dir, calls)
            return instance.dump()

    raise DumpBackendError(
        'No module supports file extensions "{}"'.format(extension))