3 # Copyright (c) 2020 Collabora Ltd
4 # Copyright © 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
29 import simplejson
as json
33 from enum
import Enum
, unique
37 from framework
import core
38 from framework
import exceptions
39 from framework
import status
40 from framework
.replay
import backends
41 from framework
.replay
import query_traces_yaml
as qty
42 from framework
.replay
.download_utils
import ensure_file
43 from framework
.replay
.image_checksum
import hexdigest_from_image
44 from framework
.replay
.options
import OPTIONS
47 __all__
= ['from_yaml',
51 def _replay(trace_path
, results_path
):
53 success
= backends
.dump(trace_path
, results_path
, [])
54 except (backends
.DumpBackendNotImplementedError
,
55 backends
.DumpBackendError
) as e
:
60 print("[check_image] Trace {} couldn't be replayed. "
61 "See above logs for more information.".format(trace_path
))
64 file_name
= path
.basename(trace_path
)
65 files
= glob(path
.join(results_path
, file_name
+ '-*' + '.png'))
67 print('[check_image] No dumped files found '
68 'in the results path "{}". '
69 'See above logs for more information.'.format(results_path
))
72 return hexdigest_from_image(image_file
), image_file
75 def _check_trace(trace_path
, expected_checksum
):
76 ensure_file(trace_path
)
80 trace_dir
= path
.dirname(trace_path
)
81 dir_in_results
= path
.join('trace', OPTIONS
.device_name
or '', trace_dir
)
82 results_path
= path
.join(OPTIONS
.results_path
, dir_in_results
)
83 core
.check_dir(results_path
)
85 checksum
, image_file
= _replay(path
.join(OPTIONS
.db_path
, trace_path
),
88 print('[check_image]\n'
90 ' expected: {}'.format(checksum
or 'error', expected_checksum
))
92 json_result
['images'] = [
93 {'image_desc': trace_path
,
94 'image_ref': expected_checksum
+ '.png',
95 'image_render': expected_checksum
+ '.png'}]
98 json_result
['images'][0]['image_render'] = None
99 return status
.CRASH
, json_result
101 if checksum
== expected_checksum
:
102 if not OPTIONS
.keep_image
:
103 os
.remove(image_file
)
104 print('[check_image] Images match for:\n {}\n'.format(trace_path
))
107 print('[check_image] Images differ for:\n {}'.format(trace_path
))
108 print('[check_image] For more information see '
109 'https://gitlab.freedesktop.org/'
110 'mesa/piglit/blob/master/replayer/README.md\n')
113 if result
is not status
.PASS
or OPTIONS
.keep_image
:
114 root
, ext
= path
.splitext(image_file
)
115 image_file_dest
= '{}-{}{}'.format(root
, checksum
, ext
)
116 shutil
.move(image_file
, image_file_dest
)
117 json_result
['images'][0]['image_render'] = image_file_dest
119 return result
, json_result
122 def _print_result(result
, trace_path
, json_result
):
124 json_result
['result'] = str(result
)
126 output
+= json
.dumps(json_result
)
130 def from_yaml(yaml_file
):
131 y
= qty
.load_yaml(yaml_file
)
133 OPTIONS
.set_download_url(qty
.download_url(y
))
135 global_result
= status
.PASS
136 # TODO: print in subtest format
138 t_list
= qty
.traces(y
, device_name
=OPTIONS
.device_name
, checksum
=True)
140 result
, json_result
= _check_trace(t
['path'], t
['checksum'])
141 if result
is not status
.PASS
and global_result
is not status
.CRASH
:
142 global_result
= result
143 # json_results.update(json_result)
144 # _print_result(result, t['path'], json_result)
149 def trace(trace_path
, expected_checksum
):
150 result
, json_result
= _check_trace(trace_path
, expected_checksum
)
151 _print_result(result
, trace_path
, json_result
)