File: gold_utils.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (93 lines) | stat: -rw-r--r-- 3,524 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""//build/android implementations of //testing/skia_gold_common.

Used for interacting with the Skia Gold image diffing service.
"""

import os
import shutil
import time

from devil.utils import cmd_helper
from pylib.base.output_manager import Datatype
from pathlib import Path
import sys

build_android_path = Path(__file__).parents[2]
sys.path.append(str(build_android_path))

from pylib.constants import host_paths
from pylib.utils import repo_utils

with host_paths.SysPath(host_paths.BUILD_PATH):
  from skia_gold_common import skia_gold_session
  from skia_gold_common import skia_gold_session_manager
  from skia_gold_common import skia_gold_properties


class AndroidSkiaGoldSession(skia_gold_session.SkiaGoldSession):
  def _StoreDiffLinks(self, image_name, output_manager, output_dir):
    """See SkiaGoldSession._StoreDiffLinks for general documentation.

    |output_manager| must be a build.android.pylib.base.OutputManager instance.
    """
    given_path = closest_path = diff_path = None
    # The directory should contain "input-<hash>.png", "closest-<hash>.png",
    # and "diff.png".
    for f in os.listdir(output_dir):
      filepath = os.path.join(output_dir, f)
      if f.startswith('input-'):
        given_path = filepath
      elif f.startswith('closest-'):
        closest_path = filepath
      elif f == 'diff.png':
        diff_path = filepath
    results = self._comparison_results.setdefault(image_name,
                                                  self.ComparisonResults())
    # We include the timestamp in the PNG filename so that multiple tries from
    # the same run do not clobber each other.
    timestamp = _GetTimestamp()
    image_name = f'{image_name}_{timestamp}'
    if given_path:
      with output_manager.ArchivedTempfile('given_%s.png' % image_name,
                                           'gold_local_diffs',
                                           Datatype.PNG) as given_file:
        shutil.move(given_path, given_file.name)
      results.local_diff_given_image = given_file.Link()
    if closest_path:
      with output_manager.ArchivedTempfile('closest_%s.png' % image_name,
                                           'gold_local_diffs',
                                           Datatype.PNG) as closest_file:
        shutil.move(closest_path, closest_file.name)
      results.local_diff_closest_image = closest_file.Link()
    if diff_path:
      with output_manager.ArchivedTempfile('diff_%s.png' % image_name,
                                           'gold_local_diffs',
                                           Datatype.PNG) as diff_file:
        shutil.move(diff_path, diff_file.name)
      results.local_diff_diff_image = diff_file.Link()

  @staticmethod
  def _RunCmdForRcAndOutput(cmd):
    rc, stdout, _ = cmd_helper.GetCmdStatusOutputAndError(cmd,
                                                          merge_stderr=True)
    return rc, stdout


def _GetTimestamp():
  return time.strftime('%Y%m%dT%H%M%S-UTC', time.gmtime())


class AndroidSkiaGoldSessionManager(
    skia_gold_session_manager.SkiaGoldSessionManager):
  @staticmethod
  def GetSessionClass():
    return AndroidSkiaGoldSession


class AndroidSkiaGoldProperties(skia_gold_properties.SkiaGoldProperties):
  @staticmethod
  def _GetGitOriginMainHeadSha1():
    return repo_utils.GetGitOriginMainHeadSHA1(host_paths.DIR_SOURCE_ROOT)