blob: 6a91cc4f513599563e14669e67dafa8931c845da [file] [log] [blame]
epoger@google.com9d331542013-05-28 15:25:38 +00001#!/usr/bin/env python
2# Copyright (c) 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Schema of the JSON summary file written out by the GM tool.
7
8This must be kept in sync with the kJsonKey_ constants in gm_expectations.cpp !
9"""
10
11__author__ = 'Elliot Poger'
12
13
14# system-level imports
commit-bot@chromium.org83aaf882013-12-18 15:28:24 +000015import io
epoger@google.com9d331542013-05-28 15:25:38 +000016import json
scroggo@google.comd23e6832013-10-10 21:09:24 +000017import os
commit-bot@chromium.org16f41802014-02-26 19:05:20 +000018import posixpath
19import re
epoger@google.com9d331542013-05-28 15:25:38 +000020
21
epoger@google.coma783f2b2013-07-08 17:51:58 +000022# Key strings used in GM results JSON files (both expected-results.json and
23# actual-results.json).
24#
commit-bot@chromium.org16f41802014-02-26 19:05:20 +000025# NOTE: These constants must be kept in sync with the kJsonKey_ constants in
commit-bot@chromium.org7418bd82014-05-06 15:31:31 +000026# gm_expectations.cpp and tools/PictureRenderer.cpp !
27# Eric suggests: create gm/gm_expectations_constants.h containing ONLY variable
28# declarations so as to be readable by both gm/gm_expectations.cpp and Python.
epoger@google.coma783f2b2013-07-08 17:51:58 +000029
epoger@google.com06e626d2013-09-03 17:32:15 +000030
epoger@google.com9d331542013-05-28 15:25:38 +000031JSONKEY_ACTUALRESULTS = 'actual-results'
epoger@google.com06e626d2013-09-03 17:32:15 +000032
epoger@google.coma783f2b2013-07-08 17:51:58 +000033# Tests whose results failed to match expectations.
epoger@google.com9d331542013-05-28 15:25:38 +000034JSONKEY_ACTUALRESULTS_FAILED = 'failed'
epoger@google.com06e626d2013-09-03 17:32:15 +000035
epoger@google.coma783f2b2013-07-08 17:51:58 +000036# Tests whose results failed to match expectations, but IGNOREFAILURE causes
37# us to take them less seriously.
epoger@google.com9d331542013-05-28 15:25:38 +000038JSONKEY_ACTUALRESULTS_FAILUREIGNORED = 'failure-ignored'
epoger@google.com06e626d2013-09-03 17:32:15 +000039
epoger@google.coma783f2b2013-07-08 17:51:58 +000040# Tests for which we do not have any expectations. They may be new tests that
41# we haven't had a chance to check in expectations for yet, or we may have
42# consciously decided to leave them without expectations because we are unhappy
43# with the results (although we should try to move away from that, and instead
44# check in expectations with the IGNOREFAILURE flag set).
epoger@google.com9d331542013-05-28 15:25:38 +000045JSONKEY_ACTUALRESULTS_NOCOMPARISON = 'no-comparison'
epoger@google.com06e626d2013-09-03 17:32:15 +000046
epoger@google.coma783f2b2013-07-08 17:51:58 +000047# Tests whose results matched their expectations.
epoger@google.com9d331542013-05-28 15:25:38 +000048JSONKEY_ACTUALRESULTS_SUCCEEDED = 'succeeded'
49
epoger@google.com06e626d2013-09-03 17:32:15 +000050
epogerb492c6f2014-08-14 07:32:49 -070051# Descriptions of the result set as a whole.
52JSONKEY_DESCRIPTIONS = 'descriptions'
53
54
epoger@google.com53953b42013-07-02 20:22:27 +000055JSONKEY_EXPECTEDRESULTS = 'expected-results'
epoger@google.com06e626d2013-09-03 17:32:15 +000056
epoger@google.coma783f2b2013-07-08 17:51:58 +000057# One or more [HashType/DigestValue] pairs representing valid results for this
58# test. Typically, there will just be one pair, but we allow for multiple
59# expectations, and the test will pass if any one of them is matched.
epoger@google.com53953b42013-07-02 20:22:27 +000060JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS = 'allowed-digests'
epoger@google.com06e626d2013-09-03 17:32:15 +000061
62# Optional: one or more integers listing Skia bugs (under
63# https://code.google.com/p/skia/issues/list ) that pertain to this expectation.
64JSONKEY_EXPECTEDRESULTS_BUGS = 'bugs'
65
epoger@google.coma783f2b2013-07-08 17:51:58 +000066# If IGNOREFAILURE is set to True, a failure of this test will be reported
67# within the FAILUREIGNORED section (thus NOT causing the buildbots to go red)
68# rather than the FAILED section (which WOULD cause the buildbots to go red).
epoger@google.com53953b42013-07-02 20:22:27 +000069JSONKEY_EXPECTEDRESULTS_IGNOREFAILURE = 'ignore-failure'
70
epoger@google.com06e626d2013-09-03 17:32:15 +000071# Optional: a free-form text string with human-readable information about
72# this expectation.
73JSONKEY_EXPECTEDRESULTS_NOTES = 'notes'
74
75# Optional: boolean indicating whether this expectation was reviewed/approved
76# by a human being.
77# If True: a human looked at this image and approved it.
78# If False: this expectation was committed blind. (In such a case, please
79# add notes indicating why!)
80# If absent: this expectation was committed by a tool that didn't enforce human
81# review of expectations.
82JSONKEY_EXPECTEDRESULTS_REVIEWED = 'reviewed-by-human'
83
epoger@google.coma783f2b2013-07-08 17:51:58 +000084# Allowed hash types for test expectations.
epoger@google.com53953b42013-07-02 20:22:27 +000085JSONKEY_HASHTYPE_BITMAP_64BITMD5 = 'bitmap-64bitMD5'
86
commit-bot@chromium.org7418bd82014-05-06 15:31:31 +000087JSONKEY_HEADER = 'header'
88JSONKEY_HEADER_TYPE = 'type'
89JSONKEY_HEADER_REVISION = 'revision'
90JSONKEY_IMAGE_CHECKSUMALGORITHM = 'checksumAlgorithm'
91JSONKEY_IMAGE_CHECKSUMVALUE = 'checksumValue'
92JSONKEY_IMAGE_COMPARISONRESULT = 'comparisonResult'
93JSONKEY_IMAGE_FILEPATH = 'filepath'
94JSONKEY_SOURCE_TILEDIMAGES = 'tiled-images'
95JSONKEY_SOURCE_WHOLEIMAGE = 'whole-image'
96
97
epoger@google.com61822a22013-07-16 18:56:32 +000098# Root directory where the buildbots store their actually-generated images...
99# as a publicly readable HTTP URL:
100GM_ACTUALS_ROOT_HTTP_URL = (
101 'http://chromium-skia-gm.commondatastorage.googleapis.com/gm')
102# as a GS URL that allows credential-protected write access:
103GM_ACTUALS_ROOT_GS_URL = 'gs://chromium-skia-gm/gm'
104
scroggo@google.comd23e6832013-10-10 21:09:24 +0000105# Root directory where buildbots store skimage actual results json files.
106SKIMAGE_ACTUALS_BASE_URL = (
107 'http://chromium-skia-gm.commondatastorage.googleapis.com/skimage/actuals')
108# Root directory inside trunk where skimage expectations are stored.
109SKIMAGE_EXPECTATIONS_ROOT = os.path.join('expectations', 'skimage')
110
epoger@google.com61822a22013-07-16 18:56:32 +0000111# Pattern used to assemble each image's filename
commit-bot@chromium.org69ac6de2014-01-07 17:21:11 +0000112IMAGE_FILENAME_PATTERN = '(.+)_(.+)\.png' # matches (testname, config)
epoger@google.com61822a22013-07-16 18:56:32 +0000113
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000114# Pattern used to create image URLs, relative to some base URL.
115GM_RELATIVE_URL_FORMATTER = '%s/%s/%s.png' # pass in (hash_type, test_name,
116 # hash_digest)
117GM_RELATIVE_URL_PATTERN = '(.+)/(.+)/(.+).png' # matches (hash_type, test_name,
118 # hash_digest)
119GM_RELATIVE_URL_RE = re.compile(GM_RELATIVE_URL_PATTERN)
120
121
epoger@google.com61822a22013-07-16 18:56:32 +0000122def CreateGmActualUrl(test_name, hash_type, hash_digest,
123 gm_actuals_root_url=GM_ACTUALS_ROOT_HTTP_URL):
124 """Return the URL we can use to download a particular version of
125 the actually-generated image for this particular GM test.
126
127 test_name: name of the test, e.g. 'perlinnoise'
128 hash_type: string indicating the hash type used to generate hash_digest,
129 e.g. JSONKEY_HASHTYPE_BITMAP_64BITMD5
130 hash_digest: the hash digest of the image to retrieve
131 gm_actuals_root_url: root url where actual images are stored
132 """
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000133 return posixpath.join(
134 gm_actuals_root_url, CreateGmRelativeUrl(
135 test_name=test_name, hash_type=hash_type, hash_digest=hash_digest))
136
137
138def CreateGmRelativeUrl(test_name, hash_type, hash_digest):
139 """Returns a relative URL pointing at a test result's image.
140
141 Returns the URL we can use to download a particular version of
142 the actually-generated image for this particular GM test,
143 relative to the URL root.
144
145 Args:
146 test_name: name of the test, e.g. 'perlinnoise'
147 hash_type: string indicating the hash type used to generate hash_digest,
148 e.g. JSONKEY_HASHTYPE_BITMAP_64BITMD5
149 hash_digest: the hash digest of the image to retrieve
150 """
151 return GM_RELATIVE_URL_FORMATTER % (hash_type, test_name, hash_digest)
152
153
154def SplitGmRelativeUrl(url):
155 """Splits the relative URL into (test_name, hash_type, hash_digest) tuple.
156
157 This is the inverse of CreateGmRelativeUrl().
158
159 Args:
160 url: a URL generated with CreateGmRelativeUrl().
161
162 Returns: (test_name, hash_type, hash_digest) tuple.
163 """
164 hash_type, test_name, hash_digest = GM_RELATIVE_URL_RE.match(url).groups()
165 return (test_name, hash_type, hash_digest)
166
epoger@google.com61822a22013-07-16 18:56:32 +0000167
epoger@google.com4075fd42013-06-04 17:50:36 +0000168def LoadFromString(file_contents):
epoger@google.com9d331542013-05-28 15:25:38 +0000169 """Loads the JSON summary written out by the GM tool.
epoger5c4b1372014-08-14 07:12:46 -0700170
epoger@google.com9d331542013-05-28 15:25:38 +0000171 Returns a dictionary keyed by the values listed as JSONKEY_ constants
epoger5c4b1372014-08-14 07:12:46 -0700172 above; if file_contents is empty, returns None."""
epoger@google.com4075fd42013-06-04 17:50:36 +0000173 # TODO(epoger): we should add a version number to the JSON file to ensure
epoger@google.com9d331542013-05-28 15:25:38 +0000174 # that the writer and reader agree on the schema (raising an exception
175 # otherwise).
epoger5c4b1372014-08-14 07:12:46 -0700176 if not file_contents:
177 return None
epoger@google.com4075fd42013-06-04 17:50:36 +0000178 json_dict = json.loads(file_contents)
epoger@google.com9d331542013-05-28 15:25:38 +0000179 return json_dict
epoger@google.com4075fd42013-06-04 17:50:36 +0000180
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000181
epoger@google.com4075fd42013-06-04 17:50:36 +0000182def LoadFromFile(file_path):
183 """Loads the JSON summary written out by the GM tool.
184 Returns a dictionary keyed by the values listed as JSONKEY_ constants
185 above."""
186 file_contents = open(file_path, 'r').read()
187 return LoadFromString(file_contents)
epoger@google.coma783f2b2013-07-08 17:51:58 +0000188
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000189
epoger@google.coma783f2b2013-07-08 17:51:58 +0000190def WriteToFile(json_dict, file_path):
commit-bot@chromium.org83aaf882013-12-18 15:28:24 +0000191 """Writes the JSON summary in json_dict out to file_path.
192
193 The file is written Unix-style (each line ends with just LF, not CRLF);
194 see https://code.google.com/p/skia/issues/detail?id=1815 for reasons."""
195 with io.open(file_path, mode='w', newline='', encoding='utf-8') as outfile:
196 outfile.write(unicode(json.dumps(json_dict, outfile, sort_keys=True,
197 indent=2)))