blob: 4deeb350f15115bf84a7b71957a8d840ef2ca611 [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):
162 print ('# Getting files to rebaseline from JSON summary URL %s ...'
163 % json_url)
epoger@google.combda4e912013-06-11 16:16:02 +0000164 try:
165 json_contents = self._GetContentsOfUrl(json_url)
166 except urllib2.HTTPError:
167 message = 'unable to load JSON summary URL %s' % json_url
168 if self._missing_json_is_fatal:
169 raise ValueError(message)
170 else:
171 print '# %s' % message
172 return []
173
epoger@google.com99ba65a2013-06-05 15:43:37 +0000174 json_dict = gm_json.LoadFromString(json_contents)
175 actual_results = json_dict[gm_json.JSONKEY_ACTUALRESULTS]
176
177 files_to_rebaseline = []
178 failed_results = actual_results[gm_json.JSONKEY_ACTUALRESULTS_FAILED]
179 if failed_results:
180 files_to_rebaseline.extend(failed_results.keys())
181
182 print '# ... found files_to_rebaseline %s' % files_to_rebaseline
183 return files_to_rebaseline
184
epoger@google.com9166bf52013-05-30 15:46:19 +0000185 # Rebaseline a single file.
186 def _RebaselineOneFile(self, expectations_subdir, builder_name,
187 infilename, outfilename):
epoger@google.com99ba65a2013-06-05 15:43:37 +0000188 print '# ' + infilename
epoger@google.com9166bf52013-05-30 15:46:19 +0000189 url = ('http://skia-autogen.googlecode.com/svn/gm-actual/' +
190 expectations_subdir + '/' + builder_name + '/' +
191 expectations_subdir + '/' + infilename)
epoger@google.comdb29a312013-06-04 14:58:47 +0000192
193 # Try to download this file, but if that fails, keep going...
194 #
195 # This not treated as a fatal failure because not all
196 # platforms generate all configs (e.g., Android does not
197 # generate PDF).
198 #
199 # We could tweak the list of configs within this tool to
200 # reflect which combinations the bots actually generate, and
201 # then fail if any of those expected combinations are
202 # missing... but then this tool would become useless every
203 # time someone tweaked the configs on the bots without
204 # updating this script.
205 try:
206 self._DownloadFile(source_url=url, dest_filename=outfilename)
207 except CommandFailedException:
epoger@google.com9166bf52013-05-30 15:46:19 +0000208 print '# Couldn\'t fetch ' + url
209 return
epoger@google.comdb29a312013-06-04 14:58:47 +0000210
211 # Add this file to version control (if it isn't already).
epoger@google.com9166bf52013-05-30 15:46:19 +0000212 if self._is_svn_checkout:
213 cmd = [ 'svn', 'add', '--quiet', outfilename ]
214 self._Call(cmd)
215 cmd = [ 'svn', 'propset', '--quiet', 'svn:mime-type', 'image/png',
216 outfilename ];
217 self._Call(cmd)
218 elif self._is_git_checkout:
219 cmd = [ 'git', 'add', outfilename ]
220 self._Call(cmd)
221
222 # Rebaseline the given configs for a single test.
223 #
224 # params:
225 # expectations_subdir
226 # builder_name
227 # test: a single test to rebaseline
228 def _RebaselineOneTest(self, expectations_subdir, builder_name, test):
229 if self._configs:
230 configs = self._configs
231 else:
232 if (expectations_subdir == 'base-shuttle-win7-intel-angle'):
233 configs = [ 'angle', 'anglemsaa16' ]
234 else:
235 configs = [ '565', '8888', 'gpu', 'pdf', 'mesa', 'msaa16',
236 'msaa4' ]
237 print '# ' + expectations_subdir + ':'
238 for config in configs:
239 infilename = test + '_' + config + '.png'
epoger@google.com9166bf52013-05-30 15:46:19 +0000240 outfilename = os.path.join(expectations_subdir, infilename);
241 self._RebaselineOneFile(expectations_subdir=expectations_subdir,
242 builder_name=builder_name,
243 infilename=infilename,
244 outfilename=outfilename)
245
246 # Rebaseline all platforms/tests/types we specified in the constructor.
247 def RebaselineAll(self):
epoger@google.com99ba65a2013-06-05 15:43:37 +0000248 for subdir in self._subdirs:
249 if not subdir in SUBDIR_MAPPING.keys():
250 raise Exception(('unrecognized platform subdir "%s"; ' +
251 'should be one of %s') % (
252 subdir, SUBDIR_MAPPING.keys()))
253 builder_name = SUBDIR_MAPPING[subdir]
254 if self._tests:
255 for test in self._tests:
256 self._RebaselineOneTest(expectations_subdir=subdir,
257 builder_name=builder_name,
258 test=test)
259 else: # get the raw list of files that need rebaselining from JSON
260 json_url = '/'.join([self._json_base_url,
261 subdir, builder_name, subdir,
262 self._json_filename])
263 filenames = self._GetFilesToRebaseline(json_url=json_url)
264 for filename in filenames:
265 outfilename = os.path.join(subdir, filename);
266 self._RebaselineOneFile(expectations_subdir=subdir,
267 builder_name=builder_name,
268 infilename=filename,
269 outfilename=outfilename)
epoger@google.comec3397b2013-05-29 17:09:43 +0000270
epoger@google.com9166bf52013-05-30 15:46:19 +0000271# main...
epoger@google.comec3397b2013-05-29 17:09:43 +0000272
epoger@google.com9166bf52013-05-30 15:46:19 +0000273parser = argparse.ArgumentParser()
274parser.add_argument('--configs', metavar='CONFIG', nargs='+',
275 help='which configurations to rebaseline, e.g. ' +
276 '"--configs 565 8888"; if unspecified, run a default ' +
epoger@google.com99ba65a2013-06-05 15:43:37 +0000277 'set of configs. This should ONLY be specified if ' +
278 '--tests has also been specified.')
epoger@google.com82f31782013-06-11 15:45:46 +0000279parser.add_argument('--dry-run', action='store_true',
epoger@google.com9166bf52013-05-30 15:46:19 +0000280 help='instead of actually downloading files or adding ' +
281 'files to checkout, display a list of operations that ' +
282 'we would normally perform')
epoger@google.com82f31782013-06-11 15:45:46 +0000283parser.add_argument('--json-base-url',
epoger@google.com99ba65a2013-06-05 15:43:37 +0000284 help='base URL from which to read JSON_FILENAME ' +
285 'files; defaults to %(default)s',
286 default='http://skia-autogen.googlecode.com/svn/gm-actual')
epoger@google.com82f31782013-06-11 15:45:46 +0000287parser.add_argument('--json-filename',
epoger@google.com99ba65a2013-06-05 15:43:37 +0000288 help='filename (under JSON_BASE_URL) to read a summary ' +
289 'of results from; defaults to %(default)s',
290 default='actual-results.json')
epoger@google.com9166bf52013-05-30 15:46:19 +0000291parser.add_argument('--subdirs', metavar='SUBDIR', nargs='+',
292 help='which platform subdirectories to rebaseline; ' +
293 'if unspecified, rebaseline all subdirs, same as ' +
294 '"--subdirs %s"' % ' '.join(sorted(SUBDIR_MAPPING.keys())))
epoger@google.com99ba65a2013-06-05 15:43:37 +0000295parser.add_argument('--tests', metavar='TEST', nargs='+',
epoger@google.com9166bf52013-05-30 15:46:19 +0000296 help='which tests to rebaseline, e.g. ' +
epoger@google.com99ba65a2013-06-05 15:43:37 +0000297 '"--tests aaclip bigmatrix"; if unspecified, then all ' +
298 'failing tests (according to the actual-results.json ' +
299 'file) will be rebaselined.')
epoger@google.com9166bf52013-05-30 15:46:19 +0000300args = parser.parse_args()
301rebaseliner = Rebaseliner(tests=args.tests, configs=args.configs,
epoger@google.com99ba65a2013-06-05 15:43:37 +0000302 subdirs=args.subdirs, dry_run=args.dry_run,
303 json_base_url=args.json_base_url,
304 json_filename=args.json_filename)
epoger@google.com9166bf52013-05-30 15:46:19 +0000305rebaseliner.RebaselineAll()