1# Copyright 2020 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Implementation of skia_gold_session.py without output managers. 5 6Diff output is instead stored in a directory and pointed to with file:// URLs. 7""" 8 9import os 10import subprocess 11import time 12from typing import List, Tuple 13 14from skia_gold_common import skia_gold_session 15 16 17class OutputManagerlessSkiaGoldSession(skia_gold_session.SkiaGoldSession): 18 def RunComparison(self, *args, **kwargs) -> skia_gold_session.StepRetVal: 19 assert 'output_manager' not in kwargs, 'Cannot specify output_manager' 20 return super().RunComparison(*args, **kwargs) 21 22 def _CreateDiffOutputDir(self, name: str) -> str: 23 # Do this instead of just making a temporary directory so that it's easier 24 # for users to look through multiple results. We intentionally do not clean 25 # this directory up since the user might need to look at it later. 26 timestamp = int(time.time()) 27 name = '%s_%d' % (name, timestamp) 28 filepath = os.path.join(self._local_png_directory, name) 29 os.makedirs(filepath) 30 return filepath 31 32 def _StoreDiffLinks(self, image_name: str, _, output_dir: str) -> None: 33 results = self._comparison_results.setdefault(image_name, 34 self.ComparisonResults()) 35 # The directory should contain "input-<hash>.png", "closest-<hash>.png", 36 # and "diff.png". 37 for f in os.listdir(output_dir): 38 file_url = 'file://%s' % os.path.join(output_dir, f) 39 if f.startswith('input-'): 40 results.local_diff_given_image = file_url 41 elif f.startswith('closest-'): 42 results.local_diff_closest_image = file_url 43 elif f == 'diff.png': 44 results.local_diff_diff_image = file_url 45 46 def _RequiresOutputManager(self) -> bool: 47 return False 48 49 @staticmethod 50 def _RunCmdForRcAndOutput(cmd: List[str]) -> Tuple[int, str]: 51 try: 52 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True) 53 return 0, output 54 except subprocess.CalledProcessError as e: 55 return e.returncode, e.output 56