blob: 5e83b8378dc9c2a80d289fc4121dd0d41660ca5c [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.
epoger@google.com61822a22013-07-16 18:56:32 +000012
13TODO(epoger): Fix indentation in this file (2-space indents, not 4-space).
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.com27e1c002013-07-24 15:38:39 +000020import subprocess
epoger@google.comec3397b2013-05-29 17:09:43 +000021import sys
epoger@google.com99ba65a2013-06-05 15:43:37 +000022import urllib2
23
epoger@google.com99a8ec92013-06-19 18:56:59 +000024# Imports from local directory
25import rebaseline_imagefiles
26
epoger@google.com99ba65a2013-06-05 15:43:37 +000027# Imports from within Skia
28#
epoger@google.comdad53102013-06-12 14:25:30 +000029# We need to add the 'gm' directory, so that we can import gm_json.py within
30# that directory. That script allows us to parse the actual-results.json file
31# written out by the GM tool.
32# Make sure that the 'gm' dir is in the PYTHONPATH, but add it at the *end*
33# so any dirs that are already in the PYTHONPATH will be preferred.
34#
35# This assumes that the 'gm' directory has been checked out as a sibling of
36# the 'tools' directory containing this script, which will be the case if
37# 'trunk' was checked out as a single unit.
epoger@google.com99ba65a2013-06-05 15:43:37 +000038GM_DIRECTORY = os.path.realpath(
39 os.path.join(os.path.dirname(os.path.dirname(__file__)), 'gm'))
40if GM_DIRECTORY not in sys.path:
41 sys.path.append(GM_DIRECTORY)
42import gm_json
43
epoger@google.come94a7d22013-07-23 19:37:03 +000044# Mapping of expectations/gm subdir (under
45# https://skia.googlecode.com/svn/trunk/expectations/gm/ )
epoger@google.comec3397b2013-05-29 17:09:43 +000046# to builder name (see list at http://108.170.217.252:10117/builders )
epoger@google.com9166bf52013-05-30 15:46:19 +000047SUBDIR_MAPPING = {
epoger@google.comec3397b2013-05-29 17:09:43 +000048 'base-shuttle-win7-intel-float':
49 'Test-Win7-ShuttleA-HD2000-x86-Release',
50 'base-shuttle-win7-intel-angle':
51 'Test-Win7-ShuttleA-HD2000-x86-Release-ANGLE',
52 'base-shuttle-win7-intel-directwrite':
53 'Test-Win7-ShuttleA-HD2000-x86-Release-DirectWrite',
54 'base-shuttle_ubuntu12_ati5770':
55 'Test-Ubuntu12-ShuttleA-ATI5770-x86_64-Release',
56 'base-macmini':
57 'Test-Mac10.6-MacMini4.1-GeForce320M-x86-Release',
58 'base-macmini-lion-float':
59 'Test-Mac10.7-MacMini4.1-GeForce320M-x86-Release',
60 'base-android-galaxy-nexus':
61 'Test-Android-GalaxyNexus-SGX540-Arm7-Debug',
62 'base-android-nexus-7':
63 'Test-Android-Nexus7-Tegra3-Arm7-Release',
64 'base-android-nexus-s':
65 'Test-Android-NexusS-SGX540-Arm7-Release',
66 'base-android-xoom':
67 'Test-Android-Xoom-Tegra2-Arm7-Release',
68 'base-android-nexus-10':
69 'Test-Android-Nexus10-MaliT604-Arm7-Release',
robertphillips@google.com63e96272013-07-02 12:54:37 +000070 'base-android-nexus-4':
71 'Test-Android-Nexus4-Adreno320-Arm7-Release',
epoger@google.comec3397b2013-05-29 17:09:43 +000072}
73
epoger@google.com9166bf52013-05-30 15:46:19 +000074
epoger@google.com66ba9f92013-07-11 19:20:30 +000075class _InternalException(Exception):
epoger@google.comdb29a312013-06-04 14:58:47 +000076 pass
77
epoger@google.comffcbdbf2013-07-16 17:35:39 +000078# Object that handles exceptions, either raising them immediately or collecting
79# them to display later on.
80class ExceptionHandler(object):
81
82 # params:
83 # keep_going_on_failure: if False, report failures and quit right away;
84 # if True, collect failures until
85 # ReportAllFailures() is called
86 def __init__(self, keep_going_on_failure=False):
87 self._keep_going_on_failure = keep_going_on_failure
88 self._failures_encountered = []
89 self._exiting = False
90
91 # Exit the program with the given status value.
92 def _Exit(self, status=1):
93 self._exiting = True
94 sys.exit(status)
95
96 # We have encountered an exception; either collect the info and keep going,
97 # or exit the program right away.
98 def RaiseExceptionOrContinue(self, e):
99 # If we are already quitting the program, propagate any exceptions
100 # so that the proper exit status will be communicated to the shell.
101 if self._exiting:
102 raise e
103
104 if self._keep_going_on_failure:
105 print >> sys.stderr, 'WARNING: swallowing exception %s' % e
106 self._failures_encountered.append(e)
107 else:
108 print >> sys.stderr, e
109 print >> sys.stderr, (
110 'Halting at first exception; to keep going, re-run ' +
111 'with the --keep-going-on-failure option set.')
112 self._Exit()
113
114 def ReportAllFailures(self):
115 if self._failures_encountered:
116 print >> sys.stderr, ('Encountered %d failures (see above).' %
117 len(self._failures_encountered))
118 self._Exit()
119
120
epoger@google.com99a8ec92013-06-19 18:56:59 +0000121# Object that rebaselines a JSON expectations file (not individual image files).
epoger@google.com99a8ec92013-06-19 18:56:59 +0000122class JsonRebaseliner(object):
epoger@google.com9166bf52013-05-30 15:46:19 +0000123
124 # params:
epoger@google.coma783f2b2013-07-08 17:51:58 +0000125 # expectations_root: root directory of all expectations JSON files
126 # expectations_filename: filename (under expectations_root) of JSON
127 # expectations file; typically
128 # "expected-results.json"
129 # actuals_base_url: base URL from which to read actual-result JSON files
130 # actuals_filename: filename (under actuals_base_url) from which to read a
131 # summary of results; typically "actual-results.json"
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000132 # exception_handler: reference to rebaseline.ExceptionHandler object
epoger@google.com99ba65a2013-06-05 15:43:37 +0000133 # tests: list of tests to rebaseline, or None if we should rebaseline
134 # whatever files the JSON results summary file tells us to
epoger@google.com9de25e32013-07-10 15:27:18 +0000135 # configs: which configs to run for each test, or None if we should
136 # rebaseline whatever configs the JSON results summary file tells
137 # us to
epoger@google.comdad53102013-06-12 14:25:30 +0000138 # add_new: if True, add expectations for tests which don't have any yet
epoger@google.coma783f2b2013-07-08 17:51:58 +0000139 def __init__(self, expectations_root, expectations_filename,
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000140 actuals_base_url, actuals_filename, exception_handler,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000141 tests=None, configs=None, add_new=False):
epoger@google.com99a8ec92013-06-19 18:56:59 +0000142 self._expectations_root = expectations_root
epoger@google.coma783f2b2013-07-08 17:51:58 +0000143 self._expectations_filename = expectations_filename
epoger@google.com9166bf52013-05-30 15:46:19 +0000144 self._tests = tests
145 self._configs = configs
epoger@google.coma783f2b2013-07-08 17:51:58 +0000146 self._actuals_base_url = actuals_base_url
147 self._actuals_filename = actuals_filename
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000148 self._exception_handler = exception_handler
epoger@google.comdad53102013-06-12 14:25:30 +0000149 self._add_new = add_new
epoger@google.com61822a22013-07-16 18:56:32 +0000150 self._image_filename_re = re.compile(gm_json.IMAGE_FILENAME_PATTERN)
epoger@google.com27e1c002013-07-24 15:38:39 +0000151 self._using_svn = os.path.isdir(os.path.join(expectations_root, '.svn'))
152
153 # Executes subprocess.call(cmd).
154 # Raises an Exception if the command fails.
155 def _Call(self, cmd):
156 if subprocess.call(cmd) != 0:
157 raise _InternalException('error running command: ' + ' '.join(cmd))
epoger@google.com9166bf52013-05-30 15:46:19 +0000158
epoger@google.coma783f2b2013-07-08 17:51:58 +0000159 # Returns the full contents of filepath, as a single string.
160 # If filepath looks like a URL, try to read it that way instead of as
161 # a path on local storage.
epoger@google.com66ba9f92013-07-11 19:20:30 +0000162 #
163 # Raises _InternalException if there is a problem.
epoger@google.coma783f2b2013-07-08 17:51:58 +0000164 def _GetFileContents(self, filepath):
165 if filepath.startswith('http:') or filepath.startswith('https:'):
epoger@google.com66ba9f92013-07-11 19:20:30 +0000166 try:
167 return urllib2.urlopen(filepath).read()
168 except urllib2.HTTPError as e:
169 raise _InternalException('unable to read URL %s: %s' % (
170 filepath, e))
epoger@google.com99ba65a2013-06-05 15:43:37 +0000171 else:
epoger@google.coma783f2b2013-07-08 17:51:58 +0000172 return open(filepath, 'r').read()
epoger@google.com99ba65a2013-06-05 15:43:37 +0000173
epoger@google.come78d2072013-06-12 17:44:14 +0000174 # Returns a dictionary of actual results from actual-results.json file.
175 #
176 # The dictionary returned has this format:
177 # {
178 # u'imageblur_565.png': [u'bitmap-64bitMD5', 3359963596899141322],
179 # u'imageblur_8888.png': [u'bitmap-64bitMD5', 4217923806027861152],
180 # u'shadertext3_8888.png': [u'bitmap-64bitMD5', 3713708307125704716]
181 # }
182 #
epoger@google.com66ba9f92013-07-11 19:20:30 +0000183 # If the JSON actual result summary file cannot be loaded, logs a warning
184 # message and returns None.
185 # If the JSON actual result summary file can be loaded, but we have
186 # trouble parsing it, raises an Exception.
epoger@google.come78d2072013-06-12 17:44:14 +0000187 #
188 # params:
189 # json_url: URL pointing to a JSON actual result summary file
190 # sections: a list of section names to include in the results, e.g.
191 # [gm_json.JSONKEY_ACTUALRESULTS_FAILED,
192 # gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON] ;
193 # if None, then include ALL sections.
194 def _GetActualResults(self, json_url, sections=None):
epoger@google.com66ba9f92013-07-11 19:20:30 +0000195 try:
196 json_contents = self._GetFileContents(json_url)
197 except _InternalException:
198 print >> sys.stderr, (
199 'could not read json_url %s ; skipping this platform.' %
200 json_url)
201 return None
epoger@google.come78d2072013-06-12 17:44:14 +0000202 json_dict = gm_json.LoadFromString(json_contents)
203 results_to_return = {}
204 actual_results = json_dict[gm_json.JSONKEY_ACTUALRESULTS]
205 if not sections:
206 sections = actual_results.keys()
207 for section in sections:
208 section_results = actual_results[section]
209 if section_results:
210 results_to_return.update(section_results)
211 return results_to_return
212
epoger@google.com99a8ec92013-06-19 18:56:59 +0000213 # Rebaseline all tests/types we specified in the constructor,
epoger@google.come94a7d22013-07-23 19:37:03 +0000214 # within this expectations/gm subdir.
epoger@google.com99a8ec92013-06-19 18:56:59 +0000215 #
216 # params:
217 # subdir : e.g. 'base-shuttle-win7-intel-float'
218 # builder : e.g. 'Test-Win7-ShuttleA-HD2000-x86-Release'
219 def RebaselineSubdir(self, subdir, builder):
epoger@google.coma783f2b2013-07-08 17:51:58 +0000220 # Read in the actual result summary, and extract all the tests whose
221 # results we need to update.
222 actuals_url = '/'.join([self._actuals_base_url,
223 subdir, builder, subdir,
224 self._actuals_filename])
epoger@google.comb248dd52013-07-17 00:09:10 +0000225 # In most cases, we won't need to re-record results that are already
226 # succeeding, but including the SUCCEEDED results will allow us to
227 # re-record expectations if they somehow get out of sync.
228 sections = [gm_json.JSONKEY_ACTUALRESULTS_FAILED,
229 gm_json.JSONKEY_ACTUALRESULTS_SUCCEEDED]
epoger@google.coma783f2b2013-07-08 17:51:58 +0000230 if self._add_new:
231 sections.append(gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON)
232 results_to_update = self._GetActualResults(json_url=actuals_url,
233 sections=sections)
epoger@google.come78d2072013-06-12 17:44:14 +0000234
epoger@google.coma783f2b2013-07-08 17:51:58 +0000235 # Read in current expectations.
236 expectations_json_filepath = os.path.join(
237 self._expectations_root, subdir, self._expectations_filename)
238 expectations_dict = gm_json.LoadFromFile(expectations_json_filepath)
epoger@google.com4b383012013-07-16 21:10:54 +0000239 expected_results = expectations_dict[gm_json.JSONKEY_EXPECTEDRESULTS]
epoger@google.coma783f2b2013-07-08 17:51:58 +0000240
241 # Update the expectations in memory, skipping any tests/configs that
242 # the caller asked to exclude.
243 skipped_images = []
244 if results_to_update:
245 for (image_name, image_results) in results_to_update.iteritems():
epoger@google.com61822a22013-07-16 18:56:32 +0000246 (test, config) = \
247 self._image_filename_re.match(image_name).groups()
epoger@google.coma783f2b2013-07-08 17:51:58 +0000248 if self._tests:
249 if test not in self._tests:
250 skipped_images.append(image_name)
251 continue
252 if self._configs:
253 if config not in self._configs:
254 skipped_images.append(image_name)
255 continue
epoger@google.com4b383012013-07-16 21:10:54 +0000256 if not expected_results.get(image_name):
257 expected_results[image_name] = {}
258 expected_results[image_name] \
epoger@google.com61822a22013-07-16 18:56:32 +0000259 [gm_json.JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS] = \
260 [image_results]
epoger@google.coma783f2b2013-07-08 17:51:58 +0000261
262 # Write out updated expectations.
263 gm_json.WriteToFile(expectations_dict, expectations_json_filepath)
264
epoger@google.com27e1c002013-07-24 15:38:39 +0000265 # Mark the JSON file as plaintext, so text-style diffs can be applied.
266 # Fixes https://code.google.com/p/skia/issues/detail?id=1442
267 if self._using_svn:
268 self._Call(['svn', 'propset', '--quiet', 'svn:mime-type',
269 'text/x-json', expectations_json_filepath])
epoger@google.comec3397b2013-05-29 17:09:43 +0000270
epoger@google.com9166bf52013-05-30 15:46:19 +0000271# main...
epoger@google.comec3397b2013-05-29 17:09:43 +0000272
epoger@google.com9166bf52013-05-30 15:46:19 +0000273parser = argparse.ArgumentParser()
epoger@google.coma783f2b2013-07-08 17:51:58 +0000274parser.add_argument('--actuals-base-url',
275 help='base URL from which to read files containing JSON ' +
276 'summaries of actual GM results; defaults to %(default)s',
277 default='http://skia-autogen.googlecode.com/svn/gm-actual')
278parser.add_argument('--actuals-filename',
279 help='filename (within platform-specific subdirectories ' +
280 'of ACTUALS_BASE_URL) to read a summary of results from; ' +
281 'defaults to %(default)s',
282 default='actual-results.json')
283# TODO(epoger): Add test that exercises --add-new argument.
epoger@google.comdad53102013-06-12 14:25:30 +0000284parser.add_argument('--add-new', action='store_true',
285 help='in addition to the standard behavior of ' +
286 'updating expectations for failing tests, add ' +
287 'expectations for tests which don\'t have expectations ' +
288 'yet.')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000289# TODO(epoger): Add test that exercises --configs argument.
epoger@google.com9166bf52013-05-30 15:46:19 +0000290parser.add_argument('--configs', metavar='CONFIG', nargs='+',
291 help='which configurations to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000292 '"--configs 565 8888", as a filter over the full set of ' +
293 'results in ACTUALS_FILENAME; if unspecified, rebaseline ' +
294 '*all* configs that are available.')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000295# TODO(epoger): The --dry-run argument will no longer be needed once we
296# are only rebaselining JSON files.
epoger@google.com82f31782013-06-11 15:45:46 +0000297parser.add_argument('--dry-run', action='store_true',
epoger@google.com9166bf52013-05-30 15:46:19 +0000298 help='instead of actually downloading files or adding ' +
299 'files to checkout, display a list of operations that ' +
300 'we would normally perform')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000301parser.add_argument('--expectations-filename',
302 help='filename (under EXPECTATIONS_ROOT) to read ' +
303 'current expectations from, and to write new ' +
304 'expectations into; defaults to %(default)s',
305 default='expected-results.json')
epoger@google.com99a8ec92013-06-19 18:56:59 +0000306parser.add_argument('--expectations-root',
307 help='root of expectations directory to update-- should ' +
308 'contain one or more base-* subdirectories. Defaults to ' +
309 '%(default)s',
epoger@google.come94a7d22013-07-23 19:37:03 +0000310 default=os.path.join('expectations', 'gm'))
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000311parser.add_argument('--keep-going-on-failure', action='store_true',
312 help='instead of halting at the first error encountered, ' +
313 'keep going and rebaseline as many tests as possible, ' +
314 'and then report the full set of errors at the end')
epoger@google.com9166bf52013-05-30 15:46:19 +0000315parser.add_argument('--subdirs', metavar='SUBDIR', nargs='+',
316 help='which platform subdirectories to rebaseline; ' +
317 'if unspecified, rebaseline all subdirs, same as ' +
318 '"--subdirs %s"' % ' '.join(sorted(SUBDIR_MAPPING.keys())))
epoger@google.coma783f2b2013-07-08 17:51:58 +0000319# TODO(epoger): Add test that exercises --tests argument.
epoger@google.com99ba65a2013-06-05 15:43:37 +0000320parser.add_argument('--tests', metavar='TEST', nargs='+',
epoger@google.com9166bf52013-05-30 15:46:19 +0000321 help='which tests to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000322 '"--tests aaclip bigmatrix", as a filter over the full ' +
323 'set of results in ACTUALS_FILENAME; if unspecified, ' +
324 'rebaseline *all* tests that are available.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000325args = parser.parse_args()
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000326exception_handler = ExceptionHandler(
327 keep_going_on_failure=args.keep_going_on_failure)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000328if args.subdirs:
329 subdirs = args.subdirs
330 missing_json_is_fatal = True
331else:
332 subdirs = sorted(SUBDIR_MAPPING.keys())
333 missing_json_is_fatal = False
334for subdir in subdirs:
335 if not subdir in SUBDIR_MAPPING.keys():
336 raise Exception(('unrecognized platform subdir "%s"; ' +
337 'should be one of %s') % (
338 subdir, SUBDIR_MAPPING.keys()))
339 builder = SUBDIR_MAPPING[subdir]
340
341 # We instantiate different Rebaseliner objects depending
342 # on whether we are rebaselining an expected-results.json file, or
epoger@google.come94a7d22013-07-23 19:37:03 +0000343 # individual image files. Different expectations/gm subdirectories may move
epoger@google.com99a8ec92013-06-19 18:56:59 +0000344 # from individual image files to JSON-format expectations at different
345 # times, so we need to make this determination per subdirectory.
346 #
347 # See https://goto.google.com/ChecksumTransitionDetail
348 expectations_json_file = os.path.join(args.expectations_root, subdir,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000349 args.expectations_filename)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000350 if os.path.isfile(expectations_json_file):
epoger@google.com99a8ec92013-06-19 18:56:59 +0000351 rebaseliner = JsonRebaseliner(
352 expectations_root=args.expectations_root,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000353 expectations_filename=args.expectations_filename,
epoger@google.com99a8ec92013-06-19 18:56:59 +0000354 tests=args.tests, configs=args.configs,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000355 actuals_base_url=args.actuals_base_url,
356 actuals_filename=args.actuals_filename,
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000357 exception_handler=exception_handler,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000358 add_new=args.add_new)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000359 else:
epoger@google.com89fa4b92013-07-10 16:54:10 +0000360 # TODO(epoger): When we get rid of the ImageRebaseliner implementation,
361 # we should raise an Exception in this case (no JSON expectations file
362 # found to update), to prevent a recurrence of
363 # https://code.google.com/p/skia/issues/detail?id=1403 ('rebaseline.py
364 # script fails with misleading output when run outside of gm-expected
365 # dir')
epoger@google.com99a8ec92013-06-19 18:56:59 +0000366 rebaseliner = rebaseline_imagefiles.ImageRebaseliner(
367 expectations_root=args.expectations_root,
368 tests=args.tests, configs=args.configs,
369 dry_run=args.dry_run,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000370 json_base_url=args.actuals_base_url,
371 json_filename=args.actuals_filename,
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000372 exception_handler=exception_handler,
epoger@google.com99a8ec92013-06-19 18:56:59 +0000373 add_new=args.add_new,
374 missing_json_is_fatal=missing_json_is_fatal)
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000375
epoger@google.com3e7399f2013-07-10 17:23:47 +0000376 try:
377 rebaseliner.RebaselineSubdir(subdir=subdir, builder=builder)
378 except BaseException as e:
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000379 exception_handler.RaiseExceptionOrContinue(e)
380
381exception_handler.ReportAllFailures()