blob: 51292819115229a83fef4239ad9d9868d1048b0f [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.
senorblanco@chromium.org782f3b42012-10-29 18:06:26 +000012'''
13
epoger@google.com99ba65a2013-06-05 15:43:37 +000014# System-level imports
epoger@google.com9166bf52013-05-30 15:46:19 +000015import argparse
epoger@google.comfd040112013-08-20 16:21:55 +000016import json
epoger@google.comec3397b2013-05-29 17:09:43 +000017import os
epoger@google.come78d2072013-06-12 17:44:14 +000018import re
epoger@google.com27e1c002013-07-24 15:38:39 +000019import subprocess
epoger@google.comec3397b2013-05-29 17:09:43 +000020import sys
epoger@google.com99ba65a2013-06-05 15:43:37 +000021import urllib2
22
23# Imports from within Skia
24#
epoger@google.comdad53102013-06-12 14:25:30 +000025# We need to add the 'gm' directory, so that we can import gm_json.py within
26# that directory. That script allows us to parse the actual-results.json file
27# written out by the GM tool.
28# Make sure that the 'gm' dir is in the PYTHONPATH, but add it at the *end*
29# so any dirs that are already in the PYTHONPATH will be preferred.
30#
31# This assumes that the 'gm' directory has been checked out as a sibling of
32# the 'tools' directory containing this script, which will be the case if
33# 'trunk' was checked out as a single unit.
epoger@google.com99ba65a2013-06-05 15:43:37 +000034GM_DIRECTORY = os.path.realpath(
35 os.path.join(os.path.dirname(os.path.dirname(__file__)), 'gm'))
36if GM_DIRECTORY not in sys.path:
epoger@google.com2a192a82013-08-02 20:54:46 +000037 sys.path.append(GM_DIRECTORY)
epoger@google.com99ba65a2013-06-05 15:43:37 +000038import gm_json
39
epoger@google.com44f165f2013-08-24 20:45:31 +000040# TODO(epoger): In the long run, we want to build this list automatically,
41# but for now we hard-code it until we can properly address
42# https://code.google.com/p/skia/issues/detail?id=1544
43# ('live query of builder list makes rebaseline.py slow to start up')
44TEST_BUILDERS = [
45 'Test-Android-GalaxyNexus-SGX540-Arm7-Debug',
46 'Test-Android-GalaxyNexus-SGX540-Arm7-Release',
47 'Test-Android-IntelRhb-SGX544-x86-Debug',
48 'Test-Android-IntelRhb-SGX544-x86-Release',
49 'Test-Android-Nexus10-MaliT604-Arm7-Debug',
50 'Test-Android-Nexus10-MaliT604-Arm7-Release',
51 'Test-Android-Nexus4-Adreno320-Arm7-Debug',
52 'Test-Android-Nexus4-Adreno320-Arm7-Release',
53 'Test-Android-Nexus7-Tegra3-Arm7-Debug',
54 'Test-Android-Nexus7-Tegra3-Arm7-Release',
55 'Test-Android-NexusS-SGX540-Arm7-Debug',
56 'Test-Android-NexusS-SGX540-Arm7-Release',
57 'Test-Android-Xoom-Tegra2-Arm7-Debug',
58 'Test-Android-Xoom-Tegra2-Arm7-Release',
59 'Test-ChromeOS-Alex-GMA3150-x86-Debug',
60 'Test-ChromeOS-Alex-GMA3150-x86-Release',
61 'Test-ChromeOS-Daisy-MaliT604-Arm7-Debug',
62 'Test-ChromeOS-Daisy-MaliT604-Arm7-Release',
63 'Test-ChromeOS-Link-HD4000-x86_64-Debug',
64 'Test-ChromeOS-Link-HD4000-x86_64-Release',
65 'Test-Mac10.6-MacMini4.1-GeForce320M-x86-Debug',
66 'Test-Mac10.6-MacMini4.1-GeForce320M-x86-Release',
67 'Test-Mac10.6-MacMini4.1-GeForce320M-x86_64-Debug',
68 'Test-Mac10.6-MacMini4.1-GeForce320M-x86_64-Release',
69 'Test-Mac10.7-MacMini4.1-GeForce320M-x86-Debug',
70 'Test-Mac10.7-MacMini4.1-GeForce320M-x86-Release',
71 'Test-Mac10.7-MacMini4.1-GeForce320M-x86_64-Debug',
72 'Test-Mac10.7-MacMini4.1-GeForce320M-x86_64-Release',
73 'Test-Mac10.8-MacMini4.1-GeForce320M-x86-Debug',
74 'Test-Mac10.8-MacMini4.1-GeForce320M-x86-Release',
75 'Test-Mac10.8-MacMini4.1-GeForce320M-x86_64-Debug',
76 'Test-Mac10.8-MacMini4.1-GeForce320M-x86_64-Release',
77 'Test-Ubuntu12-ShuttleA-ATI5770-x86-Debug',
78 'Test-Ubuntu12-ShuttleA-ATI5770-x86-Release',
79 'Test-Ubuntu12-ShuttleA-ATI5770-x86_64-Debug',
80 'Test-Ubuntu12-ShuttleA-ATI5770-x86_64-Release',
81 'Test-Ubuntu12-ShuttleA-HD2000-x86_64-Release-Valgrind',
82 'Test-Ubuntu12-ShuttleA-NoGPU-x86_64-Debug',
83 'Test-Ubuntu13-ShuttleA-HD2000-x86_64-Debug-ASAN',
84 'Test-Win7-ShuttleA-HD2000-x86-Debug',
85 'Test-Win7-ShuttleA-HD2000-x86-Debug-ANGLE',
86 'Test-Win7-ShuttleA-HD2000-x86-Debug-DirectWrite',
87 'Test-Win7-ShuttleA-HD2000-x86-Release',
88 'Test-Win7-ShuttleA-HD2000-x86-Release-ANGLE',
89 'Test-Win7-ShuttleA-HD2000-x86-Release-DirectWrite',
90 'Test-Win7-ShuttleA-HD2000-x86_64-Debug',
91 'Test-Win7-ShuttleA-HD2000-x86_64-Release',
92]
epoger@google.com9166bf52013-05-30 15:46:19 +000093
epoger@google.com66ba9f92013-07-11 19:20:30 +000094class _InternalException(Exception):
epoger@google.com2a192a82013-08-02 20:54:46 +000095 pass
epoger@google.comdb29a312013-06-04 14:58:47 +000096
epoger@google.comffcbdbf2013-07-16 17:35:39 +000097# Object that handles exceptions, either raising them immediately or collecting
98# them to display later on.
99class ExceptionHandler(object):
100
epoger@google.com2a192a82013-08-02 20:54:46 +0000101 # params:
102 # keep_going_on_failure: if False, report failures and quit right away;
103 # if True, collect failures until
104 # ReportAllFailures() is called
105 def __init__(self, keep_going_on_failure=False):
106 self._keep_going_on_failure = keep_going_on_failure
107 self._failures_encountered = []
108 self._exiting = False
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000109
epoger@google.com2a192a82013-08-02 20:54:46 +0000110 # Exit the program with the given status value.
111 def _Exit(self, status=1):
112 self._exiting = True
113 sys.exit(status)
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000114
epoger@google.com2a192a82013-08-02 20:54:46 +0000115 # We have encountered an exception; either collect the info and keep going,
116 # or exit the program right away.
117 def RaiseExceptionOrContinue(self, e):
118 # If we are already quitting the program, propagate any exceptions
119 # so that the proper exit status will be communicated to the shell.
120 if self._exiting:
121 raise e
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000122
epoger@google.com2a192a82013-08-02 20:54:46 +0000123 if self._keep_going_on_failure:
124 print >> sys.stderr, 'WARNING: swallowing exception %s' % e
125 self._failures_encountered.append(e)
126 else:
127 print >> sys.stderr, e
128 print >> sys.stderr, (
129 'Halting at first exception; to keep going, re-run ' +
130 'with the --keep-going-on-failure option set.')
131 self._Exit()
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000132
epoger@google.com2a192a82013-08-02 20:54:46 +0000133 def ReportAllFailures(self):
134 if self._failures_encountered:
135 print >> sys.stderr, ('Encountered %d failures (see above).' %
136 len(self._failures_encountered))
137 self._Exit()
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000138
139
epoger@google.com99a8ec92013-06-19 18:56:59 +0000140# Object that rebaselines a JSON expectations file (not individual image files).
epoger@google.com99a8ec92013-06-19 18:56:59 +0000141class JsonRebaseliner(object):
epoger@google.com9166bf52013-05-30 15:46:19 +0000142
epoger@google.com2a192a82013-08-02 20:54:46 +0000143 # params:
144 # expectations_root: root directory of all expectations JSON files
145 # expectations_input_filename: filename (under expectations_root) of JSON
146 # expectations file to read; typically
147 # "expected-results.json"
148 # expectations_output_filename: filename (under expectations_root) to
149 # which updated expectations should be
150 # written; typically the same as
151 # expectations_input_filename, to overwrite
152 # the old content
153 # actuals_base_url: base URL from which to read actual-result JSON files
154 # actuals_filename: filename (under actuals_base_url) from which to read a
155 # summary of results; typically "actual-results.json"
156 # exception_handler: reference to rebaseline.ExceptionHandler object
157 # tests: list of tests to rebaseline, or None if we should rebaseline
158 # whatever files the JSON results summary file tells us to
159 # configs: which configs to run for each test, or None if we should
160 # rebaseline whatever configs the JSON results summary file tells
161 # us to
162 # add_new: if True, add expectations for tests which don't have any yet
163 def __init__(self, expectations_root, expectations_input_filename,
164 expectations_output_filename, actuals_base_url,
165 actuals_filename, exception_handler,
166 tests=None, configs=None, add_new=False):
167 self._expectations_root = expectations_root
168 self._expectations_input_filename = expectations_input_filename
169 self._expectations_output_filename = expectations_output_filename
170 self._tests = tests
171 self._configs = configs
172 self._actuals_base_url = actuals_base_url
173 self._actuals_filename = actuals_filename
174 self._exception_handler = exception_handler
175 self._add_new = add_new
176 self._image_filename_re = re.compile(gm_json.IMAGE_FILENAME_PATTERN)
177 self._using_svn = os.path.isdir(os.path.join(expectations_root, '.svn'))
epoger@google.com27e1c002013-07-24 15:38:39 +0000178
epoger@google.com2a192a82013-08-02 20:54:46 +0000179 # Executes subprocess.call(cmd).
180 # Raises an Exception if the command fails.
181 def _Call(self, cmd):
182 if subprocess.call(cmd) != 0:
183 raise _InternalException('error running command: ' + ' '.join(cmd))
epoger@google.com9166bf52013-05-30 15:46:19 +0000184
epoger@google.com2a192a82013-08-02 20:54:46 +0000185 # Returns the full contents of filepath, as a single string.
186 # If filepath looks like a URL, try to read it that way instead of as
187 # a path on local storage.
188 #
189 # Raises _InternalException if there is a problem.
190 def _GetFileContents(self, filepath):
191 if filepath.startswith('http:') or filepath.startswith('https:'):
192 try:
193 return urllib2.urlopen(filepath).read()
194 except urllib2.HTTPError as e:
195 raise _InternalException('unable to read URL %s: %s' % (
196 filepath, e))
197 else:
198 return open(filepath, 'r').read()
epoger@google.com99ba65a2013-06-05 15:43:37 +0000199
epoger@google.com2a192a82013-08-02 20:54:46 +0000200 # Returns a dictionary of actual results from actual-results.json file.
201 #
202 # The dictionary returned has this format:
203 # {
204 # u'imageblur_565.png': [u'bitmap-64bitMD5', 3359963596899141322],
205 # u'imageblur_8888.png': [u'bitmap-64bitMD5', 4217923806027861152],
206 # u'shadertext3_8888.png': [u'bitmap-64bitMD5', 3713708307125704716]
207 # }
208 #
209 # If the JSON actual result summary file cannot be loaded, logs a warning
210 # message and returns None.
211 # If the JSON actual result summary file can be loaded, but we have
212 # trouble parsing it, raises an Exception.
213 #
214 # params:
215 # json_url: URL pointing to a JSON actual result summary file
216 # sections: a list of section names to include in the results, e.g.
217 # [gm_json.JSONKEY_ACTUALRESULTS_FAILED,
218 # gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON] ;
219 # if None, then include ALL sections.
220 def _GetActualResults(self, json_url, sections=None):
221 try:
222 json_contents = self._GetFileContents(json_url)
223 except _InternalException:
224 print >> sys.stderr, (
225 'could not read json_url %s ; skipping this platform.' %
226 json_url)
227 return None
228 json_dict = gm_json.LoadFromString(json_contents)
229 results_to_return = {}
230 actual_results = json_dict[gm_json.JSONKEY_ACTUALRESULTS]
231 if not sections:
232 sections = actual_results.keys()
233 for section in sections:
234 section_results = actual_results[section]
235 if section_results:
236 results_to_return.update(section_results)
237 return results_to_return
epoger@google.come78d2072013-06-12 17:44:14 +0000238
epoger@google.com2a192a82013-08-02 20:54:46 +0000239 # Rebaseline all tests/types we specified in the constructor,
epoger@google.comfd040112013-08-20 16:21:55 +0000240 # within this builder's subdirectory in expectations/gm .
epoger@google.com2a192a82013-08-02 20:54:46 +0000241 #
242 # params:
epoger@google.com2a192a82013-08-02 20:54:46 +0000243 # builder : e.g. 'Test-Win7-ShuttleA-HD2000-x86-Release'
epoger@google.comfd040112013-08-20 16:21:55 +0000244 def RebaselineSubdir(self, builder):
epoger@google.com2a192a82013-08-02 20:54:46 +0000245 # Read in the actual result summary, and extract all the tests whose
246 # results we need to update.
247 actuals_url = '/'.join([self._actuals_base_url,
epoger@google.comfd040112013-08-20 16:21:55 +0000248 builder, self._actuals_filename])
epoger@google.com2a192a82013-08-02 20:54:46 +0000249 # In most cases, we won't need to re-record results that are already
250 # succeeding, but including the SUCCEEDED results will allow us to
251 # re-record expectations if they somehow get out of sync.
252 sections = [gm_json.JSONKEY_ACTUALRESULTS_FAILED,
253 gm_json.JSONKEY_ACTUALRESULTS_SUCCEEDED]
254 if self._add_new:
255 sections.append(gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON)
256 results_to_update = self._GetActualResults(json_url=actuals_url,
257 sections=sections)
epoger@google.come78d2072013-06-12 17:44:14 +0000258
epoger@google.com2a192a82013-08-02 20:54:46 +0000259 # Read in current expectations.
260 expectations_input_filepath = os.path.join(
epoger@google.comfd040112013-08-20 16:21:55 +0000261 self._expectations_root, builder, self._expectations_input_filename)
epoger@google.com2a192a82013-08-02 20:54:46 +0000262 expectations_dict = gm_json.LoadFromFile(expectations_input_filepath)
263 expected_results = expectations_dict[gm_json.JSONKEY_EXPECTEDRESULTS]
epoger@google.coma783f2b2013-07-08 17:51:58 +0000264
epoger@google.com2a192a82013-08-02 20:54:46 +0000265 # Update the expectations in memory, skipping any tests/configs that
266 # the caller asked to exclude.
267 skipped_images = []
268 if results_to_update:
269 for (image_name, image_results) in results_to_update.iteritems():
270 (test, config) = self._image_filename_re.match(image_name).groups()
271 if self._tests:
272 if test not in self._tests:
273 skipped_images.append(image_name)
274 continue
275 if self._configs:
276 if config not in self._configs:
277 skipped_images.append(image_name)
278 continue
279 if not expected_results.get(image_name):
280 expected_results[image_name] = {}
281 expected_results[image_name][gm_json.JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS] = \
epoger@google.com61822a22013-07-16 18:56:32 +0000282 [image_results]
epoger@google.coma783f2b2013-07-08 17:51:58 +0000283
epoger@google.com2a192a82013-08-02 20:54:46 +0000284 # Write out updated expectations.
285 expectations_output_filepath = os.path.join(
epoger@google.comfd040112013-08-20 16:21:55 +0000286 self._expectations_root, builder, self._expectations_output_filename)
epoger@google.com2a192a82013-08-02 20:54:46 +0000287 gm_json.WriteToFile(expectations_dict, expectations_output_filepath)
epoger@google.coma783f2b2013-07-08 17:51:58 +0000288
epoger@google.com2a192a82013-08-02 20:54:46 +0000289 # Mark the JSON file as plaintext, so text-style diffs can be applied.
290 # Fixes https://code.google.com/p/skia/issues/detail?id=1442
291 if self._using_svn:
292 self._Call(['svn', 'propset', '--quiet', 'svn:mime-type',
293 'text/x-json', expectations_output_filepath])
epoger@google.comec3397b2013-05-29 17:09:43 +0000294
epoger@google.com9166bf52013-05-30 15:46:19 +0000295# main...
epoger@google.comec3397b2013-05-29 17:09:43 +0000296
epoger@google.com9166bf52013-05-30 15:46:19 +0000297parser = argparse.ArgumentParser()
epoger@google.coma783f2b2013-07-08 17:51:58 +0000298parser.add_argument('--actuals-base-url',
299 help='base URL from which to read files containing JSON ' +
300 'summaries of actual GM results; defaults to %(default)s',
301 default='http://skia-autogen.googlecode.com/svn/gm-actual')
302parser.add_argument('--actuals-filename',
epoger@google.comfd040112013-08-20 16:21:55 +0000303 help='filename (within builder-specific subdirectories ' +
epoger@google.coma783f2b2013-07-08 17:51:58 +0000304 'of ACTUALS_BASE_URL) to read a summary of results from; ' +
305 'defaults to %(default)s',
306 default='actual-results.json')
307# TODO(epoger): Add test that exercises --add-new argument.
epoger@google.comdad53102013-06-12 14:25:30 +0000308parser.add_argument('--add-new', action='store_true',
309 help='in addition to the standard behavior of ' +
310 'updating expectations for failing tests, add ' +
311 'expectations for tests which don\'t have expectations ' +
312 'yet.')
epoger@google.comfd040112013-08-20 16:21:55 +0000313parser.add_argument('--builders', metavar='BUILDER', nargs='+',
314 help='which platforms to rebaseline; ' +
315 'if unspecified, rebaseline all platforms, same as ' +
316 '"--builders %s"' % ' '.join(sorted(TEST_BUILDERS)))
epoger@google.coma783f2b2013-07-08 17:51:58 +0000317# TODO(epoger): Add test that exercises --configs argument.
epoger@google.com9166bf52013-05-30 15:46:19 +0000318parser.add_argument('--configs', metavar='CONFIG', nargs='+',
319 help='which configurations to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000320 '"--configs 565 8888", as a filter over the full set of ' +
321 'results in ACTUALS_FILENAME; if unspecified, rebaseline ' +
322 '*all* configs that are available.')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000323parser.add_argument('--expectations-filename',
324 help='filename (under EXPECTATIONS_ROOT) to read ' +
325 'current expectations from, and to write new ' +
epoger@google.comc60e7452013-07-24 19:36:51 +0000326 'expectations into (unless a separate ' +
327 'EXPECTATIONS_FILENAME_OUTPUT has been specified); ' +
328 'defaults to %(default)s',
epoger@google.coma783f2b2013-07-08 17:51:58 +0000329 default='expected-results.json')
epoger@google.comc60e7452013-07-24 19:36:51 +0000330parser.add_argument('--expectations-filename-output',
331 help='filename (under EXPECTATIONS_ROOT) to write ' +
332 'updated expectations into; by default, overwrites the ' +
333 'input file (EXPECTATIONS_FILENAME)',
334 default='')
epoger@google.com99a8ec92013-06-19 18:56:59 +0000335parser.add_argument('--expectations-root',
336 help='root of expectations directory to update-- should ' +
epoger@google.comfd040112013-08-20 16:21:55 +0000337 'contain one or more builder subdirectories. Defaults to ' +
epoger@google.com99a8ec92013-06-19 18:56:59 +0000338 '%(default)s',
epoger@google.come94a7d22013-07-23 19:37:03 +0000339 default=os.path.join('expectations', 'gm'))
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000340parser.add_argument('--keep-going-on-failure', action='store_true',
341 help='instead of halting at the first error encountered, ' +
342 'keep going and rebaseline as many tests as possible, ' +
343 'and then report the full set of errors at the end')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000344# TODO(epoger): Add test that exercises --tests argument.
epoger@google.com99ba65a2013-06-05 15:43:37 +0000345parser.add_argument('--tests', metavar='TEST', nargs='+',
epoger@google.com9166bf52013-05-30 15:46:19 +0000346 help='which tests to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000347 '"--tests aaclip bigmatrix", as a filter over the full ' +
348 'set of results in ACTUALS_FILENAME; if unspecified, ' +
349 'rebaseline *all* tests that are available.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000350args = parser.parse_args()
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000351exception_handler = ExceptionHandler(
352 keep_going_on_failure=args.keep_going_on_failure)
epoger@google.comfd040112013-08-20 16:21:55 +0000353if args.builders:
354 builders = args.builders
epoger@google.com2a192a82013-08-02 20:54:46 +0000355 missing_json_is_fatal = True
epoger@google.com99a8ec92013-06-19 18:56:59 +0000356else:
epoger@google.comfd040112013-08-20 16:21:55 +0000357 builders = sorted(TEST_BUILDERS)
epoger@google.com2a192a82013-08-02 20:54:46 +0000358 missing_json_is_fatal = False
epoger@google.comfd040112013-08-20 16:21:55 +0000359for builder in builders:
360 if not builder in TEST_BUILDERS:
361 raise Exception(('unrecognized builder "%s"; ' +
epoger@google.com2a192a82013-08-02 20:54:46 +0000362 'should be one of %s') % (
epoger@google.comfd040112013-08-20 16:21:55 +0000363 builder, TEST_BUILDERS))
epoger@google.com99a8ec92013-06-19 18:56:59 +0000364
epoger@google.comfd040112013-08-20 16:21:55 +0000365 expectations_json_file = os.path.join(args.expectations_root, builder,
epoger@google.com2a192a82013-08-02 20:54:46 +0000366 args.expectations_filename)
367 if os.path.isfile(expectations_json_file):
368 rebaseliner = JsonRebaseliner(
369 expectations_root=args.expectations_root,
370 expectations_input_filename=args.expectations_filename,
371 expectations_output_filename=(args.expectations_filename_output or
372 args.expectations_filename),
373 tests=args.tests, configs=args.configs,
374 actuals_base_url=args.actuals_base_url,
375 actuals_filename=args.actuals_filename,
376 exception_handler=exception_handler,
377 add_new=args.add_new)
epoger@google.com3e7399f2013-07-10 17:23:47 +0000378 try:
epoger@google.comfd040112013-08-20 16:21:55 +0000379 rebaseliner.RebaselineSubdir(builder=builder)
epoger@google.com3e7399f2013-07-10 17:23:47 +0000380 except BaseException as e:
epoger@google.com2a192a82013-08-02 20:54:46 +0000381 exception_handler.RaiseExceptionOrContinue(e)
382 else:
383 exception_handler.RaiseExceptionOrContinue(_InternalException(
384 'expectations_json_file %s not found' % expectations_json_file))
epoger@google.comffcbdbf2013-07-16 17:35:39 +0000385
386exception_handler.ReportAllFailures()