blob: ec5c1c99c220f30ca582018f474d38faf8cd27a2 [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.com66ba9f92013-07-11 19:20:30 +000074class _InternalException(Exception):
epoger@google.comdb29a312013-06-04 14:58:47 +000075 pass
76
epoger@google.comffcbdbf2013-07-16 17:35:39 +000077# Object that handles exceptions, either raising them immediately or collecting
78# them to display later on.
79class ExceptionHandler(object):
80
81 # params:
82 # keep_going_on_failure: if False, report failures and quit right away;
83 # if True, collect failures until
84 # ReportAllFailures() is called
85 def __init__(self, keep_going_on_failure=False):
86 self._keep_going_on_failure = keep_going_on_failure
87 self._failures_encountered = []
88 self._exiting = False
89
90 # Exit the program with the given status value.
91 def _Exit(self, status=1):
92 self._exiting = True
93 sys.exit(status)
94
95 # We have encountered an exception; either collect the info and keep going,
96 # or exit the program right away.
97 def RaiseExceptionOrContinue(self, e):
98 # If we are already quitting the program, propagate any exceptions
99 # so that the proper exit status will be communicated to the shell.
100 if self._exiting:
101 raise e
102
103 if self._keep_going_on_failure:
104 print >> sys.stderr, 'WARNING: swallowing exception %s' % e
105 self._failures_encountered.append(e)
106 else:
107 print >> sys.stderr, e
108 print >> sys.stderr, (
109 'Halting at first exception; to keep going, re-run ' +
110 'with the --keep-going-on-failure option set.')
111 self._Exit()
112
113 def ReportAllFailures(self):
114 if self._failures_encountered:
115 print >> sys.stderr, ('Encountered %d failures (see above).' %
116 len(self._failures_encountered))
117 self._Exit()
118
119
epoger@google.com99a8ec92013-06-19 18:56:59 +0000120# Object that rebaselines a JSON expectations file (not individual image files).
epoger@google.com99a8ec92013-06-19 18:56:59 +0000121class JsonRebaseliner(object):
epoger@google.com9166bf52013-05-30 15:46:19 +0000122
123 # params:
epoger@google.coma783f2b2013-07-08 17:51:58 +0000124 # expectations_root: root directory of all expectations JSON files
125 # expectations_filename: filename (under expectations_root) of JSON
126 # expectations file; typically
127 # "expected-results.json"
128 # actuals_base_url: base URL from which to read actual-result JSON files
129 # actuals_filename: filename (under actuals_base_url) from which to read a
130 # summary of results; typically "actual-results.json"
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000131 # exception_handler: reference to rebaseline.ExceptionHandler object
epoger@google.com99ba65a2013-06-05 15:43:37 +0000132 # tests: list of tests to rebaseline, or None if we should rebaseline
133 # whatever files the JSON results summary file tells us to
epoger@google.com9de25e32013-07-10 15:27:18 +0000134 # configs: which configs to run for each test, or None if we should
135 # rebaseline whatever configs the JSON results summary file tells
136 # us to
epoger@google.comdad53102013-06-12 14:25:30 +0000137 # add_new: if True, add expectations for tests which don't have any yet
epoger@google.coma783f2b2013-07-08 17:51:58 +0000138 def __init__(self, expectations_root, expectations_filename,
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000139 actuals_base_url, actuals_filename, exception_handler,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000140 tests=None, configs=None, add_new=False):
epoger@google.com99a8ec92013-06-19 18:56:59 +0000141 self._expectations_root = expectations_root
epoger@google.coma783f2b2013-07-08 17:51:58 +0000142 self._expectations_filename = expectations_filename
epoger@google.com9166bf52013-05-30 15:46:19 +0000143 self._tests = tests
144 self._configs = configs
epoger@google.coma783f2b2013-07-08 17:51:58 +0000145 self._actuals_base_url = actuals_base_url
146 self._actuals_filename = actuals_filename
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000147 self._exception_handler = exception_handler
epoger@google.comdad53102013-06-12 14:25:30 +0000148 self._add_new = add_new
epoger@google.come78d2072013-06-12 17:44:14 +0000149 self._testname_pattern = re.compile('(\S+)_(\S+).png')
epoger@google.com9166bf52013-05-30 15:46:19 +0000150
epoger@google.coma783f2b2013-07-08 17:51:58 +0000151 # Returns the full contents of filepath, as a single string.
152 # If filepath looks like a URL, try to read it that way instead of as
153 # a path on local storage.
epoger@google.com66ba9f92013-07-11 19:20:30 +0000154 #
155 # Raises _InternalException if there is a problem.
epoger@google.coma783f2b2013-07-08 17:51:58 +0000156 def _GetFileContents(self, filepath):
157 if filepath.startswith('http:') or filepath.startswith('https:'):
epoger@google.com66ba9f92013-07-11 19:20:30 +0000158 try:
159 return urllib2.urlopen(filepath).read()
160 except urllib2.HTTPError as e:
161 raise _InternalException('unable to read URL %s: %s' % (
162 filepath, e))
epoger@google.com99ba65a2013-06-05 15:43:37 +0000163 else:
epoger@google.coma783f2b2013-07-08 17:51:58 +0000164 return open(filepath, 'r').read()
epoger@google.com99ba65a2013-06-05 15:43:37 +0000165
epoger@google.come78d2072013-06-12 17:44:14 +0000166 # Returns a dictionary of actual results from actual-results.json file.
167 #
168 # The dictionary returned has this format:
169 # {
170 # u'imageblur_565.png': [u'bitmap-64bitMD5', 3359963596899141322],
171 # u'imageblur_8888.png': [u'bitmap-64bitMD5', 4217923806027861152],
172 # u'shadertext3_8888.png': [u'bitmap-64bitMD5', 3713708307125704716]
173 # }
174 #
epoger@google.com66ba9f92013-07-11 19:20:30 +0000175 # If the JSON actual result summary file cannot be loaded, logs a warning
176 # message and returns None.
177 # If the JSON actual result summary file can be loaded, but we have
178 # trouble parsing it, raises an Exception.
epoger@google.come78d2072013-06-12 17:44:14 +0000179 #
180 # params:
181 # json_url: URL pointing to a JSON actual result summary file
182 # sections: a list of section names to include in the results, e.g.
183 # [gm_json.JSONKEY_ACTUALRESULTS_FAILED,
184 # gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON] ;
185 # if None, then include ALL sections.
186 def _GetActualResults(self, json_url, sections=None):
epoger@google.com66ba9f92013-07-11 19:20:30 +0000187 try:
188 json_contents = self._GetFileContents(json_url)
189 except _InternalException:
190 print >> sys.stderr, (
191 'could not read json_url %s ; skipping this platform.' %
192 json_url)
193 return None
epoger@google.come78d2072013-06-12 17:44:14 +0000194 json_dict = gm_json.LoadFromString(json_contents)
195 results_to_return = {}
196 actual_results = json_dict[gm_json.JSONKEY_ACTUALRESULTS]
197 if not sections:
198 sections = actual_results.keys()
199 for section in sections:
200 section_results = actual_results[section]
201 if section_results:
202 results_to_return.update(section_results)
203 return results_to_return
204
epoger@google.com99a8ec92013-06-19 18:56:59 +0000205 # Rebaseline all tests/types we specified in the constructor,
206 # within this gm-expectations subdir.
207 #
208 # params:
209 # subdir : e.g. 'base-shuttle-win7-intel-float'
210 # builder : e.g. 'Test-Win7-ShuttleA-HD2000-x86-Release'
211 def RebaselineSubdir(self, subdir, builder):
epoger@google.coma783f2b2013-07-08 17:51:58 +0000212 # Read in the actual result summary, and extract all the tests whose
213 # results we need to update.
214 actuals_url = '/'.join([self._actuals_base_url,
215 subdir, builder, subdir,
216 self._actuals_filename])
217 sections = [gm_json.JSONKEY_ACTUALRESULTS_FAILED]
218 if self._add_new:
219 sections.append(gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON)
220 results_to_update = self._GetActualResults(json_url=actuals_url,
221 sections=sections)
epoger@google.come78d2072013-06-12 17:44:14 +0000222
epoger@google.coma783f2b2013-07-08 17:51:58 +0000223 # Read in current expectations.
224 expectations_json_filepath = os.path.join(
225 self._expectations_root, subdir, self._expectations_filename)
226 expectations_dict = gm_json.LoadFromFile(expectations_json_filepath)
227
228 # Update the expectations in memory, skipping any tests/configs that
229 # the caller asked to exclude.
230 skipped_images = []
231 if results_to_update:
232 for (image_name, image_results) in results_to_update.iteritems():
233 (test, config) = self._testname_pattern.match(image_name).groups()
234 if self._tests:
235 if test not in self._tests:
236 skipped_images.append(image_name)
237 continue
238 if self._configs:
239 if config not in self._configs:
240 skipped_images.append(image_name)
241 continue
242 expectations_dict[gm_json.JSONKEY_EXPECTEDRESULTS] \
243 [image_name] \
244 [gm_json.JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS] = \
245 [image_results]
246
247 # Write out updated expectations.
248 gm_json.WriteToFile(expectations_dict, expectations_json_filepath)
249
epoger@google.comec3397b2013-05-29 17:09:43 +0000250
epoger@google.com9166bf52013-05-30 15:46:19 +0000251# main...
epoger@google.comec3397b2013-05-29 17:09:43 +0000252
epoger@google.com9166bf52013-05-30 15:46:19 +0000253parser = argparse.ArgumentParser()
epoger@google.coma783f2b2013-07-08 17:51:58 +0000254parser.add_argument('--actuals-base-url',
255 help='base URL from which to read files containing JSON ' +
256 'summaries of actual GM results; defaults to %(default)s',
257 default='http://skia-autogen.googlecode.com/svn/gm-actual')
258parser.add_argument('--actuals-filename',
259 help='filename (within platform-specific subdirectories ' +
260 'of ACTUALS_BASE_URL) to read a summary of results from; ' +
261 'defaults to %(default)s',
262 default='actual-results.json')
263# TODO(epoger): Add test that exercises --add-new argument.
epoger@google.comdad53102013-06-12 14:25:30 +0000264parser.add_argument('--add-new', action='store_true',
265 help='in addition to the standard behavior of ' +
266 'updating expectations for failing tests, add ' +
267 'expectations for tests which don\'t have expectations ' +
268 'yet.')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000269# TODO(epoger): Add test that exercises --configs argument.
epoger@google.com9166bf52013-05-30 15:46:19 +0000270parser.add_argument('--configs', metavar='CONFIG', nargs='+',
271 help='which configurations to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000272 '"--configs 565 8888", as a filter over the full set of ' +
273 'results in ACTUALS_FILENAME; if unspecified, rebaseline ' +
274 '*all* configs that are available.')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000275# TODO(epoger): The --dry-run argument will no longer be needed once we
276# are only rebaselining JSON files.
epoger@google.com82f31782013-06-11 15:45:46 +0000277parser.add_argument('--dry-run', action='store_true',
epoger@google.com9166bf52013-05-30 15:46:19 +0000278 help='instead of actually downloading files or adding ' +
279 'files to checkout, display a list of operations that ' +
280 'we would normally perform')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000281parser.add_argument('--expectations-filename',
282 help='filename (under EXPECTATIONS_ROOT) to read ' +
283 'current expectations from, and to write new ' +
284 'expectations into; defaults to %(default)s',
285 default='expected-results.json')
epoger@google.com99a8ec92013-06-19 18:56:59 +0000286parser.add_argument('--expectations-root',
287 help='root of expectations directory to update-- should ' +
288 'contain one or more base-* subdirectories. Defaults to ' +
289 '%(default)s',
290 default='.')
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000291parser.add_argument('--keep-going-on-failure', action='store_true',
292 help='instead of halting at the first error encountered, ' +
293 'keep going and rebaseline as many tests as possible, ' +
294 'and then report the full set of errors at the end')
epoger@google.com9166bf52013-05-30 15:46:19 +0000295parser.add_argument('--subdirs', metavar='SUBDIR', nargs='+',
296 help='which platform subdirectories to rebaseline; ' +
297 'if unspecified, rebaseline all subdirs, same as ' +
298 '"--subdirs %s"' % ' '.join(sorted(SUBDIR_MAPPING.keys())))
epoger@google.coma783f2b2013-07-08 17:51:58 +0000299# TODO(epoger): Add test that exercises --tests argument.
epoger@google.com99ba65a2013-06-05 15:43:37 +0000300parser.add_argument('--tests', metavar='TEST', nargs='+',
epoger@google.com9166bf52013-05-30 15:46:19 +0000301 help='which tests to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000302 '"--tests aaclip bigmatrix", as a filter over the full ' +
303 'set of results in ACTUALS_FILENAME; if unspecified, ' +
304 'rebaseline *all* tests that are available.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000305args = parser.parse_args()
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000306exception_handler = ExceptionHandler(
307 keep_going_on_failure=args.keep_going_on_failure)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000308if args.subdirs:
309 subdirs = args.subdirs
310 missing_json_is_fatal = True
311else:
312 subdirs = sorted(SUBDIR_MAPPING.keys())
313 missing_json_is_fatal = False
314for subdir in subdirs:
315 if not subdir in SUBDIR_MAPPING.keys():
316 raise Exception(('unrecognized platform subdir "%s"; ' +
317 'should be one of %s') % (
318 subdir, SUBDIR_MAPPING.keys()))
319 builder = SUBDIR_MAPPING[subdir]
320
321 # We instantiate different Rebaseliner objects depending
322 # on whether we are rebaselining an expected-results.json file, or
323 # individual image files. Different gm-expected subdirectories may move
324 # from individual image files to JSON-format expectations at different
325 # times, so we need to make this determination per subdirectory.
326 #
327 # See https://goto.google.com/ChecksumTransitionDetail
328 expectations_json_file = os.path.join(args.expectations_root, subdir,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000329 args.expectations_filename)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000330 if os.path.isfile(expectations_json_file):
epoger@google.com99a8ec92013-06-19 18:56:59 +0000331 rebaseliner = JsonRebaseliner(
332 expectations_root=args.expectations_root,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000333 expectations_filename=args.expectations_filename,
epoger@google.com99a8ec92013-06-19 18:56:59 +0000334 tests=args.tests, configs=args.configs,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000335 actuals_base_url=args.actuals_base_url,
336 actuals_filename=args.actuals_filename,
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000337 exception_handler=exception_handler,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000338 add_new=args.add_new)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000339 else:
epoger@google.com89fa4b92013-07-10 16:54:10 +0000340 # TODO(epoger): When we get rid of the ImageRebaseliner implementation,
341 # we should raise an Exception in this case (no JSON expectations file
342 # found to update), to prevent a recurrence of
343 # https://code.google.com/p/skia/issues/detail?id=1403 ('rebaseline.py
344 # script fails with misleading output when run outside of gm-expected
345 # dir')
epoger@google.com99a8ec92013-06-19 18:56:59 +0000346 rebaseliner = rebaseline_imagefiles.ImageRebaseliner(
347 expectations_root=args.expectations_root,
348 tests=args.tests, configs=args.configs,
349 dry_run=args.dry_run,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000350 json_base_url=args.actuals_base_url,
351 json_filename=args.actuals_filename,
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000352 exception_handler=exception_handler,
epoger@google.com99a8ec92013-06-19 18:56:59 +0000353 add_new=args.add_new,
354 missing_json_is_fatal=missing_json_is_fatal)
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000355
epoger@google.com3e7399f2013-07-10 17:23:47 +0000356 try:
357 rebaseliner.RebaselineSubdir(subdir=subdir, builder=builder)
358 except BaseException as e:
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000359 exception_handler.RaiseExceptionOrContinue(e)
360
361exception_handler.ReportAllFailures()