blob: 8faa02ea1f0f2bcb134b2c0153adaa0ed63ba82a [file] [log] [blame]
senorblanco@chromium.org782f3b42012-10-29 18:06:26 +00001#!/usr/bin/python
2
3'''
4Copyright 2012 Google Inc.
5
6Use of this source code is governed by a BSD-style license that can be
7found in the LICENSE file.
8'''
9
10'''
senorblanco@chromium.org123a0b52012-11-29 21:50:34 +000011Rebaselines the given GM tests, on all bots and all configurations.
12Must be run from the gm-expected directory. If run from a git or SVN
13checkout, the files will be added to the staging area for commit.
senorblanco@chromium.org782f3b42012-10-29 18:06:26 +000014'''
15
epoger@google.com99ba65a2013-06-05 15:43:37 +000016# System-level imports
epoger@google.com9166bf52013-05-30 15:46:19 +000017import argparse
epoger@google.comec3397b2013-05-29 17:09:43 +000018import os
epoger@google.come78d2072013-06-12 17:44:14 +000019import re
epoger@google.comec3397b2013-05-29 17:09:43 +000020import sys
epoger@google.com99ba65a2013-06-05 15:43:37 +000021import urllib2
22
epoger@google.com99a8ec92013-06-19 18:56:59 +000023# Imports from local directory
24import rebaseline_imagefiles
25
epoger@google.com99ba65a2013-06-05 15:43:37 +000026# Imports from within Skia
27#
epoger@google.comdad53102013-06-12 14:25:30 +000028# We need to add the 'gm' directory, so that we can import gm_json.py within
29# that directory. That script allows us to parse the actual-results.json file
30# written out by the GM tool.
31# Make sure that the 'gm' dir is in the PYTHONPATH, but add it at the *end*
32# so any dirs that are already in the PYTHONPATH will be preferred.
33#
34# This assumes that the 'gm' directory has been checked out as a sibling of
35# the 'tools' directory containing this script, which will be the case if
36# 'trunk' was checked out as a single unit.
epoger@google.com99ba65a2013-06-05 15:43:37 +000037GM_DIRECTORY = os.path.realpath(
38 os.path.join(os.path.dirname(os.path.dirname(__file__)), 'gm'))
39if GM_DIRECTORY not in sys.path:
40 sys.path.append(GM_DIRECTORY)
41import gm_json
42
epoger@google.comec3397b2013-05-29 17:09:43 +000043# Mapping of gm-expectations subdir (under
44# https://skia.googlecode.com/svn/gm-expected/ )
45# to builder name (see list at http://108.170.217.252:10117/builders )
epoger@google.com9166bf52013-05-30 15:46:19 +000046SUBDIR_MAPPING = {
epoger@google.comec3397b2013-05-29 17:09:43 +000047 'base-shuttle-win7-intel-float':
48 'Test-Win7-ShuttleA-HD2000-x86-Release',
49 'base-shuttle-win7-intel-angle':
50 'Test-Win7-ShuttleA-HD2000-x86-Release-ANGLE',
51 'base-shuttle-win7-intel-directwrite':
52 'Test-Win7-ShuttleA-HD2000-x86-Release-DirectWrite',
53 'base-shuttle_ubuntu12_ati5770':
54 'Test-Ubuntu12-ShuttleA-ATI5770-x86_64-Release',
55 'base-macmini':
56 'Test-Mac10.6-MacMini4.1-GeForce320M-x86-Release',
57 'base-macmini-lion-float':
58 'Test-Mac10.7-MacMini4.1-GeForce320M-x86-Release',
59 'base-android-galaxy-nexus':
60 'Test-Android-GalaxyNexus-SGX540-Arm7-Debug',
61 'base-android-nexus-7':
62 'Test-Android-Nexus7-Tegra3-Arm7-Release',
63 'base-android-nexus-s':
64 'Test-Android-NexusS-SGX540-Arm7-Release',
65 'base-android-xoom':
66 'Test-Android-Xoom-Tegra2-Arm7-Release',
67 'base-android-nexus-10':
68 'Test-Android-Nexus10-MaliT604-Arm7-Release',
robertphillips@google.com63e96272013-07-02 12:54:37 +000069 'base-android-nexus-4':
70 'Test-Android-Nexus4-Adreno320-Arm7-Release',
epoger@google.comec3397b2013-05-29 17:09:43 +000071}
72
epoger@google.com9166bf52013-05-30 15:46:19 +000073
epoger@google.comdb29a312013-06-04 14:58:47 +000074class CommandFailedException(Exception):
75 pass
76
epoger@google.com99a8ec92013-06-19 18:56:59 +000077# Object that rebaselines a JSON expectations file (not individual image files).
epoger@google.com99a8ec92013-06-19 18:56:59 +000078class JsonRebaseliner(object):
epoger@google.com9166bf52013-05-30 15:46:19 +000079
80 # params:
epoger@google.coma783f2b2013-07-08 17:51:58 +000081 # expectations_root: root directory of all expectations JSON files
82 # expectations_filename: filename (under expectations_root) of JSON
83 # expectations file; typically
84 # "expected-results.json"
85 # actuals_base_url: base URL from which to read actual-result JSON files
86 # actuals_filename: filename (under actuals_base_url) from which to read a
87 # summary of results; typically "actual-results.json"
epoger@google.com99ba65a2013-06-05 15:43:37 +000088 # tests: list of tests to rebaseline, or None if we should rebaseline
89 # whatever files the JSON results summary file tells us to
epoger@google.com9de25e32013-07-10 15:27:18 +000090 # configs: which configs to run for each test, or None if we should
91 # rebaseline whatever configs the JSON results summary file tells
92 # us to
epoger@google.comdad53102013-06-12 14:25:30 +000093 # add_new: if True, add expectations for tests which don't have any yet
epoger@google.coma783f2b2013-07-08 17:51:58 +000094 def __init__(self, expectations_root, expectations_filename,
95 actuals_base_url, actuals_filename,
96 tests=None, configs=None, add_new=False):
epoger@google.com99a8ec92013-06-19 18:56:59 +000097 self._expectations_root = expectations_root
epoger@google.coma783f2b2013-07-08 17:51:58 +000098 self._expectations_filename = expectations_filename
epoger@google.com9166bf52013-05-30 15:46:19 +000099 self._tests = tests
100 self._configs = configs
epoger@google.coma783f2b2013-07-08 17:51:58 +0000101 self._actuals_base_url = actuals_base_url
102 self._actuals_filename = actuals_filename
epoger@google.comdad53102013-06-12 14:25:30 +0000103 self._add_new = add_new
epoger@google.come78d2072013-06-12 17:44:14 +0000104 self._testname_pattern = re.compile('(\S+)_(\S+).png')
epoger@google.com9166bf52013-05-30 15:46:19 +0000105
epoger@google.coma783f2b2013-07-08 17:51:58 +0000106 # Returns the full contents of filepath, as a single string.
107 # If filepath looks like a URL, try to read it that way instead of as
108 # a path on local storage.
109 def _GetFileContents(self, filepath):
110 if filepath.startswith('http:') or filepath.startswith('https:'):
111 return urllib2.urlopen(filepath).read()
epoger@google.com99ba65a2013-06-05 15:43:37 +0000112 else:
epoger@google.coma783f2b2013-07-08 17:51:58 +0000113 return open(filepath, 'r').read()
epoger@google.com99ba65a2013-06-05 15:43:37 +0000114
epoger@google.come78d2072013-06-12 17:44:14 +0000115 # Returns a dictionary of actual results from actual-results.json file.
116 #
117 # The dictionary returned has this format:
118 # {
119 # u'imageblur_565.png': [u'bitmap-64bitMD5', 3359963596899141322],
120 # u'imageblur_8888.png': [u'bitmap-64bitMD5', 4217923806027861152],
121 # u'shadertext3_8888.png': [u'bitmap-64bitMD5', 3713708307125704716]
122 # }
123 #
epoger@google.coma783f2b2013-07-08 17:51:58 +0000124 # If the JSON actual result summary file cannot be loaded, raise an
125 # exception.
epoger@google.come78d2072013-06-12 17:44:14 +0000126 #
127 # params:
128 # json_url: URL pointing to a JSON actual result summary file
129 # sections: a list of section names to include in the results, e.g.
130 # [gm_json.JSONKEY_ACTUALRESULTS_FAILED,
131 # gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON] ;
132 # if None, then include ALL sections.
133 def _GetActualResults(self, json_url, sections=None):
epoger@google.coma783f2b2013-07-08 17:51:58 +0000134 json_contents = self._GetFileContents(json_url)
epoger@google.come78d2072013-06-12 17:44:14 +0000135 json_dict = gm_json.LoadFromString(json_contents)
136 results_to_return = {}
137 actual_results = json_dict[gm_json.JSONKEY_ACTUALRESULTS]
138 if not sections:
139 sections = actual_results.keys()
140 for section in sections:
141 section_results = actual_results[section]
142 if section_results:
143 results_to_return.update(section_results)
144 return results_to_return
145
epoger@google.com99a8ec92013-06-19 18:56:59 +0000146 # Rebaseline all tests/types we specified in the constructor,
147 # within this gm-expectations subdir.
148 #
149 # params:
150 # subdir : e.g. 'base-shuttle-win7-intel-float'
151 # builder : e.g. 'Test-Win7-ShuttleA-HD2000-x86-Release'
152 def RebaselineSubdir(self, subdir, builder):
epoger@google.coma783f2b2013-07-08 17:51:58 +0000153 # Read in the actual result summary, and extract all the tests whose
154 # results we need to update.
155 actuals_url = '/'.join([self._actuals_base_url,
156 subdir, builder, subdir,
157 self._actuals_filename])
158 sections = [gm_json.JSONKEY_ACTUALRESULTS_FAILED]
159 if self._add_new:
160 sections.append(gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON)
161 results_to_update = self._GetActualResults(json_url=actuals_url,
162 sections=sections)
epoger@google.come78d2072013-06-12 17:44:14 +0000163
epoger@google.coma783f2b2013-07-08 17:51:58 +0000164 # Read in current expectations.
165 expectations_json_filepath = os.path.join(
166 self._expectations_root, subdir, self._expectations_filename)
167 expectations_dict = gm_json.LoadFromFile(expectations_json_filepath)
168
169 # Update the expectations in memory, skipping any tests/configs that
170 # the caller asked to exclude.
171 skipped_images = []
172 if results_to_update:
173 for (image_name, image_results) in results_to_update.iteritems():
174 (test, config) = self._testname_pattern.match(image_name).groups()
175 if self._tests:
176 if test not in self._tests:
177 skipped_images.append(image_name)
178 continue
179 if self._configs:
180 if config not in self._configs:
181 skipped_images.append(image_name)
182 continue
183 expectations_dict[gm_json.JSONKEY_EXPECTEDRESULTS] \
184 [image_name] \
185 [gm_json.JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS] = \
186 [image_results]
187
188 # Write out updated expectations.
189 gm_json.WriteToFile(expectations_dict, expectations_json_filepath)
190
epoger@google.comec3397b2013-05-29 17:09:43 +0000191
epoger@google.com9166bf52013-05-30 15:46:19 +0000192# main...
epoger@google.comec3397b2013-05-29 17:09:43 +0000193
epoger@google.com9166bf52013-05-30 15:46:19 +0000194parser = argparse.ArgumentParser()
epoger@google.coma783f2b2013-07-08 17:51:58 +0000195parser.add_argument('--actuals-base-url',
196 help='base URL from which to read files containing JSON ' +
197 'summaries of actual GM results; defaults to %(default)s',
198 default='http://skia-autogen.googlecode.com/svn/gm-actual')
199parser.add_argument('--actuals-filename',
200 help='filename (within platform-specific subdirectories ' +
201 'of ACTUALS_BASE_URL) to read a summary of results from; ' +
202 'defaults to %(default)s',
203 default='actual-results.json')
204# TODO(epoger): Add test that exercises --add-new argument.
epoger@google.comdad53102013-06-12 14:25:30 +0000205parser.add_argument('--add-new', action='store_true',
206 help='in addition to the standard behavior of ' +
207 'updating expectations for failing tests, add ' +
208 'expectations for tests which don\'t have expectations ' +
209 'yet.')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000210# TODO(epoger): Add test that exercises --configs argument.
epoger@google.com9166bf52013-05-30 15:46:19 +0000211parser.add_argument('--configs', metavar='CONFIG', nargs='+',
212 help='which configurations to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000213 '"--configs 565 8888", as a filter over the full set of ' +
214 'results in ACTUALS_FILENAME; if unspecified, rebaseline ' +
215 '*all* configs that are available.')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000216# TODO(epoger): The --dry-run argument will no longer be needed once we
217# are only rebaselining JSON files.
epoger@google.com82f31782013-06-11 15:45:46 +0000218parser.add_argument('--dry-run', action='store_true',
epoger@google.com9166bf52013-05-30 15:46:19 +0000219 help='instead of actually downloading files or adding ' +
220 'files to checkout, display a list of operations that ' +
221 'we would normally perform')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000222parser.add_argument('--expectations-filename',
223 help='filename (under EXPECTATIONS_ROOT) to read ' +
224 'current expectations from, and to write new ' +
225 'expectations into; defaults to %(default)s',
226 default='expected-results.json')
epoger@google.com99a8ec92013-06-19 18:56:59 +0000227parser.add_argument('--expectations-root',
228 help='root of expectations directory to update-- should ' +
229 'contain one or more base-* subdirectories. Defaults to ' +
230 '%(default)s',
231 default='.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000232parser.add_argument('--subdirs', metavar='SUBDIR', nargs='+',
233 help='which platform subdirectories to rebaseline; ' +
234 'if unspecified, rebaseline all subdirs, same as ' +
235 '"--subdirs %s"' % ' '.join(sorted(SUBDIR_MAPPING.keys())))
epoger@google.coma783f2b2013-07-08 17:51:58 +0000236# TODO(epoger): Add test that exercises --tests argument.
epoger@google.com99ba65a2013-06-05 15:43:37 +0000237parser.add_argument('--tests', metavar='TEST', nargs='+',
epoger@google.com9166bf52013-05-30 15:46:19 +0000238 help='which tests to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000239 '"--tests aaclip bigmatrix", as a filter over the full ' +
240 'set of results in ACTUALS_FILENAME; if unspecified, ' +
241 'rebaseline *all* tests that are available.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000242args = parser.parse_args()
epoger@google.com99a8ec92013-06-19 18:56:59 +0000243if args.subdirs:
244 subdirs = args.subdirs
245 missing_json_is_fatal = True
246else:
247 subdirs = sorted(SUBDIR_MAPPING.keys())
248 missing_json_is_fatal = False
249for subdir in subdirs:
250 if not subdir in SUBDIR_MAPPING.keys():
251 raise Exception(('unrecognized platform subdir "%s"; ' +
252 'should be one of %s') % (
253 subdir, SUBDIR_MAPPING.keys()))
254 builder = SUBDIR_MAPPING[subdir]
255
256 # We instantiate different Rebaseliner objects depending
257 # on whether we are rebaselining an expected-results.json file, or
258 # individual image files. Different gm-expected subdirectories may move
259 # from individual image files to JSON-format expectations at different
260 # times, so we need to make this determination per subdirectory.
261 #
262 # See https://goto.google.com/ChecksumTransitionDetail
263 expectations_json_file = os.path.join(args.expectations_root, subdir,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000264 args.expectations_filename)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000265 if os.path.isfile(expectations_json_file):
epoger@google.com99a8ec92013-06-19 18:56:59 +0000266 rebaseliner = JsonRebaseliner(
267 expectations_root=args.expectations_root,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000268 expectations_filename=args.expectations_filename,
epoger@google.com99a8ec92013-06-19 18:56:59 +0000269 tests=args.tests, configs=args.configs,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000270 actuals_base_url=args.actuals_base_url,
271 actuals_filename=args.actuals_filename,
272 add_new=args.add_new)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000273 else:
epoger@google.com89fa4b92013-07-10 16:54:10 +0000274 # TODO(epoger): When we get rid of the ImageRebaseliner implementation,
275 # we should raise an Exception in this case (no JSON expectations file
276 # found to update), to prevent a recurrence of
277 # https://code.google.com/p/skia/issues/detail?id=1403 ('rebaseline.py
278 # script fails with misleading output when run outside of gm-expected
279 # dir')
epoger@google.com99a8ec92013-06-19 18:56:59 +0000280 rebaseliner = rebaseline_imagefiles.ImageRebaseliner(
281 expectations_root=args.expectations_root,
282 tests=args.tests, configs=args.configs,
283 dry_run=args.dry_run,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000284 json_base_url=args.actuals_base_url,
285 json_filename=args.actuals_filename,
epoger@google.com99a8ec92013-06-19 18:56:59 +0000286 add_new=args.add_new,
287 missing_json_is_fatal=missing_json_is_fatal)
288 rebaseliner.RebaselineSubdir(subdir=subdir, builder=builder)