blob: 1abc1dc91c46ec66c80790cd5f76cded0ad2ee65 [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.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.
epoger@google.com66ba9f92013-07-11 19:20:30 +0000109 #
110 # Raises _InternalException if there is a problem.
epoger@google.coma783f2b2013-07-08 17:51:58 +0000111 def _GetFileContents(self, filepath):
112 if filepath.startswith('http:') or filepath.startswith('https:'):
epoger@google.com66ba9f92013-07-11 19:20:30 +0000113 try:
114 return urllib2.urlopen(filepath).read()
115 except urllib2.HTTPError as e:
116 raise _InternalException('unable to read URL %s: %s' % (
117 filepath, e))
epoger@google.com99ba65a2013-06-05 15:43:37 +0000118 else:
epoger@google.coma783f2b2013-07-08 17:51:58 +0000119 return open(filepath, 'r').read()
epoger@google.com99ba65a2013-06-05 15:43:37 +0000120
epoger@google.come78d2072013-06-12 17:44:14 +0000121 # Returns a dictionary of actual results from actual-results.json file.
122 #
123 # The dictionary returned has this format:
124 # {
125 # u'imageblur_565.png': [u'bitmap-64bitMD5', 3359963596899141322],
126 # u'imageblur_8888.png': [u'bitmap-64bitMD5', 4217923806027861152],
127 # u'shadertext3_8888.png': [u'bitmap-64bitMD5', 3713708307125704716]
128 # }
129 #
epoger@google.com66ba9f92013-07-11 19:20:30 +0000130 # If the JSON actual result summary file cannot be loaded, logs a warning
131 # message and returns None.
132 # If the JSON actual result summary file can be loaded, but we have
133 # trouble parsing it, raises an Exception.
epoger@google.come78d2072013-06-12 17:44:14 +0000134 #
135 # params:
136 # json_url: URL pointing to a JSON actual result summary file
137 # sections: a list of section names to include in the results, e.g.
138 # [gm_json.JSONKEY_ACTUALRESULTS_FAILED,
139 # gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON] ;
140 # if None, then include ALL sections.
141 def _GetActualResults(self, json_url, sections=None):
epoger@google.com66ba9f92013-07-11 19:20:30 +0000142 try:
143 json_contents = self._GetFileContents(json_url)
144 except _InternalException:
145 print >> sys.stderr, (
146 'could not read json_url %s ; skipping this platform.' %
147 json_url)
148 return None
epoger@google.come78d2072013-06-12 17:44:14 +0000149 json_dict = gm_json.LoadFromString(json_contents)
150 results_to_return = {}
151 actual_results = json_dict[gm_json.JSONKEY_ACTUALRESULTS]
152 if not sections:
153 sections = actual_results.keys()
154 for section in sections:
155 section_results = actual_results[section]
156 if section_results:
157 results_to_return.update(section_results)
158 return results_to_return
159
epoger@google.com99a8ec92013-06-19 18:56:59 +0000160 # Rebaseline all tests/types we specified in the constructor,
161 # within this gm-expectations subdir.
162 #
163 # params:
164 # subdir : e.g. 'base-shuttle-win7-intel-float'
165 # builder : e.g. 'Test-Win7-ShuttleA-HD2000-x86-Release'
166 def RebaselineSubdir(self, subdir, builder):
epoger@google.coma783f2b2013-07-08 17:51:58 +0000167 # Read in the actual result summary, and extract all the tests whose
168 # results we need to update.
169 actuals_url = '/'.join([self._actuals_base_url,
170 subdir, builder, subdir,
171 self._actuals_filename])
172 sections = [gm_json.JSONKEY_ACTUALRESULTS_FAILED]
173 if self._add_new:
174 sections.append(gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON)
175 results_to_update = self._GetActualResults(json_url=actuals_url,
176 sections=sections)
epoger@google.come78d2072013-06-12 17:44:14 +0000177
epoger@google.coma783f2b2013-07-08 17:51:58 +0000178 # Read in current expectations.
179 expectations_json_filepath = os.path.join(
180 self._expectations_root, subdir, self._expectations_filename)
181 expectations_dict = gm_json.LoadFromFile(expectations_json_filepath)
182
183 # Update the expectations in memory, skipping any tests/configs that
184 # the caller asked to exclude.
185 skipped_images = []
186 if results_to_update:
187 for (image_name, image_results) in results_to_update.iteritems():
188 (test, config) = self._testname_pattern.match(image_name).groups()
189 if self._tests:
190 if test not in self._tests:
191 skipped_images.append(image_name)
192 continue
193 if self._configs:
194 if config not in self._configs:
195 skipped_images.append(image_name)
196 continue
197 expectations_dict[gm_json.JSONKEY_EXPECTEDRESULTS] \
198 [image_name] \
199 [gm_json.JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS] = \
200 [image_results]
201
202 # Write out updated expectations.
203 gm_json.WriteToFile(expectations_dict, expectations_json_filepath)
204
epoger@google.comec3397b2013-05-29 17:09:43 +0000205
epoger@google.com9166bf52013-05-30 15:46:19 +0000206# main...
epoger@google.comec3397b2013-05-29 17:09:43 +0000207
epoger@google.com9166bf52013-05-30 15:46:19 +0000208parser = argparse.ArgumentParser()
epoger@google.coma783f2b2013-07-08 17:51:58 +0000209parser.add_argument('--actuals-base-url',
210 help='base URL from which to read files containing JSON ' +
211 'summaries of actual GM results; defaults to %(default)s',
212 default='http://skia-autogen.googlecode.com/svn/gm-actual')
213parser.add_argument('--actuals-filename',
214 help='filename (within platform-specific subdirectories ' +
215 'of ACTUALS_BASE_URL) to read a summary of results from; ' +
216 'defaults to %(default)s',
217 default='actual-results.json')
218# TODO(epoger): Add test that exercises --add-new argument.
epoger@google.comdad53102013-06-12 14:25:30 +0000219parser.add_argument('--add-new', action='store_true',
220 help='in addition to the standard behavior of ' +
221 'updating expectations for failing tests, add ' +
222 'expectations for tests which don\'t have expectations ' +
223 'yet.')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000224# TODO(epoger): Add test that exercises --configs argument.
epoger@google.com9166bf52013-05-30 15:46:19 +0000225parser.add_argument('--configs', metavar='CONFIG', nargs='+',
226 help='which configurations to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000227 '"--configs 565 8888", as a filter over the full set of ' +
228 'results in ACTUALS_FILENAME; if unspecified, rebaseline ' +
229 '*all* configs that are available.')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000230# TODO(epoger): The --dry-run argument will no longer be needed once we
231# are only rebaselining JSON files.
epoger@google.com82f31782013-06-11 15:45:46 +0000232parser.add_argument('--dry-run', action='store_true',
epoger@google.com9166bf52013-05-30 15:46:19 +0000233 help='instead of actually downloading files or adding ' +
234 'files to checkout, display a list of operations that ' +
235 'we would normally perform')
epoger@google.coma783f2b2013-07-08 17:51:58 +0000236parser.add_argument('--expectations-filename',
237 help='filename (under EXPECTATIONS_ROOT) to read ' +
238 'current expectations from, and to write new ' +
239 'expectations into; defaults to %(default)s',
240 default='expected-results.json')
epoger@google.com99a8ec92013-06-19 18:56:59 +0000241parser.add_argument('--expectations-root',
242 help='root of expectations directory to update-- should ' +
243 'contain one or more base-* subdirectories. Defaults to ' +
244 '%(default)s',
245 default='.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000246parser.add_argument('--subdirs', metavar='SUBDIR', nargs='+',
247 help='which platform subdirectories to rebaseline; ' +
248 'if unspecified, rebaseline all subdirs, same as ' +
249 '"--subdirs %s"' % ' '.join(sorted(SUBDIR_MAPPING.keys())))
epoger@google.coma783f2b2013-07-08 17:51:58 +0000250# TODO(epoger): Add test that exercises --tests argument.
epoger@google.com99ba65a2013-06-05 15:43:37 +0000251parser.add_argument('--tests', metavar='TEST', nargs='+',
epoger@google.com9166bf52013-05-30 15:46:19 +0000252 help='which tests to rebaseline, e.g. ' +
epoger@google.com9de25e32013-07-10 15:27:18 +0000253 '"--tests aaclip bigmatrix", as a filter over the full ' +
254 'set of results in ACTUALS_FILENAME; if unspecified, ' +
255 'rebaseline *all* tests that are available.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000256args = parser.parse_args()
epoger@google.com99a8ec92013-06-19 18:56:59 +0000257if args.subdirs:
258 subdirs = args.subdirs
259 missing_json_is_fatal = True
260else:
261 subdirs = sorted(SUBDIR_MAPPING.keys())
262 missing_json_is_fatal = False
263for subdir in subdirs:
264 if not subdir in SUBDIR_MAPPING.keys():
265 raise Exception(('unrecognized platform subdir "%s"; ' +
266 'should be one of %s') % (
267 subdir, SUBDIR_MAPPING.keys()))
268 builder = SUBDIR_MAPPING[subdir]
269
270 # We instantiate different Rebaseliner objects depending
271 # on whether we are rebaselining an expected-results.json file, or
272 # individual image files. Different gm-expected subdirectories may move
273 # from individual image files to JSON-format expectations at different
274 # times, so we need to make this determination per subdirectory.
275 #
276 # See https://goto.google.com/ChecksumTransitionDetail
277 expectations_json_file = os.path.join(args.expectations_root, subdir,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000278 args.expectations_filename)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000279 if os.path.isfile(expectations_json_file):
epoger@google.com99a8ec92013-06-19 18:56:59 +0000280 rebaseliner = JsonRebaseliner(
281 expectations_root=args.expectations_root,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000282 expectations_filename=args.expectations_filename,
epoger@google.com99a8ec92013-06-19 18:56:59 +0000283 tests=args.tests, configs=args.configs,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000284 actuals_base_url=args.actuals_base_url,
285 actuals_filename=args.actuals_filename,
286 add_new=args.add_new)
epoger@google.com99a8ec92013-06-19 18:56:59 +0000287 else:
epoger@google.com89fa4b92013-07-10 16:54:10 +0000288 # TODO(epoger): When we get rid of the ImageRebaseliner implementation,
289 # we should raise an Exception in this case (no JSON expectations file
290 # found to update), to prevent a recurrence of
291 # https://code.google.com/p/skia/issues/detail?id=1403 ('rebaseline.py
292 # script fails with misleading output when run outside of gm-expected
293 # dir')
epoger@google.com99a8ec92013-06-19 18:56:59 +0000294 rebaseliner = rebaseline_imagefiles.ImageRebaseliner(
295 expectations_root=args.expectations_root,
296 tests=args.tests, configs=args.configs,
297 dry_run=args.dry_run,
epoger@google.coma783f2b2013-07-08 17:51:58 +0000298 json_base_url=args.actuals_base_url,
299 json_filename=args.actuals_filename,
epoger@google.com99a8ec92013-06-19 18:56:59 +0000300 add_new=args.add_new,
301 missing_json_is_fatal=missing_json_is_fatal)
epoger@google.com3e7399f2013-07-10 17:23:47 +0000302 try:
303 rebaseliner.RebaselineSubdir(subdir=subdir, builder=builder)
304 except BaseException as e:
305 print >> sys.stderr, e
306 sys.exit(1)