blob: 44ec5ea502ce5a675851573e4e3279213f433cc8 [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
15import json
16
17
epoger@google.coma783f2b2013-07-08 17:51:58 +000018# Key strings used in GM results JSON files (both expected-results.json and
19# actual-results.json).
20#
epoger@google.com9d331542013-05-28 15:25:38 +000021# These constants must be kept in sync with the kJsonKey_ constants in
22# gm_expectations.cpp !
epoger@google.coma783f2b2013-07-08 17:51:58 +000023
epoger@google.com9d331542013-05-28 15:25:38 +000024JSONKEY_ACTUALRESULTS = 'actual-results'
epoger@google.coma783f2b2013-07-08 17:51:58 +000025# Tests whose results failed to match expectations.
epoger@google.com9d331542013-05-28 15:25:38 +000026JSONKEY_ACTUALRESULTS_FAILED = 'failed'
epoger@google.coma783f2b2013-07-08 17:51:58 +000027# Tests whose results failed to match expectations, but IGNOREFAILURE causes
28# us to take them less seriously.
epoger@google.com9d331542013-05-28 15:25:38 +000029JSONKEY_ACTUALRESULTS_FAILUREIGNORED = 'failure-ignored'
epoger@google.coma783f2b2013-07-08 17:51:58 +000030# Tests for which we do not have any expectations. They may be new tests that
31# we haven't had a chance to check in expectations for yet, or we may have
32# consciously decided to leave them without expectations because we are unhappy
33# with the results (although we should try to move away from that, and instead
34# check in expectations with the IGNOREFAILURE flag set).
epoger@google.com9d331542013-05-28 15:25:38 +000035JSONKEY_ACTUALRESULTS_NOCOMPARISON = 'no-comparison'
epoger@google.coma783f2b2013-07-08 17:51:58 +000036# Tests whose results matched their expectations.
epoger@google.com9d331542013-05-28 15:25:38 +000037JSONKEY_ACTUALRESULTS_SUCCEEDED = 'succeeded'
38
epoger@google.com53953b42013-07-02 20:22:27 +000039JSONKEY_EXPECTEDRESULTS = 'expected-results'
epoger@google.coma783f2b2013-07-08 17:51:58 +000040# One or more [HashType/DigestValue] pairs representing valid results for this
41# test. Typically, there will just be one pair, but we allow for multiple
42# expectations, and the test will pass if any one of them is matched.
epoger@google.com53953b42013-07-02 20:22:27 +000043JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS = 'allowed-digests'
epoger@google.coma783f2b2013-07-08 17:51:58 +000044# If IGNOREFAILURE is set to True, a failure of this test will be reported
45# within the FAILUREIGNORED section (thus NOT causing the buildbots to go red)
46# rather than the FAILED section (which WOULD cause the buildbots to go red).
epoger@google.com53953b42013-07-02 20:22:27 +000047JSONKEY_EXPECTEDRESULTS_IGNOREFAILURE = 'ignore-failure'
48
epoger@google.coma783f2b2013-07-08 17:51:58 +000049# Allowed hash types for test expectations.
epoger@google.com53953b42013-07-02 20:22:27 +000050JSONKEY_HASHTYPE_BITMAP_64BITMD5 = 'bitmap-64bitMD5'
51
epoger@google.com61822a22013-07-16 18:56:32 +000052# Root directory where the buildbots store their actually-generated images...
53# as a publicly readable HTTP URL:
54GM_ACTUALS_ROOT_HTTP_URL = (
55 'http://chromium-skia-gm.commondatastorage.googleapis.com/gm')
56# as a GS URL that allows credential-protected write access:
57GM_ACTUALS_ROOT_GS_URL = 'gs://chromium-skia-gm/gm'
58
59# Pattern used to assemble each image's filename
60IMAGE_FILENAME_PATTERN = '(\S+)_(\S+).png' # matches (testname, config)
61
62def CreateGmActualUrl(test_name, hash_type, hash_digest,
63 gm_actuals_root_url=GM_ACTUALS_ROOT_HTTP_URL):
64 """Return the URL we can use to download a particular version of
65 the actually-generated image for this particular GM test.
66
67 test_name: name of the test, e.g. 'perlinnoise'
68 hash_type: string indicating the hash type used to generate hash_digest,
69 e.g. JSONKEY_HASHTYPE_BITMAP_64BITMD5
70 hash_digest: the hash digest of the image to retrieve
71 gm_actuals_root_url: root url where actual images are stored
72 """
73 return '%s/%s/%s/%s.png' % (gm_actuals_root_url, hash_type, test_name,
74 hash_digest)
75
epoger@google.com4075fd42013-06-04 17:50:36 +000076def LoadFromString(file_contents):
epoger@google.com9d331542013-05-28 15:25:38 +000077 """Loads the JSON summary written out by the GM tool.
78 Returns a dictionary keyed by the values listed as JSONKEY_ constants
79 above."""
epoger@google.com4075fd42013-06-04 17:50:36 +000080 # TODO(epoger): we should add a version number to the JSON file to ensure
epoger@google.com9d331542013-05-28 15:25:38 +000081 # that the writer and reader agree on the schema (raising an exception
82 # otherwise).
epoger@google.com4075fd42013-06-04 17:50:36 +000083 json_dict = json.loads(file_contents)
epoger@google.com9d331542013-05-28 15:25:38 +000084 return json_dict
epoger@google.com4075fd42013-06-04 17:50:36 +000085
86def LoadFromFile(file_path):
87 """Loads the JSON summary written out by the GM tool.
88 Returns a dictionary keyed by the values listed as JSONKEY_ constants
89 above."""
90 file_contents = open(file_path, 'r').read()
91 return LoadFromString(file_contents)
epoger@google.coma783f2b2013-07-08 17:51:58 +000092
93def WriteToFile(json_dict, file_path):
94 """Writes the JSON summary in json_dict out to file_path."""
95 with open(file_path, 'w') as outfile:
96 json.dump(json_dict, outfile, sort_keys=True, indent=2)