cl: Fix missing OpenCL 3.0 definition
[piglit.git] / framework / replay / backends / __init__.py
blob9c9ed678df89a92dc221dcbddf7d9e0e3095cb3a
1 # coding=utf-8
3 # Copyright (c) 2014-2016, 2019 Intel Corporation
4 # Copyright © 2019-2020 Valve Corporation.
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 # and/or sell copies of the Software, and to permit persons to whom the
11 # Software is furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 # OTHER DEALINGS IN THE SOFTWARE.
24 # SPDX-License-Identifier: MIT
26 """Provides a module like interface for dump backends.
28 This package provides an abstract interface for working with dump backends,
29 which implement various functions as provided in the Registry class, and then
30 provide a Registry instance as REGISTRY, which maps individual objects to
31 objects that piglit expects to use. This module then provides a thin
32 abstraction layer so that piglit is never aware of what backend it's using, it
33 just asks for an object and receives one.
35 Most consumers will want to import framework.replay.backends and work directly
36 with the helper functions here. For some more detailed uses (test cases
37 especially) the modules themselves may be used.
39 When this module is loaded it will search through framework/replay/backends for
40 python modules (those ending in .py), and attempt to import them. Then it will
41 look for an attribute REGISTRY in those modules, and if it as a
42 framework.replay.register.Registry instance, it will add the name of that
43 module (sans .py) as a key, and the instance as a value to the DUMPBACKENDS
44 dictionary. Each of the helper functions in this module uses that dictionary to
45 find the function that a user actually wants.
47 """
49 import importlib
50 import os
51 from os import path
53 from .register import Registry
55 __all__ = [
56 'DUMPBACKENDS',
57 'DumpBackendError',
58 'DumpBackendNotImplementedError',
59 'dump',
63 class DumpBackendError(Exception):
64 pass
67 class DumpBackendNotImplementedError(NotImplementedError):
68 pass
71 def _register():
72 """Register backends.
74 Walk through the list of backends and register them to a name in a
75 dictionary, so that they can be referenced from helper functions.
77 """
78 registry = {}
80 for module in os.listdir(path.dirname(path.abspath(__file__))):
81 module, extension = path.splitext(module)
82 if extension == '.py':
83 mod = importlib.import_module(
84 'framework.replay.backends.{}'.format(module))
85 if hasattr(mod, 'REGISTRY') and isinstance(mod.REGISTRY, Registry):
86 registry[module] = mod.REGISTRY
88 return registry
91 DUMPBACKENDS = _register()
94 def dump(trace_path, output_dir=None, calls=None):
95 """Wrapper for dumping traces.
97 This function will attempt to determine how to dump the trace file (based
98 on file extension), and then pass the trace file path, output_dir and calls
99 into the appropriate instance, and then call dump in such instance.
102 name, extension = path.splitext(trace_path)
104 for dump_backend in DUMPBACKENDS.values():
105 if extension in dump_backend.extensions:
106 backend = dump_backend.backend
108 if backend is None:
109 raise DumpBackendNotImplementedError(
110 'DumpBackend for "{}" is not implemented'.format(extension))
112 instance = backend(trace_path, output_dir, calls)
113 return instance.dump()
115 raise DumpBackendError(
116 'No module supports file extensions "{}"'.format(extension))