epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 1 | #!/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 | |
| 8 | This 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.org | 83aaf88 | 2013-12-18 15:28:24 +0000 | [diff] [blame] | 15 | import io |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 16 | import json |
scroggo@google.com | d23e683 | 2013-10-10 21:09:24 +0000 | [diff] [blame] | 17 | import os |
commit-bot@chromium.org | 16f4180 | 2014-02-26 19:05:20 +0000 | [diff] [blame] | 18 | import posixpath |
| 19 | import re |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 20 | |
| 21 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 22 | # Key strings used in GM results JSON files (both expected-results.json and |
| 23 | # actual-results.json). |
| 24 | # |
commit-bot@chromium.org | 16f4180 | 2014-02-26 19:05:20 +0000 | [diff] [blame] | 25 | # NOTE: These constants must be kept in sync with the kJsonKey_ constants in |
commit-bot@chromium.org | 7418bd8 | 2014-05-06 15:31:31 +0000 | [diff] [blame] | 26 | # 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.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 29 | |
epoger@google.com | 06e626d | 2013-09-03 17:32:15 +0000 | [diff] [blame] | 30 | |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 31 | JSONKEY_ACTUALRESULTS = 'actual-results' |
epoger@google.com | 06e626d | 2013-09-03 17:32:15 +0000 | [diff] [blame] | 32 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 33 | # Tests whose results failed to match expectations. |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 34 | JSONKEY_ACTUALRESULTS_FAILED = 'failed' |
epoger@google.com | 06e626d | 2013-09-03 17:32:15 +0000 | [diff] [blame] | 35 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 36 | # Tests whose results failed to match expectations, but IGNOREFAILURE causes |
| 37 | # us to take them less seriously. |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 38 | JSONKEY_ACTUALRESULTS_FAILUREIGNORED = 'failure-ignored' |
epoger@google.com | 06e626d | 2013-09-03 17:32:15 +0000 | [diff] [blame] | 39 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 40 | # 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.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 45 | JSONKEY_ACTUALRESULTS_NOCOMPARISON = 'no-comparison' |
epoger@google.com | 06e626d | 2013-09-03 17:32:15 +0000 | [diff] [blame] | 46 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 47 | # Tests whose results matched their expectations. |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 48 | JSONKEY_ACTUALRESULTS_SUCCEEDED = 'succeeded' |
| 49 | |
epoger@google.com | 06e626d | 2013-09-03 17:32:15 +0000 | [diff] [blame] | 50 | |
epoger | b492c6f | 2014-08-14 07:32:49 -0700 | [diff] [blame] | 51 | # Descriptions of the result set as a whole. |
| 52 | JSONKEY_DESCRIPTIONS = 'descriptions' |
stephana | 3b5c86c | 2014-08-18 13:37:59 -0700 | [diff] [blame] | 53 | JSONKEY_DESCRIPTIONS_BUILDER = 'builder' |
| 54 | JSONKEY_DESCRIPTIONS_RENDER_MODE = 'renderMode' |
epoger | b492c6f | 2014-08-14 07:32:49 -0700 | [diff] [blame] | 55 | |
epoger@google.com | 53953b4 | 2013-07-02 20:22:27 +0000 | [diff] [blame] | 56 | JSONKEY_EXPECTEDRESULTS = 'expected-results' |
epoger@google.com | 06e626d | 2013-09-03 17:32:15 +0000 | [diff] [blame] | 57 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 58 | # One or more [HashType/DigestValue] pairs representing valid results for this |
| 59 | # test. Typically, there will just be one pair, but we allow for multiple |
| 60 | # expectations, and the test will pass if any one of them is matched. |
epoger@google.com | 53953b4 | 2013-07-02 20:22:27 +0000 | [diff] [blame] | 61 | JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS = 'allowed-digests' |
epoger@google.com | 06e626d | 2013-09-03 17:32:15 +0000 | [diff] [blame] | 62 | |
| 63 | # Optional: one or more integers listing Skia bugs (under |
| 64 | # https://code.google.com/p/skia/issues/list ) that pertain to this expectation. |
| 65 | JSONKEY_EXPECTEDRESULTS_BUGS = 'bugs' |
| 66 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 67 | # If IGNOREFAILURE is set to True, a failure of this test will be reported |
| 68 | # within the FAILUREIGNORED section (thus NOT causing the buildbots to go red) |
| 69 | # rather than the FAILED section (which WOULD cause the buildbots to go red). |
epoger@google.com | 53953b4 | 2013-07-02 20:22:27 +0000 | [diff] [blame] | 70 | JSONKEY_EXPECTEDRESULTS_IGNOREFAILURE = 'ignore-failure' |
| 71 | |
epoger@google.com | 06e626d | 2013-09-03 17:32:15 +0000 | [diff] [blame] | 72 | # Optional: a free-form text string with human-readable information about |
| 73 | # this expectation. |
| 74 | JSONKEY_EXPECTEDRESULTS_NOTES = 'notes' |
| 75 | |
| 76 | # Optional: boolean indicating whether this expectation was reviewed/approved |
| 77 | # by a human being. |
| 78 | # If True: a human looked at this image and approved it. |
| 79 | # If False: this expectation was committed blind. (In such a case, please |
| 80 | # add notes indicating why!) |
| 81 | # If absent: this expectation was committed by a tool that didn't enforce human |
| 82 | # review of expectations. |
| 83 | JSONKEY_EXPECTEDRESULTS_REVIEWED = 'reviewed-by-human' |
| 84 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 85 | # Allowed hash types for test expectations. |
epoger@google.com | 53953b4 | 2013-07-02 20:22:27 +0000 | [diff] [blame] | 86 | JSONKEY_HASHTYPE_BITMAP_64BITMD5 = 'bitmap-64bitMD5' |
| 87 | |
commit-bot@chromium.org | 7418bd8 | 2014-05-06 15:31:31 +0000 | [diff] [blame] | 88 | JSONKEY_HEADER = 'header' |
| 89 | JSONKEY_HEADER_TYPE = 'type' |
| 90 | JSONKEY_HEADER_REVISION = 'revision' |
| 91 | JSONKEY_IMAGE_CHECKSUMALGORITHM = 'checksumAlgorithm' |
| 92 | JSONKEY_IMAGE_CHECKSUMVALUE = 'checksumValue' |
| 93 | JSONKEY_IMAGE_COMPARISONRESULT = 'comparisonResult' |
| 94 | JSONKEY_IMAGE_FILEPATH = 'filepath' |
| 95 | JSONKEY_SOURCE_TILEDIMAGES = 'tiled-images' |
| 96 | JSONKEY_SOURCE_WHOLEIMAGE = 'whole-image' |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 97 | JSONKEY_IMAGE_BASE_GS_URL = 'image-base-gs-url' |
commit-bot@chromium.org | 7418bd8 | 2014-05-06 15:31:31 +0000 | [diff] [blame] | 98 | |
| 99 | |
epoger@google.com | 61822a2 | 2013-07-16 18:56:32 +0000 | [diff] [blame] | 100 | # Root directory where the buildbots store their actually-generated images... |
| 101 | # as a publicly readable HTTP URL: |
| 102 | GM_ACTUALS_ROOT_HTTP_URL = ( |
| 103 | 'http://chromium-skia-gm.commondatastorage.googleapis.com/gm') |
| 104 | # as a GS URL that allows credential-protected write access: |
| 105 | GM_ACTUALS_ROOT_GS_URL = 'gs://chromium-skia-gm/gm' |
| 106 | |
scroggo@google.com | d23e683 | 2013-10-10 21:09:24 +0000 | [diff] [blame] | 107 | # Root directory where buildbots store skimage actual results json files. |
| 108 | SKIMAGE_ACTUALS_BASE_URL = ( |
| 109 | 'http://chromium-skia-gm.commondatastorage.googleapis.com/skimage/actuals') |
| 110 | # Root directory inside trunk where skimage expectations are stored. |
| 111 | SKIMAGE_EXPECTATIONS_ROOT = os.path.join('expectations', 'skimage') |
| 112 | |
epoger@google.com | 61822a2 | 2013-07-16 18:56:32 +0000 | [diff] [blame] | 113 | # Pattern used to assemble each image's filename |
commit-bot@chromium.org | 69ac6de | 2014-01-07 17:21:11 +0000 | [diff] [blame] | 114 | IMAGE_FILENAME_PATTERN = '(.+)_(.+)\.png' # matches (testname, config) |
epoger@google.com | 61822a2 | 2013-07-16 18:56:32 +0000 | [diff] [blame] | 115 | |
commit-bot@chromium.org | 16f4180 | 2014-02-26 19:05:20 +0000 | [diff] [blame] | 116 | # Pattern used to create image URLs, relative to some base URL. |
| 117 | GM_RELATIVE_URL_FORMATTER = '%s/%s/%s.png' # pass in (hash_type, test_name, |
| 118 | # hash_digest) |
| 119 | GM_RELATIVE_URL_PATTERN = '(.+)/(.+)/(.+).png' # matches (hash_type, test_name, |
| 120 | # hash_digest) |
| 121 | GM_RELATIVE_URL_RE = re.compile(GM_RELATIVE_URL_PATTERN) |
| 122 | |
| 123 | |
epoger@google.com | 61822a2 | 2013-07-16 18:56:32 +0000 | [diff] [blame] | 124 | def CreateGmActualUrl(test_name, hash_type, hash_digest, |
| 125 | gm_actuals_root_url=GM_ACTUALS_ROOT_HTTP_URL): |
| 126 | """Return the URL we can use to download a particular version of |
| 127 | the actually-generated image for this particular GM test. |
| 128 | |
| 129 | test_name: name of the test, e.g. 'perlinnoise' |
| 130 | hash_type: string indicating the hash type used to generate hash_digest, |
| 131 | e.g. JSONKEY_HASHTYPE_BITMAP_64BITMD5 |
| 132 | hash_digest: the hash digest of the image to retrieve |
| 133 | gm_actuals_root_url: root url where actual images are stored |
| 134 | """ |
commit-bot@chromium.org | 16f4180 | 2014-02-26 19:05:20 +0000 | [diff] [blame] | 135 | return posixpath.join( |
| 136 | gm_actuals_root_url, CreateGmRelativeUrl( |
| 137 | test_name=test_name, hash_type=hash_type, hash_digest=hash_digest)) |
| 138 | |
| 139 | |
| 140 | def CreateGmRelativeUrl(test_name, hash_type, hash_digest): |
| 141 | """Returns a relative URL pointing at a test result's image. |
| 142 | |
| 143 | Returns the URL we can use to download a particular version of |
| 144 | the actually-generated image for this particular GM test, |
| 145 | relative to the URL root. |
| 146 | |
| 147 | Args: |
| 148 | test_name: name of the test, e.g. 'perlinnoise' |
| 149 | hash_type: string indicating the hash type used to generate hash_digest, |
| 150 | e.g. JSONKEY_HASHTYPE_BITMAP_64BITMD5 |
| 151 | hash_digest: the hash digest of the image to retrieve |
| 152 | """ |
| 153 | return GM_RELATIVE_URL_FORMATTER % (hash_type, test_name, hash_digest) |
| 154 | |
| 155 | |
| 156 | def SplitGmRelativeUrl(url): |
| 157 | """Splits the relative URL into (test_name, hash_type, hash_digest) tuple. |
| 158 | |
| 159 | This is the inverse of CreateGmRelativeUrl(). |
| 160 | |
| 161 | Args: |
| 162 | url: a URL generated with CreateGmRelativeUrl(). |
| 163 | |
| 164 | Returns: (test_name, hash_type, hash_digest) tuple. |
| 165 | """ |
| 166 | hash_type, test_name, hash_digest = GM_RELATIVE_URL_RE.match(url).groups() |
| 167 | return (test_name, hash_type, hash_digest) |
| 168 | |
epoger@google.com | 61822a2 | 2013-07-16 18:56:32 +0000 | [diff] [blame] | 169 | |
epoger@google.com | 4075fd4 | 2013-06-04 17:50:36 +0000 | [diff] [blame] | 170 | def LoadFromString(file_contents): |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 171 | """Loads the JSON summary written out by the GM tool. |
epoger | 5c4b137 | 2014-08-14 07:12:46 -0700 | [diff] [blame] | 172 | |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 173 | Returns a dictionary keyed by the values listed as JSONKEY_ constants |
epoger | 5c4b137 | 2014-08-14 07:12:46 -0700 | [diff] [blame] | 174 | above; if file_contents is empty, returns None.""" |
epoger@google.com | 4075fd4 | 2013-06-04 17:50:36 +0000 | [diff] [blame] | 175 | # TODO(epoger): we should add a version number to the JSON file to ensure |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 176 | # that the writer and reader agree on the schema (raising an exception |
| 177 | # otherwise). |
epoger | 5c4b137 | 2014-08-14 07:12:46 -0700 | [diff] [blame] | 178 | if not file_contents: |
| 179 | return None |
epoger@google.com | 4075fd4 | 2013-06-04 17:50:36 +0000 | [diff] [blame] | 180 | json_dict = json.loads(file_contents) |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 181 | return json_dict |
epoger@google.com | 4075fd4 | 2013-06-04 17:50:36 +0000 | [diff] [blame] | 182 | |
commit-bot@chromium.org | 16f4180 | 2014-02-26 19:05:20 +0000 | [diff] [blame] | 183 | |
epoger@google.com | 4075fd4 | 2013-06-04 17:50:36 +0000 | [diff] [blame] | 184 | def LoadFromFile(file_path): |
| 185 | """Loads the JSON summary written out by the GM tool. |
| 186 | Returns a dictionary keyed by the values listed as JSONKEY_ constants |
| 187 | above.""" |
| 188 | file_contents = open(file_path, 'r').read() |
| 189 | return LoadFromString(file_contents) |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 190 | |
commit-bot@chromium.org | 16f4180 | 2014-02-26 19:05:20 +0000 | [diff] [blame] | 191 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 192 | def WriteToFile(json_dict, file_path): |
commit-bot@chromium.org | 83aaf88 | 2013-12-18 15:28:24 +0000 | [diff] [blame] | 193 | """Writes the JSON summary in json_dict out to file_path. |
| 194 | |
| 195 | The file is written Unix-style (each line ends with just LF, not CRLF); |
| 196 | see https://code.google.com/p/skia/issues/detail?id=1815 for reasons.""" |
| 197 | with io.open(file_path, mode='w', newline='', encoding='utf-8') as outfile: |
| 198 | outfile.write(unicode(json.dumps(json_dict, outfile, sort_keys=True, |
| 199 | indent=2))) |