diff options
author | Andres Gomez <agomez@igalia.com> | 2020-07-30 00:23:47 +0300 |
---|---|---|
committer | Andres Gomez <agomez@igalia.com> | 2020-11-02 22:16:01 +0200 |
commit | cb575a209fb9e09769af70e9b8ca89a37db373fa (patch) | |
tree | d055cbdfda2eac9b3f821c66f7280a699ed1aa21 /framework | |
parent | 4747737741af5df8aed6194fd5a2dc603ed1dabb (diff) |
framework/replay: add global options
v2:
- Document the global options.
Signed-off-by: Andres Gomez <agomez@igalia.com>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
Part-of: <https://gitlab.freedesktop.org/mesa/piglit/-/merge_requests/353>
Diffstat (limited to 'framework')
-rw-r--r-- | framework/replay/__init__.py | 1 | ||||
-rw-r--r-- | framework/replay/compare_replay.py | 43 | ||||
-rw-r--r-- | framework/replay/download_utils.py | 12 | ||||
-rw-r--r-- | framework/replay/dump_trace_images.py | 6 | ||||
-rw-r--r-- | framework/replay/options.py | 91 |
5 files changed, 122 insertions, 31 deletions
diff --git a/framework/replay/__init__.py b/framework/replay/__init__.py index 78f8e7cb8..eea2d157a 100644 --- a/framework/replay/__init__.py +++ b/framework/replay/__init__.py @@ -33,5 +33,6 @@ from .compare_replay import * from .download_utils import * from .dump_trace_images import * from .image_checksum import * +from .options import * from .query_traces_yaml import * from .trace_utils import * diff --git a/framework/replay/compare_replay.py b/framework/replay/compare_replay.py index 3044b96e9..1d380884e 100644 --- a/framework/replay/compare_replay.py +++ b/framework/replay/compare_replay.py @@ -34,16 +34,15 @@ from framework.replay import query_traces_yaml as qty from framework.replay.download_utils import ensure_file from framework.replay.dump_trace_images import dump_from_trace from framework.replay.image_checksum import hexdigest_from_image +from framework.replay.options import OPTIONS __all__ = ['from_yaml', 'trace'] -TRACES_DB_PATH = './traces-db/' -RESULTS_PATH = './results/' -def _replay(trace_path, results_path, device_name): - success = dump_from_trace(trace_path, results_path, [], device_name) +def _replay(trace_path, results_path): + success = dump_from_trace(trace_path, results_path, []) if not success: print("[check_image] Trace {} couldn't be replayed. " @@ -57,28 +56,27 @@ def _replay(trace_path, results_path, device_name): return hexdigest_from_image(image_file), image_file -def _check_trace(download_url, device_name, trace_path, expected_checksum): - ensure_file(download_url, trace_path, TRACES_DB_PATH) +def _check_trace(trace_path, expected_checksum): + ensure_file(trace_path) result = {} result[trace_path] = {} result[trace_path]['expected'] = expected_checksum trace_dir = path.dirname(trace_path) - dir_in_results = path.join('trace', device_name, trace_dir) - results_path = path.join(RESULTS_PATH, dir_in_results) + dir_in_results = path.join('trace', OPTIONS.device_name, trace_dir) + results_path = path.join(OPTIONS.results_path, dir_in_results) os.makedirs(results_path, exist_ok=True) - checksum, image_file = _replay(path.join(TRACES_DB_PATH, trace_path), - results_path, - device_name)) + checksum, image_file = _replay(path.join(OPTIONS.db_path, trace_path), + results_path) result[trace_path]['actual'] = checksum or 'error' if checksum is None: return False, result if checksum == expected_checksum: - if os.environ.get('TRACIE_STORE_IMAGES', '0') != '1': + if not OPTIONS.keep_image: os.remove(image_file) print('[check_image] Images match for:\n {}\n'.format(trace_path)) ok = True @@ -91,7 +89,7 @@ def _check_trace(download_url, device_name, trace_path, expected_checksum): 'mesa/mesa/blob/master/.gitlab-ci/tracie/README.md') ok = False - if not ok or os.environ.get('TRACIE_STORE_IMAGES', '0') == '1': + if not ok or OPTIONS.keep_image: result[trace_path]['image'] = image_file result[trace_path]['actual'] = checksum @@ -100,24 +98,22 @@ def _check_trace(download_url, device_name, trace_path, expected_checksum): def _write_results(results): - os.makedirs(RESULTS_PATH, exist_ok=True) - results_file_path = path.join(RESULTS_PATH, 'results.yml') + os.makedirs(OPTIONS.results_path, exist_ok=True) + results_file_path = path.join(OPTIONS.results_path, 'results.yml') with open(results_file_path, 'w') as f: yaml.safe_dump(results, f, default_flow_style=False) -def from_yaml(yaml_file, device_name): +def from_yaml(yaml_file): y = qty.load_yaml(yaml_file) - download_url = qty.download_url(y) + OPTIONS.set_download_url(qty.download_url(y)) all_ok = True results = {} - t_list = qty.traces(y, device_name=device_name, checksum=True) + t_list = qty.traces(y, device_name=OPTIONS.device_name, checksum=True) for t in t_list: - ok, result = _check_trace(download_url, - device_name, - t['path'], t['checksum']) + ok, result = _check_trace(t['path'], t['checksum']) all_ok = all_ok and ok results.update(result) @@ -126,9 +122,8 @@ def from_yaml(yaml_file, device_name): return all_ok -def trace(download_url, device_name, trace_path, expected_checksum): - ok, result = _check_trace(download_url, device_name, trace_path, - expected_checksum) +def trace(trace_path, expected_checksum): + ok, result = _check_trace(trace_path, expected_checksum) _write_results(result) diff --git a/framework/replay/download_utils.py b/framework/replay/download_utils.py index be0763b30..aa21be550 100644 --- a/framework/replay/download_utils.py +++ b/framework/replay/download_utils.py @@ -29,27 +29,29 @@ import requests from os import path from time import time +from framework.replay.options import OPTIONS + __all__ = ['ensure_file'] -def ensure_file(download_url, file_path, destination): - destination_file_path = path.join(destination, file_path) - if download_url is None: +def ensure_file(file_path): + destination_file_path = path.join(OPTIONS.db_path, file_path) + if OPTIONS.download['url'] is None: assert path.exists(destination_file_path), ( '{} missing'.format(destination_file_path)) return os.makedirs(path.dirname(destination_file_path), exist_ok=True) - if path.exists(destination_file_path): + if not OPTIONS.download['force'] and path.exists(destination_file_path): return print('[check_image] Downloading file {}'.format( file_path), end=' ', flush=True) download_time = time() with open(destination_file_path, 'wb') as file: - with requests.get(download_url + file_path, + with requests.get(OPTIONS.download['url'].geturl() + file_path, allow_redirects=True, stream=True) as r: r.raise_for_status() for chunk in r.iter_content(chunk_size=8194): diff --git a/framework/replay/dump_trace_images.py b/framework/replay/dump_trace_images.py index e35b4c126..6f5fa75f2 100644 --- a/framework/replay/dump_trace_images.py +++ b/framework/replay/dump_trace_images.py @@ -28,6 +28,7 @@ import subprocess from os import path +from framework.replay.options import OPTIONS from framework.replay.trace_utils import trace_type_from_filename, TraceType @@ -133,10 +134,11 @@ def _dump_with_testtrace(trace_path, output_dir, calls): Image.frombytes('RGBA', (32, 32), bytes(color * 32 * 32)).save(outputfile) -def dump_from_trace(trace_path, output_dir, calls, device_name): +def dump_from_trace(trace_path, output_dir=None, calls=[]): _log('Info', 'Dumping trace {}'.format(trace_path), end='...\n') if output_dir is None: - output_dir = path.join('trace', device_name, path.dirname(trace_path)) + output_dir = path.join('trace', OPTIONS.device_name, + path.dirname(trace_path)) os.makedirs(output_dir, exist_ok=True) trace_type = trace_type_from_filename(path.basename(trace_path)) try: diff --git a/framework/replay/options.py b/framework/replay/options.py new file mode 100644 index 000000000..407ae13b6 --- /dev/null +++ b/framework/replay/options.py @@ -0,0 +1,91 @@ +# coding=utf-8 +# +# Copyright (c) 2015-2016, 2019 Intel Corporation +# Copyright © 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 + +"""Stores global replay options. + +This is as close to a true global function as python gets. + +""" + +import os +import sys + +from urllib.parse import urlparse + +__all__ = ['OPTIONS'] + +# pylint: disable=too-few-public-methods + + +def _safe_urlparse(url): + if url: + try: + parsed_url = urlparse(url) + return parsed_url + except Exception as e: + print(e, file=sys.stderr) + + return None + + +class _Options(object): # pylint: disable=too-many-instance-attributes + """Contains all options for a replay run. + + This is used as a sort of global state object. + + Options are as follows: + device_name -- The device against we are replaying and checking. + keep_image -- Whether to always keep the dumped images or not. + db_path -- The path to the objects db or where it will be created. + results_path -- The path in which to place the results. + download.url -- The URL from which to download the files. + download.force -- Forces downloading even if the destination file already + exists. + """ + + def __init__(self): + self.device_name = None + self.keep_image = False + self.db_path = None + self.results_path = None + self.download = {'url': None, + 'force': False + } + + def clear(self): + """Reinitialize all values to defaults.""" + self.__init__() + + def set_download_url(self, url): + """Safely set the parsed download url.""" + self.download['url'] = _safe_urlparse(url) + + def __iter__(self): + for key, values in self.__dict__.items(): + if not key.startswith('_'): + yield key, values + + +OPTIONS = _Options() |