blob: 157d8c213da36b79e7a9abd088dfb823653fec0d [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 subprocess
21import sys
epoger@google.com99ba65a2013-06-05 15:43:37 +000022import urllib2
23
24# Imports from within Skia
25#
epoger@google.comdad53102013-06-12 14:25:30 +000026# We need to add the 'gm' directory, so that we can import gm_json.py within
27# that directory. That script allows us to parse the actual-results.json file
28# written out by the GM tool.
29# Make sure that the 'gm' dir is in the PYTHONPATH, but add it at the *end*
30# so any dirs that are already in the PYTHONPATH will be preferred.
31#
32# This assumes that the 'gm' directory has been checked out as a sibling of
33# the 'tools' directory containing this script, which will be the case if
34# 'trunk' was checked out as a single unit.
epoger@google.com99ba65a2013-06-05 15:43:37 +000035GM_DIRECTORY = os.path.realpath(
36 os.path.join(os.path.dirname(os.path.dirname(__file__)), 'gm'))
37if GM_DIRECTORY not in sys.path:
38 sys.path.append(GM_DIRECTORY)
39import gm_json
40
senorblanco@chromium.org782f3b42012-10-29 18:06:26 +000041
epoger@google.comec3397b2013-05-29 17:09:43 +000042# Mapping of gm-expectations subdir (under
43# https://skia.googlecode.com/svn/gm-expected/ )
44# to builder name (see list at http://108.170.217.252:10117/builders )
epoger@google.com9166bf52013-05-30 15:46:19 +000045SUBDIR_MAPPING = {
epoger@google.comec3397b2013-05-29 17:09:43 +000046 'base-shuttle-win7-intel-float':
47 'Test-Win7-ShuttleA-HD2000-x86-Release',
48 'base-shuttle-win7-intel-angle':
49 'Test-Win7-ShuttleA-HD2000-x86-Release-ANGLE',
50 'base-shuttle-win7-intel-directwrite':
51 'Test-Win7-ShuttleA-HD2000-x86-Release-DirectWrite',
52 'base-shuttle_ubuntu12_ati5770':
53 'Test-Ubuntu12-ShuttleA-ATI5770-x86_64-Release',
54 'base-macmini':
55 'Test-Mac10.6-MacMini4.1-GeForce320M-x86-Release',
56 'base-macmini-lion-float':
57 'Test-Mac10.7-MacMini4.1-GeForce320M-x86-Release',
58 'base-android-galaxy-nexus':
59 'Test-Android-GalaxyNexus-SGX540-Arm7-Debug',
60 'base-android-nexus-7':
61 'Test-Android-Nexus7-Tegra3-Arm7-Release',
62 'base-android-nexus-s':
63 'Test-Android-NexusS-SGX540-Arm7-Release',
64 'base-android-xoom':
65 'Test-Android-Xoom-Tegra2-Arm7-Release',
66 'base-android-nexus-10':
67 'Test-Android-Nexus10-MaliT604-Arm7-Release',
68}
69
epoger@google.com9166bf52013-05-30 15:46:19 +000070
epoger@google.comdb29a312013-06-04 14:58:47 +000071class CommandFailedException(Exception):
72 pass
73
epoger@google.com9166bf52013-05-30 15:46:19 +000074class Rebaseliner(object):
75
76 # params:
epoger@google.com99ba65a2013-06-05 15:43:37 +000077 # json_base_url: base URL from which to read json_filename
78 # json_filename: filename (under json_base_url) from which to read a
79 # summary of results; typically "actual-results.json"
80 # subdirs: which platform subdirectories to rebaseline; if not specified,
epoger@google.com9166bf52013-05-30 15:46:19 +000081 # rebaseline all platform subdirectories
epoger@google.com99ba65a2013-06-05 15:43:37 +000082 # tests: list of tests to rebaseline, or None if we should rebaseline
83 # whatever files the JSON results summary file tells us to
84 # configs: which configs to run for each test; this should only be
85 # specified if the list of tests was also specified (otherwise,
86 # the JSON file will give us test names and configs)
epoger@google.com9166bf52013-05-30 15:46:19 +000087 # dry_run: if True, instead of actually downloading files or adding
88 # files to checkout, display a list of operations that
89 # we would normally perform
epoger@google.comdad53102013-06-12 14:25:30 +000090 # add_new: if True, add expectations for tests which don't have any yet
epoger@google.com99ba65a2013-06-05 15:43:37 +000091 def __init__(self, json_base_url, json_filename,
epoger@google.comdad53102013-06-12 14:25:30 +000092 subdirs=None, tests=None, configs=None, dry_run=False,
93 add_new=False):
epoger@google.com99ba65a2013-06-05 15:43:37 +000094 if configs and not tests:
95 raise ValueError('configs should only be specified if tests ' +
96 'were specified also')
epoger@google.com9166bf52013-05-30 15:46:19 +000097 self._tests = tests
98 self._configs = configs
99 if not subdirs:
100 self._subdirs = sorted(SUBDIR_MAPPING.keys())
epoger@google.combda4e912013-06-11 16:16:02 +0000101 self._missing_json_is_fatal = False
epoger@google.com9166bf52013-05-30 15:46:19 +0000102 else:
103 self._subdirs = subdirs
epoger@google.combda4e912013-06-11 16:16:02 +0000104 self._missing_json_is_fatal = True
epoger@google.com99ba65a2013-06-05 15:43:37 +0000105 self._json_base_url = json_base_url
106 self._json_filename = json_filename
epoger@google.com9166bf52013-05-30 15:46:19 +0000107 self._dry_run = dry_run
epoger@google.comdad53102013-06-12 14:25:30 +0000108 self._add_new = add_new
epoger@google.come78d2072013-06-12 17:44:14 +0000109 self._googlestorage_gm_actuals_root = (
110 'http://chromium-skia-gm.commondatastorage.googleapis.com/gm')
111 self._testname_pattern = re.compile('(\S+)_(\S+).png')
epoger@google.com9166bf52013-05-30 15:46:19 +0000112 self._is_svn_checkout = (
113 os.path.exists('.svn') or
114 os.path.exists(os.path.join(os.pardir, '.svn')))
115 self._is_git_checkout = (
116 os.path.exists('.git') or
117 os.path.exists(os.path.join(os.pardir, '.git')))
118
epoger@google.comdb29a312013-06-04 14:58:47 +0000119 # If dry_run is False, execute subprocess.call(cmd).
120 # If dry_run is True, print the command we would have otherwise run.
121 # Raises a CommandFailedException if the command fails.
122 def _Call(self, cmd):
epoger@google.com9166bf52013-05-30 15:46:19 +0000123 if self._dry_run:
124 print '%s' % ' '.join(cmd)
epoger@google.comdb29a312013-06-04 14:58:47 +0000125 return
126 if subprocess.call(cmd) != 0:
127 raise CommandFailedException('error running command: ' +
128 ' '.join(cmd))
129
epoger@google.come78d2072013-06-12 17:44:14 +0000130 # Download a single actual result from GoogleStorage, returning True if it
131 # succeeded.
132 def _DownloadFromGoogleStorage(self, infilename, outfilename, all_results):
133 test_name = self._testname_pattern.match(infilename).group(1)
134 if not test_name:
135 print '# unable to find test_name for infilename %s' % infilename
136 return False
137 try:
138 hash_type, hash_value = all_results[infilename]
139 except KeyError:
140 print ('# unable to find filename %s in all_results dict' %
141 infilename)
142 return False
143 url = '%s/%s/%s/%s.png' % (self._googlestorage_gm_actuals_root,
144 hash_type, test_name, hash_value)
145 try:
146 self._DownloadFile(source_url=url, dest_filename=outfilename)
147 return True
148 except CommandFailedException:
149 print '# Couldn\'t fetch gs_url %s' % url
150 return False
151
152 # Download a single actual result from skia-autogen, returning True if it
153 # succeeded.
154 def _DownloadFromAutogen(self, infilename, outfilename,
155 expectations_subdir, builder_name):
156 url = ('http://skia-autogen.googlecode.com/svn/gm-actual/' +
157 expectations_subdir + '/' + builder_name + '/' +
158 expectations_subdir + '/' + infilename)
159 try:
160 self._DownloadFile(source_url=url, dest_filename=outfilename)
161 return True
162 except CommandFailedException:
163 print '# Couldn\'t fetch autogen_url %s' % url
164 return False
165
epoger@google.comdb29a312013-06-04 14:58:47 +0000166 # Download a single file, raising a CommandFailedException if it fails.
167 def _DownloadFile(self, source_url, dest_filename):
168 # Download into a temporary file and then rename it afterwards,
169 # so that we don't corrupt the existing file if it fails midway thru.
170 temp_filename = os.path.join(os.path.dirname(dest_filename),
171 '.temp-' + os.path.basename(dest_filename))
172
173 # TODO(epoger): Replace calls to "curl"/"mv" (which will only work on
174 # Unix) with a Python HTTP library (which should work cross-platform)
175 self._Call([ 'curl', '--fail', '--silent', source_url,
176 '--output', temp_filename ])
177 self._Call([ 'mv', temp_filename, dest_filename ])
epoger@google.com9166bf52013-05-30 15:46:19 +0000178
epoger@google.com99ba65a2013-06-05 15:43:37 +0000179 # Returns the full contents of a URL, as a single string.
180 #
181 # Unlike standard URL handling, we allow relative "file:" URLs;
182 # for example, "file:one/two" resolves to the file ./one/two
183 # (relative to current working dir)
184 def _GetContentsOfUrl(self, url):
185 file_prefix = 'file:'
186 if url.startswith(file_prefix):
187 filename = url[len(file_prefix):]
188 return open(filename, 'r').read()
189 else:
190 return urllib2.urlopen(url).read()
191
epoger@google.come78d2072013-06-12 17:44:14 +0000192 # Returns a dictionary of actual results from actual-results.json file.
193 #
194 # The dictionary returned has this format:
195 # {
196 # u'imageblur_565.png': [u'bitmap-64bitMD5', 3359963596899141322],
197 # u'imageblur_8888.png': [u'bitmap-64bitMD5', 4217923806027861152],
198 # u'shadertext3_8888.png': [u'bitmap-64bitMD5', 3713708307125704716]
199 # }
200 #
201 # If the JSON actual result summary file cannot be loaded, the behavior
202 # depends on self._missing_json_is_fatal:
203 # - if true: execution will halt with an exception
204 # - if false: we will log an error message but return an empty dictionary
205 #
206 # params:
207 # json_url: URL pointing to a JSON actual result summary file
208 # sections: a list of section names to include in the results, e.g.
209 # [gm_json.JSONKEY_ACTUALRESULTS_FAILED,
210 # gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON] ;
211 # if None, then include ALL sections.
212 def _GetActualResults(self, json_url, sections=None):
213 try:
214 json_contents = self._GetContentsOfUrl(json_url)
215 except (urllib2.HTTPError, IOError):
216 message = 'unable to load JSON summary URL %s' % json_url
217 if self._missing_json_is_fatal:
218 raise ValueError(message)
219 else:
220 print '# %s' % message
221 return {}
222
223 json_dict = gm_json.LoadFromString(json_contents)
224 results_to_return = {}
225 actual_results = json_dict[gm_json.JSONKEY_ACTUALRESULTS]
226 if not sections:
227 sections = actual_results.keys()
228 for section in sections:
229 section_results = actual_results[section]
230 if section_results:
231 results_to_return.update(section_results)
232 return results_to_return
233
epoger@google.com99ba65a2013-06-05 15:43:37 +0000234 # Returns a list of files that require rebaselining.
235 #
236 # Note that this returns a list of FILES, like this:
237 # ['imageblur_565.png', 'xfermodes_pdf.png']
238 # rather than a list of TESTS, like this:
239 # ['imageblur', 'xfermodes']
240 #
241 # params:
242 # json_url: URL pointing to a JSON actual result summary file
epoger@google.comdad53102013-06-12 14:25:30 +0000243 # add_new: if True, then return files listed in any of these sections:
244 # - JSONKEY_ACTUALRESULTS_FAILED
245 # - JSONKEY_ACTUALRESULTS_NOCOMPARISON
246 # if False, then return files listed in these sections:
247 # - JSONKEY_ACTUALRESULTS_FAILED
epoger@google.com99ba65a2013-06-05 15:43:37 +0000248 #
epoger@google.comdad53102013-06-12 14:25:30 +0000249 def _GetFilesToRebaseline(self, json_url, add_new):
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000250 if self._dry_run:
251 print ''
252 print '#'
epoger@google.com99ba65a2013-06-05 15:43:37 +0000253 print ('# Getting files to rebaseline from JSON summary URL %s ...'
254 % json_url)
epoger@google.comdad53102013-06-12 14:25:30 +0000255 sections = [gm_json.JSONKEY_ACTUALRESULTS_FAILED]
256 if add_new:
257 sections.append(gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON)
epoger@google.come78d2072013-06-12 17:44:14 +0000258 results_to_rebaseline = self._GetActualResults(json_url=json_url,
259 sections=sections)
260 files_to_rebaseline = results_to_rebaseline.keys()
261 files_to_rebaseline.sort()
epoger@google.com99ba65a2013-06-05 15:43:37 +0000262 print '# ... found files_to_rebaseline %s' % files_to_rebaseline
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000263 if self._dry_run:
264 print '#'
epoger@google.com99ba65a2013-06-05 15:43:37 +0000265 return files_to_rebaseline
266
epoger@google.com9166bf52013-05-30 15:46:19 +0000267 # Rebaseline a single file.
268 def _RebaselineOneFile(self, expectations_subdir, builder_name,
epoger@google.come78d2072013-06-12 17:44:14 +0000269 infilename, outfilename, all_results):
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000270 if self._dry_run:
271 print ''
epoger@google.com99ba65a2013-06-05 15:43:37 +0000272 print '# ' + infilename
epoger@google.comdb29a312013-06-04 14:58:47 +0000273
epoger@google.come78d2072013-06-12 17:44:14 +0000274 # First try to download this result image from Google Storage.
275 # If that fails, try skia-autogen.
276 # If that fails too, just go on to the next file.
epoger@google.comdb29a312013-06-04 14:58:47 +0000277 #
278 # This not treated as a fatal failure because not all
279 # platforms generate all configs (e.g., Android does not
280 # generate PDF).
281 #
epoger@google.come78d2072013-06-12 17:44:14 +0000282 # TODO(epoger): Once we are downloading only files that the
283 # actual-results.json file told us to, this should become a
284 # fatal error. (If the actual-results.json file told us that
285 # the test failed with XXX results, we should be able to download
286 # those results every time.)
287 if not self._DownloadFromGoogleStorage(infilename=infilename,
288 outfilename=outfilename,
289 all_results=all_results):
290 if not self._DownloadFromAutogen(infilename=infilename,
291 outfilename=outfilename,
292 expectations_subdir=expectations_subdir,
293 builder_name=builder_name):
294 print '# Couldn\'t fetch infilename ' + infilename
295 return
epoger@google.comdb29a312013-06-04 14:58:47 +0000296
epoger@google.comdad53102013-06-12 14:25:30 +0000297 # Add this file to version control (if appropriate).
298 if self._add_new:
299 if self._is_svn_checkout:
300 cmd = [ 'svn', 'add', '--quiet', outfilename ]
301 self._Call(cmd)
302 cmd = [ 'svn', 'propset', '--quiet', 'svn:mime-type',
303 'image/png', outfilename ];
304 self._Call(cmd)
305 elif self._is_git_checkout:
306 cmd = [ 'git', 'add', outfilename ]
307 self._Call(cmd)
epoger@google.com9166bf52013-05-30 15:46:19 +0000308
309 # Rebaseline the given configs for a single test.
310 #
311 # params:
312 # expectations_subdir
313 # builder_name
314 # test: a single test to rebaseline
epoger@google.come78d2072013-06-12 17:44:14 +0000315 # all_results: a dictionary of all actual results
316 def _RebaselineOneTest(self, expectations_subdir, builder_name, test,
317 all_results):
epoger@google.com9166bf52013-05-30 15:46:19 +0000318 if self._configs:
319 configs = self._configs
320 else:
321 if (expectations_subdir == 'base-shuttle-win7-intel-angle'):
322 configs = [ 'angle', 'anglemsaa16' ]
323 else:
324 configs = [ '565', '8888', 'gpu', 'pdf', 'mesa', 'msaa16',
325 'msaa4' ]
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000326 if self._dry_run:
327 print ''
epoger@google.com9166bf52013-05-30 15:46:19 +0000328 print '# ' + expectations_subdir + ':'
329 for config in configs:
330 infilename = test + '_' + config + '.png'
epoger@google.com9166bf52013-05-30 15:46:19 +0000331 outfilename = os.path.join(expectations_subdir, infilename);
332 self._RebaselineOneFile(expectations_subdir=expectations_subdir,
333 builder_name=builder_name,
334 infilename=infilename,
epoger@google.come78d2072013-06-12 17:44:14 +0000335 outfilename=outfilename,
336 all_results=all_results)
epoger@google.com9166bf52013-05-30 15:46:19 +0000337
338 # Rebaseline all platforms/tests/types we specified in the constructor.
339 def RebaselineAll(self):
epoger@google.com99ba65a2013-06-05 15:43:37 +0000340 for subdir in self._subdirs:
341 if not subdir in SUBDIR_MAPPING.keys():
342 raise Exception(('unrecognized platform subdir "%s"; ' +
343 'should be one of %s') % (
344 subdir, SUBDIR_MAPPING.keys()))
345 builder_name = SUBDIR_MAPPING[subdir]
epoger@google.come78d2072013-06-12 17:44:14 +0000346 json_url = '/'.join([self._json_base_url,
347 subdir, builder_name, subdir,
348 self._json_filename])
349 all_results = self._GetActualResults(json_url=json_url)
350
epoger@google.com99ba65a2013-06-05 15:43:37 +0000351 if self._tests:
352 for test in self._tests:
353 self._RebaselineOneTest(expectations_subdir=subdir,
354 builder_name=builder_name,
epoger@google.come78d2072013-06-12 17:44:14 +0000355 test=test, all_results=all_results)
epoger@google.com99ba65a2013-06-05 15:43:37 +0000356 else: # get the raw list of files that need rebaselining from JSON
epoger@google.comdad53102013-06-12 14:25:30 +0000357 filenames = self._GetFilesToRebaseline(json_url=json_url,
358 add_new=self._add_new)
epoger@google.com99ba65a2013-06-05 15:43:37 +0000359 for filename in filenames:
360 outfilename = os.path.join(subdir, filename);
361 self._RebaselineOneFile(expectations_subdir=subdir,
362 builder_name=builder_name,
363 infilename=filename,
epoger@google.come78d2072013-06-12 17:44:14 +0000364 outfilename=outfilename,
365 all_results=all_results)
epoger@google.comec3397b2013-05-29 17:09:43 +0000366
epoger@google.com9166bf52013-05-30 15:46:19 +0000367# main...
epoger@google.comec3397b2013-05-29 17:09:43 +0000368
epoger@google.com9166bf52013-05-30 15:46:19 +0000369parser = argparse.ArgumentParser()
epoger@google.comdad53102013-06-12 14:25:30 +0000370parser.add_argument('--add-new', action='store_true',
371 help='in addition to the standard behavior of ' +
372 'updating expectations for failing tests, add ' +
373 'expectations for tests which don\'t have expectations ' +
374 'yet.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000375parser.add_argument('--configs', metavar='CONFIG', nargs='+',
376 help='which configurations to rebaseline, e.g. ' +
377 '"--configs 565 8888"; if unspecified, run a default ' +
epoger@google.com99ba65a2013-06-05 15:43:37 +0000378 'set of configs. This should ONLY be specified if ' +
379 '--tests has also been specified.')
epoger@google.com82f31782013-06-11 15:45:46 +0000380parser.add_argument('--dry-run', action='store_true',
epoger@google.com9166bf52013-05-30 15:46:19 +0000381 help='instead of actually downloading files or adding ' +
382 'files to checkout, display a list of operations that ' +
383 'we would normally perform')
epoger@google.com82f31782013-06-11 15:45:46 +0000384parser.add_argument('--json-base-url',
epoger@google.com99ba65a2013-06-05 15:43:37 +0000385 help='base URL from which to read JSON_FILENAME ' +
386 'files; defaults to %(default)s',
387 default='http://skia-autogen.googlecode.com/svn/gm-actual')
epoger@google.com82f31782013-06-11 15:45:46 +0000388parser.add_argument('--json-filename',
epoger@google.com99ba65a2013-06-05 15:43:37 +0000389 help='filename (under JSON_BASE_URL) to read a summary ' +
390 'of results from; defaults to %(default)s',
391 default='actual-results.json')
epoger@google.com9166bf52013-05-30 15:46:19 +0000392parser.add_argument('--subdirs', metavar='SUBDIR', nargs='+',
393 help='which platform subdirectories to rebaseline; ' +
394 'if unspecified, rebaseline all subdirs, same as ' +
395 '"--subdirs %s"' % ' '.join(sorted(SUBDIR_MAPPING.keys())))
epoger@google.com99ba65a2013-06-05 15:43:37 +0000396parser.add_argument('--tests', metavar='TEST', nargs='+',
epoger@google.com9166bf52013-05-30 15:46:19 +0000397 help='which tests to rebaseline, e.g. ' +
epoger@google.com99ba65a2013-06-05 15:43:37 +0000398 '"--tests aaclip bigmatrix"; if unspecified, then all ' +
399 'failing tests (according to the actual-results.json ' +
400 'file) will be rebaselined.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000401args = parser.parse_args()
402rebaseliner = Rebaseliner(tests=args.tests, configs=args.configs,
epoger@google.com99ba65a2013-06-05 15:43:37 +0000403 subdirs=args.subdirs, dry_run=args.dry_run,
404 json_base_url=args.json_base_url,
epoger@google.comdad53102013-06-12 14:25:30 +0000405 json_filename=args.json_filename,
406 add_new=args.add_new)
epoger@google.com9166bf52013-05-30 15:46:19 +0000407rebaseliner.RebaselineAll()