blob: e270e901d70504501621afc102c6cf83096d2b98 [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
epoger@google.combd4af3a2013-06-12 19:07:00 +0000143 except ValueError as e:
144 print '# ValueError reading filename %s from all_results dict: %s'%(
145 infilename, e)
146 return False
epoger@google.come78d2072013-06-12 17:44:14 +0000147 url = '%s/%s/%s/%s.png' % (self._googlestorage_gm_actuals_root,
148 hash_type, test_name, hash_value)
149 try:
150 self._DownloadFile(source_url=url, dest_filename=outfilename)
151 return True
152 except CommandFailedException:
153 print '# Couldn\'t fetch gs_url %s' % url
154 return False
155
156 # Download a single actual result from skia-autogen, returning True if it
157 # succeeded.
158 def _DownloadFromAutogen(self, infilename, outfilename,
159 expectations_subdir, builder_name):
160 url = ('http://skia-autogen.googlecode.com/svn/gm-actual/' +
161 expectations_subdir + '/' + builder_name + '/' +
162 expectations_subdir + '/' + infilename)
163 try:
164 self._DownloadFile(source_url=url, dest_filename=outfilename)
165 return True
166 except CommandFailedException:
167 print '# Couldn\'t fetch autogen_url %s' % url
168 return False
169
epoger@google.comdb29a312013-06-04 14:58:47 +0000170 # Download a single file, raising a CommandFailedException if it fails.
171 def _DownloadFile(self, source_url, dest_filename):
172 # Download into a temporary file and then rename it afterwards,
173 # so that we don't corrupt the existing file if it fails midway thru.
174 temp_filename = os.path.join(os.path.dirname(dest_filename),
175 '.temp-' + os.path.basename(dest_filename))
176
177 # TODO(epoger): Replace calls to "curl"/"mv" (which will only work on
178 # Unix) with a Python HTTP library (which should work cross-platform)
179 self._Call([ 'curl', '--fail', '--silent', source_url,
180 '--output', temp_filename ])
181 self._Call([ 'mv', temp_filename, dest_filename ])
epoger@google.com9166bf52013-05-30 15:46:19 +0000182
epoger@google.com99ba65a2013-06-05 15:43:37 +0000183 # Returns the full contents of a URL, as a single string.
184 #
185 # Unlike standard URL handling, we allow relative "file:" URLs;
186 # for example, "file:one/two" resolves to the file ./one/two
187 # (relative to current working dir)
188 def _GetContentsOfUrl(self, url):
189 file_prefix = 'file:'
190 if url.startswith(file_prefix):
191 filename = url[len(file_prefix):]
192 return open(filename, 'r').read()
193 else:
194 return urllib2.urlopen(url).read()
195
epoger@google.come78d2072013-06-12 17:44:14 +0000196 # Returns a dictionary of actual results from actual-results.json file.
197 #
198 # The dictionary returned has this format:
199 # {
200 # u'imageblur_565.png': [u'bitmap-64bitMD5', 3359963596899141322],
201 # u'imageblur_8888.png': [u'bitmap-64bitMD5', 4217923806027861152],
202 # u'shadertext3_8888.png': [u'bitmap-64bitMD5', 3713708307125704716]
203 # }
204 #
205 # If the JSON actual result summary file cannot be loaded, the behavior
206 # depends on self._missing_json_is_fatal:
207 # - if true: execution will halt with an exception
208 # - if false: we will log an error message but return an empty dictionary
209 #
210 # params:
211 # json_url: URL pointing to a JSON actual result summary file
212 # sections: a list of section names to include in the results, e.g.
213 # [gm_json.JSONKEY_ACTUALRESULTS_FAILED,
214 # gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON] ;
215 # if None, then include ALL sections.
216 def _GetActualResults(self, json_url, sections=None):
217 try:
218 json_contents = self._GetContentsOfUrl(json_url)
219 except (urllib2.HTTPError, IOError):
220 message = 'unable to load JSON summary URL %s' % json_url
221 if self._missing_json_is_fatal:
222 raise ValueError(message)
223 else:
224 print '# %s' % message
225 return {}
226
227 json_dict = gm_json.LoadFromString(json_contents)
228 results_to_return = {}
229 actual_results = json_dict[gm_json.JSONKEY_ACTUALRESULTS]
230 if not sections:
231 sections = actual_results.keys()
232 for section in sections:
233 section_results = actual_results[section]
234 if section_results:
235 results_to_return.update(section_results)
236 return results_to_return
237
epoger@google.com99ba65a2013-06-05 15:43:37 +0000238 # Returns a list of files that require rebaselining.
239 #
240 # Note that this returns a list of FILES, like this:
241 # ['imageblur_565.png', 'xfermodes_pdf.png']
242 # rather than a list of TESTS, like this:
243 # ['imageblur', 'xfermodes']
244 #
245 # params:
246 # json_url: URL pointing to a JSON actual result summary file
epoger@google.comdad53102013-06-12 14:25:30 +0000247 # add_new: if True, then return files listed in any of these sections:
248 # - JSONKEY_ACTUALRESULTS_FAILED
249 # - JSONKEY_ACTUALRESULTS_NOCOMPARISON
250 # if False, then return files listed in these sections:
251 # - JSONKEY_ACTUALRESULTS_FAILED
epoger@google.com99ba65a2013-06-05 15:43:37 +0000252 #
epoger@google.comdad53102013-06-12 14:25:30 +0000253 def _GetFilesToRebaseline(self, json_url, add_new):
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000254 if self._dry_run:
255 print ''
256 print '#'
epoger@google.com99ba65a2013-06-05 15:43:37 +0000257 print ('# Getting files to rebaseline from JSON summary URL %s ...'
258 % json_url)
epoger@google.comdad53102013-06-12 14:25:30 +0000259 sections = [gm_json.JSONKEY_ACTUALRESULTS_FAILED]
260 if add_new:
261 sections.append(gm_json.JSONKEY_ACTUALRESULTS_NOCOMPARISON)
epoger@google.come78d2072013-06-12 17:44:14 +0000262 results_to_rebaseline = self._GetActualResults(json_url=json_url,
263 sections=sections)
264 files_to_rebaseline = results_to_rebaseline.keys()
265 files_to_rebaseline.sort()
epoger@google.com99ba65a2013-06-05 15:43:37 +0000266 print '# ... found files_to_rebaseline %s' % files_to_rebaseline
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000267 if self._dry_run:
268 print '#'
epoger@google.com99ba65a2013-06-05 15:43:37 +0000269 return files_to_rebaseline
270
epoger@google.com9166bf52013-05-30 15:46:19 +0000271 # Rebaseline a single file.
272 def _RebaselineOneFile(self, expectations_subdir, builder_name,
epoger@google.come78d2072013-06-12 17:44:14 +0000273 infilename, outfilename, all_results):
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000274 if self._dry_run:
275 print ''
epoger@google.com99ba65a2013-06-05 15:43:37 +0000276 print '# ' + infilename
epoger@google.comdb29a312013-06-04 14:58:47 +0000277
epoger@google.come78d2072013-06-12 17:44:14 +0000278 # First try to download this result image from Google Storage.
279 # If that fails, try skia-autogen.
280 # If that fails too, just go on to the next file.
epoger@google.comdb29a312013-06-04 14:58:47 +0000281 #
282 # This not treated as a fatal failure because not all
283 # platforms generate all configs (e.g., Android does not
284 # generate PDF).
285 #
epoger@google.come78d2072013-06-12 17:44:14 +0000286 # TODO(epoger): Once we are downloading only files that the
287 # actual-results.json file told us to, this should become a
288 # fatal error. (If the actual-results.json file told us that
289 # the test failed with XXX results, we should be able to download
290 # those results every time.)
291 if not self._DownloadFromGoogleStorage(infilename=infilename,
292 outfilename=outfilename,
293 all_results=all_results):
294 if not self._DownloadFromAutogen(infilename=infilename,
295 outfilename=outfilename,
296 expectations_subdir=expectations_subdir,
297 builder_name=builder_name):
298 print '# Couldn\'t fetch infilename ' + infilename
299 return
epoger@google.comdb29a312013-06-04 14:58:47 +0000300
epoger@google.comdad53102013-06-12 14:25:30 +0000301 # Add this file to version control (if appropriate).
302 if self._add_new:
303 if self._is_svn_checkout:
304 cmd = [ 'svn', 'add', '--quiet', outfilename ]
305 self._Call(cmd)
306 cmd = [ 'svn', 'propset', '--quiet', 'svn:mime-type',
307 'image/png', outfilename ];
308 self._Call(cmd)
309 elif self._is_git_checkout:
310 cmd = [ 'git', 'add', outfilename ]
311 self._Call(cmd)
epoger@google.com9166bf52013-05-30 15:46:19 +0000312
313 # Rebaseline the given configs for a single test.
314 #
315 # params:
316 # expectations_subdir
317 # builder_name
318 # test: a single test to rebaseline
epoger@google.come78d2072013-06-12 17:44:14 +0000319 # all_results: a dictionary of all actual results
320 def _RebaselineOneTest(self, expectations_subdir, builder_name, test,
321 all_results):
epoger@google.com9166bf52013-05-30 15:46:19 +0000322 if self._configs:
323 configs = self._configs
324 else:
325 if (expectations_subdir == 'base-shuttle-win7-intel-angle'):
326 configs = [ 'angle', 'anglemsaa16' ]
327 else:
328 configs = [ '565', '8888', 'gpu', 'pdf', 'mesa', 'msaa16',
329 'msaa4' ]
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000330 if self._dry_run:
331 print ''
epoger@google.com9166bf52013-05-30 15:46:19 +0000332 print '# ' + expectations_subdir + ':'
333 for config in configs:
334 infilename = test + '_' + config + '.png'
epoger@google.com9166bf52013-05-30 15:46:19 +0000335 outfilename = os.path.join(expectations_subdir, infilename);
336 self._RebaselineOneFile(expectations_subdir=expectations_subdir,
337 builder_name=builder_name,
338 infilename=infilename,
epoger@google.come78d2072013-06-12 17:44:14 +0000339 outfilename=outfilename,
340 all_results=all_results)
epoger@google.com9166bf52013-05-30 15:46:19 +0000341
342 # Rebaseline all platforms/tests/types we specified in the constructor.
343 def RebaselineAll(self):
epoger@google.com99ba65a2013-06-05 15:43:37 +0000344 for subdir in self._subdirs:
345 if not subdir in SUBDIR_MAPPING.keys():
346 raise Exception(('unrecognized platform subdir "%s"; ' +
347 'should be one of %s') % (
348 subdir, SUBDIR_MAPPING.keys()))
349 builder_name = SUBDIR_MAPPING[subdir]
epoger@google.come78d2072013-06-12 17:44:14 +0000350 json_url = '/'.join([self._json_base_url,
351 subdir, builder_name, subdir,
352 self._json_filename])
353 all_results = self._GetActualResults(json_url=json_url)
354
epoger@google.com99ba65a2013-06-05 15:43:37 +0000355 if self._tests:
356 for test in self._tests:
357 self._RebaselineOneTest(expectations_subdir=subdir,
358 builder_name=builder_name,
epoger@google.come78d2072013-06-12 17:44:14 +0000359 test=test, all_results=all_results)
epoger@google.com99ba65a2013-06-05 15:43:37 +0000360 else: # get the raw list of files that need rebaselining from JSON
epoger@google.comdad53102013-06-12 14:25:30 +0000361 filenames = self._GetFilesToRebaseline(json_url=json_url,
362 add_new=self._add_new)
epoger@google.com99ba65a2013-06-05 15:43:37 +0000363 for filename in filenames:
364 outfilename = os.path.join(subdir, filename);
365 self._RebaselineOneFile(expectations_subdir=subdir,
366 builder_name=builder_name,
367 infilename=filename,
epoger@google.come78d2072013-06-12 17:44:14 +0000368 outfilename=outfilename,
369 all_results=all_results)
epoger@google.comec3397b2013-05-29 17:09:43 +0000370
epoger@google.com9166bf52013-05-30 15:46:19 +0000371# main...
epoger@google.comec3397b2013-05-29 17:09:43 +0000372
epoger@google.com9166bf52013-05-30 15:46:19 +0000373parser = argparse.ArgumentParser()
epoger@google.comdad53102013-06-12 14:25:30 +0000374parser.add_argument('--add-new', action='store_true',
375 help='in addition to the standard behavior of ' +
376 'updating expectations for failing tests, add ' +
377 'expectations for tests which don\'t have expectations ' +
378 'yet.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000379parser.add_argument('--configs', metavar='CONFIG', nargs='+',
380 help='which configurations to rebaseline, e.g. ' +
381 '"--configs 565 8888"; if unspecified, run a default ' +
epoger@google.com99ba65a2013-06-05 15:43:37 +0000382 'set of configs. This should ONLY be specified if ' +
383 '--tests has also been specified.')
epoger@google.com82f31782013-06-11 15:45:46 +0000384parser.add_argument('--dry-run', action='store_true',
epoger@google.com9166bf52013-05-30 15:46:19 +0000385 help='instead of actually downloading files or adding ' +
386 'files to checkout, display a list of operations that ' +
387 'we would normally perform')
epoger@google.com82f31782013-06-11 15:45:46 +0000388parser.add_argument('--json-base-url',
epoger@google.com99ba65a2013-06-05 15:43:37 +0000389 help='base URL from which to read JSON_FILENAME ' +
390 'files; defaults to %(default)s',
391 default='http://skia-autogen.googlecode.com/svn/gm-actual')
epoger@google.com82f31782013-06-11 15:45:46 +0000392parser.add_argument('--json-filename',
epoger@google.com99ba65a2013-06-05 15:43:37 +0000393 help='filename (under JSON_BASE_URL) to read a summary ' +
394 'of results from; defaults to %(default)s',
395 default='actual-results.json')
epoger@google.com9166bf52013-05-30 15:46:19 +0000396parser.add_argument('--subdirs', metavar='SUBDIR', nargs='+',
397 help='which platform subdirectories to rebaseline; ' +
398 'if unspecified, rebaseline all subdirs, same as ' +
399 '"--subdirs %s"' % ' '.join(sorted(SUBDIR_MAPPING.keys())))
epoger@google.com99ba65a2013-06-05 15:43:37 +0000400parser.add_argument('--tests', metavar='TEST', nargs='+',
epoger@google.com9166bf52013-05-30 15:46:19 +0000401 help='which tests to rebaseline, e.g. ' +
epoger@google.com99ba65a2013-06-05 15:43:37 +0000402 '"--tests aaclip bigmatrix"; if unspecified, then all ' +
403 'failing tests (according to the actual-results.json ' +
404 'file) will be rebaselined.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000405args = parser.parse_args()
406rebaseliner = Rebaseliner(tests=args.tests, configs=args.configs,
epoger@google.com99ba65a2013-06-05 15:43:37 +0000407 subdirs=args.subdirs, dry_run=args.dry_run,
408 json_base_url=args.json_base_url,
epoger@google.comdad53102013-06-12 14:25:30 +0000409 json_filename=args.json_filename,
410 add_new=args.add_new)
epoger@google.com9166bf52013-05-30 15:46:19 +0000411rebaseliner.RebaselineAll()