tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gold_utils.py (3398B)


      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 """//build/android implementations of //testing/skia_gold_common.
      5 
      6 Used for interacting with the Skia Gold image diffing service.
      7 """
      8 
      9 import os
     10 import shutil
     11 import time
     12 
     13 from devil.utils import cmd_helper
     14 from pylib.base.output_manager import Datatype
     15 from pylib.constants import host_paths
     16 from pylib.utils import repo_utils
     17 
     18 with host_paths.SysPath(host_paths.BUILD_PATH):
     19  from skia_gold_common import skia_gold_session
     20  from skia_gold_common import skia_gold_session_manager
     21  from skia_gold_common import skia_gold_properties
     22 
     23 
     24 class AndroidSkiaGoldSession(skia_gold_session.SkiaGoldSession):
     25  def _StoreDiffLinks(self, image_name, output_manager, output_dir):
     26    """See SkiaGoldSession._StoreDiffLinks for general documentation.
     27 
     28    |output_manager| must be a build.android.pylib.base.OutputManager instance.
     29    """
     30    given_path = closest_path = diff_path = None
     31    # The directory should contain "input-<hash>.png", "closest-<hash>.png",
     32    # and "diff.png".
     33    for f in os.listdir(output_dir):
     34      filepath = os.path.join(output_dir, f)
     35      if f.startswith('input-'):
     36        given_path = filepath
     37      elif f.startswith('closest-'):
     38        closest_path = filepath
     39      elif f == 'diff.png':
     40        diff_path = filepath
     41    results = self._comparison_results.setdefault(image_name,
     42                                                  self.ComparisonResults())
     43    # We include the timestamp in the PNG filename so that multiple tries from
     44    # the same run do not clobber each other.
     45    timestamp = _GetTimestamp()
     46    image_name = f'{image_name}_{timestamp}'
     47    if given_path:
     48      with output_manager.ArchivedTempfile('given_%s.png' % image_name,
     49                                           'gold_local_diffs',
     50                                           Datatype.PNG) as given_file:
     51        shutil.move(given_path, given_file.name)
     52      results.local_diff_given_image = given_file.Link()
     53    if closest_path:
     54      with output_manager.ArchivedTempfile('closest_%s.png' % image_name,
     55                                           'gold_local_diffs',
     56                                           Datatype.PNG) as closest_file:
     57        shutil.move(closest_path, closest_file.name)
     58      results.local_diff_closest_image = closest_file.Link()
     59    if diff_path:
     60      with output_manager.ArchivedTempfile('diff_%s.png' % image_name,
     61                                           'gold_local_diffs',
     62                                           Datatype.PNG) as diff_file:
     63        shutil.move(diff_path, diff_file.name)
     64      results.local_diff_diff_image = diff_file.Link()
     65 
     66  @staticmethod
     67  def _RunCmdForRcAndOutput(cmd):
     68    rc, stdout, _ = cmd_helper.GetCmdStatusOutputAndError(cmd,
     69                                                          merge_stderr=True)
     70    return rc, stdout
     71 
     72 
     73 def _GetTimestamp():
     74  return time.strftime('%Y%m%dT%H%M%S-UTC', time.gmtime())
     75 
     76 
     77 class AndroidSkiaGoldSessionManager(
     78    skia_gold_session_manager.SkiaGoldSessionManager):
     79  @staticmethod
     80  def GetSessionClass():
     81    return AndroidSkiaGoldSession
     82 
     83 
     84 class AndroidSkiaGoldProperties(skia_gold_properties.SkiaGoldProperties):
     85  @staticmethod
     86  def _GetGitOriginMainHeadSha1():
     87    return repo_utils.GetGitOriginMainHeadSHA1(host_paths.DIR_SOURCE_ROOT)