blob: 5e8e15fbad80cc972e6eaa985cc91a295460ca4c [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
19import subprocess
20import sys
epoger@google.com99ba65a2013-06-05 15:43:37 +000021import urllib2
22
23# Imports from within Skia
24#
25# Make sure that they are in the PYTHONPATH, but add them at the *end*
26# so any that are already in the PYTHONPATH will be preferred.
27GM_DIRECTORY = os.path.realpath(
28 os.path.join(os.path.dirname(os.path.dirname(__file__)), 'gm'))
29if GM_DIRECTORY not in sys.path:
30 sys.path.append(GM_DIRECTORY)
31import gm_json
32
senorblanco@chromium.org782f3b42012-10-29 18:06:26 +000033
epoger@google.comec3397b2013-05-29 17:09:43 +000034# Mapping of gm-expectations subdir (under
35# https://skia.googlecode.com/svn/gm-expected/ )
36# to builder name (see list at http://108.170.217.252:10117/builders )
epoger@google.com9166bf52013-05-30 15:46:19 +000037SUBDIR_MAPPING = {
epoger@google.comec3397b2013-05-29 17:09:43 +000038 'base-shuttle-win7-intel-float':
39 'Test-Win7-ShuttleA-HD2000-x86-Release',
40 'base-shuttle-win7-intel-angle':
41 'Test-Win7-ShuttleA-HD2000-x86-Release-ANGLE',
42 'base-shuttle-win7-intel-directwrite':
43 'Test-Win7-ShuttleA-HD2000-x86-Release-DirectWrite',
44 'base-shuttle_ubuntu12_ati5770':
45 'Test-Ubuntu12-ShuttleA-ATI5770-x86_64-Release',
46 'base-macmini':
47 'Test-Mac10.6-MacMini4.1-GeForce320M-x86-Release',
48 'base-macmini-lion-float':
49 'Test-Mac10.7-MacMini4.1-GeForce320M-x86-Release',
50 'base-android-galaxy-nexus':
51 'Test-Android-GalaxyNexus-SGX540-Arm7-Debug',
52 'base-android-nexus-7':
53 'Test-Android-Nexus7-Tegra3-Arm7-Release',
54 'base-android-nexus-s':
55 'Test-Android-NexusS-SGX540-Arm7-Release',
56 'base-android-xoom':
57 'Test-Android-Xoom-Tegra2-Arm7-Release',
58 'base-android-nexus-10':
59 'Test-Android-Nexus10-MaliT604-Arm7-Release',
60}
61
epoger@google.com9166bf52013-05-30 15:46:19 +000062
epoger@google.comdb29a312013-06-04 14:58:47 +000063class CommandFailedException(Exception):
64 pass
65
epoger@google.com9166bf52013-05-30 15:46:19 +000066class Rebaseliner(object):
67
68 # params:
epoger@google.com99ba65a2013-06-05 15:43:37 +000069 # json_base_url: base URL from which to read json_filename
70 # json_filename: filename (under json_base_url) from which to read a
71 # summary of results; typically "actual-results.json"
72 # subdirs: which platform subdirectories to rebaseline; if not specified,
epoger@google.com9166bf52013-05-30 15:46:19 +000073 # rebaseline all platform subdirectories
epoger@google.com99ba65a2013-06-05 15:43:37 +000074 # tests: list of tests to rebaseline, or None if we should rebaseline
75 # whatever files the JSON results summary file tells us to
76 # configs: which configs to run for each test; this should only be
77 # specified if the list of tests was also specified (otherwise,
78 # the JSON file will give us test names and configs)
epoger@google.com9166bf52013-05-30 15:46:19 +000079 # dry_run: if True, instead of actually downloading files or adding
80 # files to checkout, display a list of operations that
81 # we would normally perform
epoger@google.com99ba65a2013-06-05 15:43:37 +000082 def __init__(self, json_base_url, json_filename,
83 subdirs=None, tests=None, configs=None, dry_run=False):
84 if configs and not tests:
85 raise ValueError('configs should only be specified if tests ' +
86 'were specified also')
epoger@google.com9166bf52013-05-30 15:46:19 +000087 self._tests = tests
88 self._configs = configs
89 if not subdirs:
90 self._subdirs = sorted(SUBDIR_MAPPING.keys())
epoger@google.combda4e912013-06-11 16:16:02 +000091 self._missing_json_is_fatal = False
epoger@google.com9166bf52013-05-30 15:46:19 +000092 else:
93 self._subdirs = subdirs
epoger@google.combda4e912013-06-11 16:16:02 +000094 self._missing_json_is_fatal = True
epoger@google.com99ba65a2013-06-05 15:43:37 +000095 self._json_base_url = json_base_url
96 self._json_filename = json_filename
epoger@google.com9166bf52013-05-30 15:46:19 +000097 self._dry_run = dry_run
98 self._is_svn_checkout = (
99 os.path.exists('.svn') or
100 os.path.exists(os.path.join(os.pardir, '.svn')))
101 self._is_git_checkout = (
102 os.path.exists('.git') or
103 os.path.exists(os.path.join(os.pardir, '.git')))
104
epoger@google.comdb29a312013-06-04 14:58:47 +0000105 # If dry_run is False, execute subprocess.call(cmd).
106 # If dry_run is True, print the command we would have otherwise run.
107 # Raises a CommandFailedException if the command fails.
108 def _Call(self, cmd):
epoger@google.com9166bf52013-05-30 15:46:19 +0000109 if self._dry_run:
110 print '%s' % ' '.join(cmd)
epoger@google.comdb29a312013-06-04 14:58:47 +0000111 return
112 if subprocess.call(cmd) != 0:
113 raise CommandFailedException('error running command: ' +
114 ' '.join(cmd))
115
116 # Download a single file, raising a CommandFailedException if it fails.
117 def _DownloadFile(self, source_url, dest_filename):
118 # Download into a temporary file and then rename it afterwards,
119 # so that we don't corrupt the existing file if it fails midway thru.
120 temp_filename = os.path.join(os.path.dirname(dest_filename),
121 '.temp-' + os.path.basename(dest_filename))
122
123 # TODO(epoger): Replace calls to "curl"/"mv" (which will only work on
124 # Unix) with a Python HTTP library (which should work cross-platform)
125 self._Call([ 'curl', '--fail', '--silent', source_url,
126 '--output', temp_filename ])
127 self._Call([ 'mv', temp_filename, dest_filename ])
epoger@google.com9166bf52013-05-30 15:46:19 +0000128
epoger@google.com99ba65a2013-06-05 15:43:37 +0000129 # Returns the full contents of a URL, as a single string.
130 #
131 # Unlike standard URL handling, we allow relative "file:" URLs;
132 # for example, "file:one/two" resolves to the file ./one/two
133 # (relative to current working dir)
134 def _GetContentsOfUrl(self, url):
135 file_prefix = 'file:'
136 if url.startswith(file_prefix):
137 filename = url[len(file_prefix):]
138 return open(filename, 'r').read()
139 else:
140 return urllib2.urlopen(url).read()
141
142 # Returns a list of files that require rebaselining.
143 #
144 # Note that this returns a list of FILES, like this:
145 # ['imageblur_565.png', 'xfermodes_pdf.png']
146 # rather than a list of TESTS, like this:
147 # ['imageblur', 'xfermodes']
148 #
epoger@google.combda4e912013-06-11 16:16:02 +0000149 # If the JSON actual result summary file cannot be loaded, the behavior
150 # depends on self._missing_json_is_fatal:
151 # - if true: execution will halt with an exception
152 # - if false: we will log an error message but return an empty list so we
153 # go on to the next platform
154 #
epoger@google.com99ba65a2013-06-05 15:43:37 +0000155 # params:
156 # json_url: URL pointing to a JSON actual result summary file
157 #
158 # TODO(epoger): add a parameter indicating whether "no-comparison"
159 # results (those for which we don't have any expectations yet)
160 # should be rebaselined. For now, we only return failed expectations.
161 def _GetFilesToRebaseline(self, json_url):
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000162 if self._dry_run:
163 print ''
164 print '#'
epoger@google.com99ba65a2013-06-05 15:43:37 +0000165 print ('# Getting files to rebaseline from JSON summary URL %s ...'
166 % json_url)
epoger@google.combda4e912013-06-11 16:16:02 +0000167 try:
168 json_contents = self._GetContentsOfUrl(json_url)
169 except urllib2.HTTPError:
170 message = 'unable to load JSON summary URL %s' % json_url
171 if self._missing_json_is_fatal:
172 raise ValueError(message)
173 else:
174 print '# %s' % message
175 return []
176
epoger@google.com99ba65a2013-06-05 15:43:37 +0000177 json_dict = gm_json.LoadFromString(json_contents)
178 actual_results = json_dict[gm_json.JSONKEY_ACTUALRESULTS]
179
180 files_to_rebaseline = []
181 failed_results = actual_results[gm_json.JSONKEY_ACTUALRESULTS_FAILED]
182 if failed_results:
183 files_to_rebaseline.extend(failed_results.keys())
184
185 print '# ... found files_to_rebaseline %s' % files_to_rebaseline
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000186 if self._dry_run:
187 print '#'
epoger@google.com99ba65a2013-06-05 15:43:37 +0000188 return files_to_rebaseline
189
epoger@google.com9166bf52013-05-30 15:46:19 +0000190 # Rebaseline a single file.
191 def _RebaselineOneFile(self, expectations_subdir, builder_name,
192 infilename, outfilename):
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000193 if self._dry_run:
194 print ''
epoger@google.com99ba65a2013-06-05 15:43:37 +0000195 print '# ' + infilename
epoger@google.com9166bf52013-05-30 15:46:19 +0000196 url = ('http://skia-autogen.googlecode.com/svn/gm-actual/' +
197 expectations_subdir + '/' + builder_name + '/' +
198 expectations_subdir + '/' + infilename)
epoger@google.comdb29a312013-06-04 14:58:47 +0000199
200 # Try to download this file, but if that fails, keep going...
201 #
202 # This not treated as a fatal failure because not all
203 # platforms generate all configs (e.g., Android does not
204 # generate PDF).
205 #
206 # We could tweak the list of configs within this tool to
207 # reflect which combinations the bots actually generate, and
208 # then fail if any of those expected combinations are
209 # missing... but then this tool would become useless every
210 # time someone tweaked the configs on the bots without
211 # updating this script.
212 try:
213 self._DownloadFile(source_url=url, dest_filename=outfilename)
214 except CommandFailedException:
epoger@google.com9166bf52013-05-30 15:46:19 +0000215 print '# Couldn\'t fetch ' + url
216 return
epoger@google.comdb29a312013-06-04 14:58:47 +0000217
218 # Add this file to version control (if it isn't already).
epoger@google.com9166bf52013-05-30 15:46:19 +0000219 if self._is_svn_checkout:
220 cmd = [ 'svn', 'add', '--quiet', outfilename ]
221 self._Call(cmd)
222 cmd = [ 'svn', 'propset', '--quiet', 'svn:mime-type', 'image/png',
223 outfilename ];
224 self._Call(cmd)
225 elif self._is_git_checkout:
226 cmd = [ 'git', 'add', outfilename ]
227 self._Call(cmd)
228
229 # Rebaseline the given configs for a single test.
230 #
231 # params:
232 # expectations_subdir
233 # builder_name
234 # test: a single test to rebaseline
235 def _RebaselineOneTest(self, expectations_subdir, builder_name, test):
236 if self._configs:
237 configs = self._configs
238 else:
239 if (expectations_subdir == 'base-shuttle-win7-intel-angle'):
240 configs = [ 'angle', 'anglemsaa16' ]
241 else:
242 configs = [ '565', '8888', 'gpu', 'pdf', 'mesa', 'msaa16',
243 'msaa4' ]
epoger@google.comcc2e1cf2013-06-11 16:24:37 +0000244 if self._dry_run:
245 print ''
epoger@google.com9166bf52013-05-30 15:46:19 +0000246 print '# ' + expectations_subdir + ':'
247 for config in configs:
248 infilename = test + '_' + config + '.png'
epoger@google.com9166bf52013-05-30 15:46:19 +0000249 outfilename = os.path.join(expectations_subdir, infilename);
250 self._RebaselineOneFile(expectations_subdir=expectations_subdir,
251 builder_name=builder_name,
252 infilename=infilename,
253 outfilename=outfilename)
254
255 # Rebaseline all platforms/tests/types we specified in the constructor.
256 def RebaselineAll(self):
epoger@google.com99ba65a2013-06-05 15:43:37 +0000257 for subdir in self._subdirs:
258 if not subdir in SUBDIR_MAPPING.keys():
259 raise Exception(('unrecognized platform subdir "%s"; ' +
260 'should be one of %s') % (
261 subdir, SUBDIR_MAPPING.keys()))
262 builder_name = SUBDIR_MAPPING[subdir]
263 if self._tests:
264 for test in self._tests:
265 self._RebaselineOneTest(expectations_subdir=subdir,
266 builder_name=builder_name,
267 test=test)
268 else: # get the raw list of files that need rebaselining from JSON
269 json_url = '/'.join([self._json_base_url,
270 subdir, builder_name, subdir,
271 self._json_filename])
272 filenames = self._GetFilesToRebaseline(json_url=json_url)
273 for filename in filenames:
274 outfilename = os.path.join(subdir, filename);
275 self._RebaselineOneFile(expectations_subdir=subdir,
276 builder_name=builder_name,
277 infilename=filename,
278 outfilename=outfilename)
epoger@google.comec3397b2013-05-29 17:09:43 +0000279
epoger@google.com9166bf52013-05-30 15:46:19 +0000280# main...
epoger@google.comec3397b2013-05-29 17:09:43 +0000281
epoger@google.com9166bf52013-05-30 15:46:19 +0000282parser = argparse.ArgumentParser()
283parser.add_argument('--configs', metavar='CONFIG', nargs='+',
284 help='which configurations to rebaseline, e.g. ' +
285 '"--configs 565 8888"; if unspecified, run a default ' +
epoger@google.com99ba65a2013-06-05 15:43:37 +0000286 'set of configs. This should ONLY be specified if ' +
287 '--tests has also been specified.')
epoger@google.com82f31782013-06-11 15:45:46 +0000288parser.add_argument('--dry-run', action='store_true',
epoger@google.com9166bf52013-05-30 15:46:19 +0000289 help='instead of actually downloading files or adding ' +
290 'files to checkout, display a list of operations that ' +
291 'we would normally perform')
epoger@google.com82f31782013-06-11 15:45:46 +0000292parser.add_argument('--json-base-url',
epoger@google.com99ba65a2013-06-05 15:43:37 +0000293 help='base URL from which to read JSON_FILENAME ' +
294 'files; defaults to %(default)s',
295 default='http://skia-autogen.googlecode.com/svn/gm-actual')
epoger@google.com82f31782013-06-11 15:45:46 +0000296parser.add_argument('--json-filename',
epoger@google.com99ba65a2013-06-05 15:43:37 +0000297 help='filename (under JSON_BASE_URL) to read a summary ' +
298 'of results from; defaults to %(default)s',
299 default='actual-results.json')
epoger@google.com9166bf52013-05-30 15:46:19 +0000300parser.add_argument('--subdirs', metavar='SUBDIR', nargs='+',
301 help='which platform subdirectories to rebaseline; ' +
302 'if unspecified, rebaseline all subdirs, same as ' +
303 '"--subdirs %s"' % ' '.join(sorted(SUBDIR_MAPPING.keys())))
epoger@google.com99ba65a2013-06-05 15:43:37 +0000304parser.add_argument('--tests', metavar='TEST', nargs='+',
epoger@google.com9166bf52013-05-30 15:46:19 +0000305 help='which tests to rebaseline, e.g. ' +
epoger@google.com99ba65a2013-06-05 15:43:37 +0000306 '"--tests aaclip bigmatrix"; if unspecified, then all ' +
307 'failing tests (according to the actual-results.json ' +
308 'file) will be rebaselined.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000309args = parser.parse_args()
310rebaseliner = Rebaseliner(tests=args.tests, configs=args.configs,
epoger@google.com99ba65a2013-06-05 15:43:37 +0000311 subdirs=args.subdirs, dry_run=args.dry_run,
312 json_base_url=args.json_base_url,
313 json_filename=args.json_filename)
epoger@google.com9166bf52013-05-30 15:46:19 +0000314rebaseliner.RebaselineAll()