blob: 14035403a286d14fd89f692645a78c1d52797d48 [file] [log] [blame]
zachr@google.com48b88912013-07-25 19:49:17 +00001#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4from __future__ import print_function
zachr@google.com785ef5d2013-07-26 19:29:39 +00005import argparse
zachr@google.com48b88912013-07-25 19:49:17 +00006import BaseHTTPServer
zachr@google.com785ef5d2013-07-26 19:29:39 +00007import json
zachr@google.com48b88912013-07-25 19:49:17 +00008import os
9import os.path
zachr@google.com785ef5d2013-07-26 19:29:39 +000010import re
zachr@google.com6f8e2c52013-08-07 15:43:04 +000011import subprocess
zachr@google.com785ef5d2013-07-26 19:29:39 +000012import sys
13import tempfile
14import urllib2
15
16# Grab the script path because that is where all the static assets are
17SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
18
19# Find the tools directory for python imports
20TOOLS_DIR = os.path.dirname(SCRIPT_DIR)
21
22# Find the root of the skia trunk for finding skpdiff binary
23SKIA_ROOT_DIR = os.path.dirname(TOOLS_DIR)
24
25# Find the default location of gm expectations
26DEFAULT_GM_EXPECTATIONS_DIR = os.path.join(SKIA_ROOT_DIR, 'expectations', 'gm')
27
28# Imports from within Skia
29if TOOLS_DIR not in sys.path:
30 sys.path.append(TOOLS_DIR)
31GM_DIR = os.path.join(SKIA_ROOT_DIR, 'gm')
32if GM_DIR not in sys.path:
33 sys.path.append(GM_DIR)
34import gm_json
35import jsondiff
zachr@google.com48b88912013-07-25 19:49:17 +000036
37# A simple dictionary of file name extensions to MIME types. The empty string
38# entry is used as the default when no extension was given or if the extension
39# has no entry in this dictionary.
40MIME_TYPE_MAP = {'': 'application/octet-stream',
41 'html': 'text/html',
42 'css': 'text/css',
43 'png': 'image/png',
zachr@google.com785ef5d2013-07-26 19:29:39 +000044 'js': 'application/javascript',
45 'json': 'application/json'
zachr@google.com48b88912013-07-25 19:49:17 +000046 }
47
48
zachr@google.com785ef5d2013-07-26 19:29:39 +000049IMAGE_FILENAME_RE = re.compile(gm_json.IMAGE_FILENAME_PATTERN)
50
51SKPDIFF_INVOKE_FORMAT = '{} --jsonp=false -o {} -f {} {}'
52
53
54def get_skpdiff_path(user_path=None):
55 """Find the skpdiff binary.
56
57 @param user_path If none, searches in Release and Debug out directories of
58 the skia root. If set, checks that the path is a real file and
59 returns it.
60 """
61 skpdiff_path = None
62 possible_paths = []
63
64 # Use the user given path, or try out some good default paths.
65 if user_path:
66 possible_paths.append(user_path)
67 else:
68 possible_paths.append(os.path.join(SKIA_ROOT_DIR, 'out',
69 'Release', 'skpdiff'))
70 possible_paths.append(os.path.join(SKIA_ROOT_DIR, 'out',
71 'Debug', 'skpdiff'))
72 # Use the first path that actually points to the binary
73 for possible_path in possible_paths:
74 if os.path.isfile(possible_path):
75 skpdiff_path = possible_path
76 break
77
78 # If skpdiff was not found, print out diagnostic info for the user.
79 if skpdiff_path is None:
80 print('Could not find skpdiff binary. Either build it into the ' +
81 'default directory, or specify the path on the command line.')
82 print('skpdiff paths tried:')
83 for possible_path in possible_paths:
84 print(' ', possible_path)
85 return skpdiff_path
86
87
88def download_file(url, output_path):
89 """Download the file at url and place it in output_path"""
90 reader = urllib2.urlopen(url)
91 with open(output_path, 'wb') as writer:
92 writer.write(reader.read())
93
94
95def download_gm_image(image_name, image_path, hash_val):
96 """Download the gm result into the given path.
97
98 @param image_name The GM file name, for example imageblur_gpu.png.
99 @param image_path Path to place the image.
100 @param hash_val The hash value of the image.
101 """
102
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000103 # Separate the test name from a image name
zachr@google.com785ef5d2013-07-26 19:29:39 +0000104 image_match = IMAGE_FILENAME_RE.match(image_name)
105 test_name = image_match.group(1)
106
107 # Calculate the URL of the requested image
108 image_url = gm_json.CreateGmActualUrl(
109 test_name, gm_json.JSONKEY_HASHTYPE_BITMAP_64BITMD5, hash_val)
110
111 # Download the image as requested
112 download_file(image_url, image_path)
113
114
zachr@google.com785ef5d2013-07-26 19:29:39 +0000115def get_image_set_from_skpdiff(skpdiff_records):
116 """Get the set of all images references in the given records.
117
118 @param skpdiff_records An array of records, which are dictionary objects.
119 """
120 expected_set = frozenset([r['baselinePath'] for r in skpdiff_records])
121 actual_set = frozenset([r['testPath'] for r in skpdiff_records])
122 return expected_set | actual_set
123
124
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000125def set_expected_hash_in_json(expected_results_json, image_name, hash_value):
126 """Set the expected hash for the object extracted from
127 expected-results.json. Note that this only work with bitmap-64bitMD5 hash
128 types.
129
130 @param expected_results_json The Python dictionary with the results to
131 modify.
132 @param image_name The name of the image to set the hash of.
133 @param hash_value The hash to set for the image.
134 """
135 expected_results = expected_results_json[gm_json.JSONKEY_EXPECTEDRESULTS]
136
137 if image_name in expected_results:
138 expected_results[image_name][gm_json.JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS][0][1] = hash_value
139 else:
140 expected_results[image_name] = {
141 gm_json.JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS:
142 [
143 [
144 gm_json.JSONKEY_HASHTYPE_BITMAP_64BITMD5,
145 hash_value
146 ]
147 ]
148 }
149
150
151def get_head_version(path):
152 """Get the version of the file at the given path stored inside the HEAD of
153 the git repository. It is returned as a string.
154
155 @param path The path of the file whose HEAD is returned. It is assumed the
156 path is inside a git repo rooted at SKIA_ROOT_DIR.
157 """
158
159 # git-show will not work with absolute paths. This ensures we give it a path
160 # relative to the skia root.
161 git_path = os.path.relpath(path, SKIA_ROOT_DIR)
162 git_show_proc = subprocess.Popen(['git', 'show', 'HEAD:' + git_path],
163 stdout=subprocess.PIPE)
164
165 # When invoked outside a shell, git will output the last committed version
166 # of the file directly to stdout.
167 git_version_content, _ = git_show_proc.communicate()
168 return git_version_content
169
170
171class GMInstance:
172 """Information about a GM test result on a specific device:
173 - device_name = the name of the device that rendered it
174 - image_name = the GM test name and config
175 - expected_hash = the current expected hash value
176 - actual_hash = the actual hash value
177 """
178 def __init__(self,
179 device_name, image_name,
180 expected_hash, actual_hash):
181 self.device_name = device_name
182 self.image_name = image_name
183 self.expected_hash = expected_hash
184 self.actual_hash = actual_hash
185
186
187class ExpectationsManager:
188 def __init__(self, expectations_dir, expected_name, updated_name,
189 skpdiff_path):
190 """
191 @param expectations_dir The directory to traverse for results files.
192 This should resemble expectations/gm in the Skia trunk.
193 @param expected_name The name of the expected result files. These
194 are in the format of expected-results.json.
195 @param updated_name The name of the updated expected result files.
196 Normally this matches --expectations-filename-output for the
197 rebaseline.py tool.
198 @param skpdiff_path The path used to execute the skpdiff command.
199 """
200 self._expectations_dir = expectations_dir
201 self._expected_name = expected_name
202 self._updated_name = updated_name
203 self._skpdiff_path = skpdiff_path
204 self._generate_gm_comparison()
205
206 def _generate_gm_comparison(self):
207 """Generate all the data needed to compare GMs:
208 - determine which GMs changed
209 - download the changed images
210 - compare them with skpdiff
211 """
212
213 # Get the expectations and compare them with actual hashes
214 self._get_expectations()
215
216
217 # Create a temporary file tree that makes sense for skpdiff to operate
218 # on.
219 image_output_dir = tempfile.mkdtemp('skpdiff')
220 expected_image_dir = os.path.join(image_output_dir, 'expected')
221 actual_image_dir = os.path.join(image_output_dir, 'actual')
222 os.mkdir(expected_image_dir)
223 os.mkdir(actual_image_dir)
224
225 # Download expected and actual images that differed into the temporary
226 # file tree.
227 self._download_expectation_images(expected_image_dir, actual_image_dir)
228
229 # Invoke skpdiff with our downloaded images and place its results in the
230 # temporary directory.
231 self.skpdiff_output_path = os.path.join(image_output_dir,
232 'skpdiff_output.json')
233 skpdiff_cmd = SKPDIFF_INVOKE_FORMAT.format(self._skpdiff_path,
234 self.skpdiff_output_path,
235 expected_image_dir,
236 actual_image_dir)
237 os.system(skpdiff_cmd)
238
239
240 def _get_expectations(self):
241 """Fills self._expectations with GMInstance objects for each test whose
242 expectation is different between the following two files:
243 - the local filesystem's updated results file
244 - git's head version of the expected results file
245 """
246 differ = jsondiff.GMDiffer()
247 self._expectations = []
248 for root, dirs, files in os.walk(self._expectations_dir):
249 for expectation_file in files:
250 # There are many files in the expectations directory. We only
251 # care about expected results.
252 if expectation_file != self._expected_name:
253 continue
254
255 # Get the name of the results file, and be sure there is an
256 # updated result to compare against. If there is not, there is
257 # no point in diffing this device.
258 expected_file_path = os.path.join(root, self._expected_name)
259 updated_file_path = os.path.join(root, self._updated_name)
260 if not os.path.isfile(updated_file_path):
261 continue
262
263 # Always get the expected results from git because we may have
264 # changed them in a previous instance of the server.
265 expected_contents = get_head_version(expected_file_path)
266 updated_contents = None
267 with open(updated_file_path, 'rb') as updated_file:
268 updated_contents = updated_file.read()
269
270 # Find all expectations that did not match.
271 expected_diff = differ.GenerateDiffDictFromStrings(
272 expected_contents,
273 updated_contents)
274
275 # The name of the device corresponds to the name of the folder
276 # we are in.
277 device_name = os.path.basename(root)
278
279 # Store old and new versions of the expectation for each GM
280 for image_name, hashes in expected_diff.iteritems():
281 self._expectations.append(
282 GMInstance(device_name, image_name,
283 hashes['old'], hashes['new']))
284
285
286 def _download_expectation_images(self, expected_image_dir, actual_image_dir):
287 """Download the expected and actual images for the _expectations array.
288
289 @param expected_image_dir The directory to download expected images
290 into.
291 @param actual_image_dir The directory to download actual images into.
292 """
293 image_map = {}
294
295 # Look through expectations and download their images.
296 for expectation in self._expectations:
297 # Build appropriate paths to download the images into.
298 expected_image_path = os.path.join(expected_image_dir,
299 expectation.device_name + '-' +
300 expectation.image_name)
301
302 actual_image_path = os.path.join(actual_image_dir,
303 expectation.device_name + '-' +
304 expectation.image_name)
305
306 print('Downloading %s for device %s' % (
307 expectation.image_name, expectation.device_name))
308
309 # Download images
310 download_gm_image(expectation.image_name,
311 expected_image_path,
312 expectation.expected_hash)
313
314 download_gm_image(expectation.image_name,
315 actual_image_path,
316 expectation.actual_hash)
317
318 # Annotate the expectations with where the images were downloaded
319 # to.
320 expectation.expected_image_path = expected_image_path
321 expectation.actual_image_path = actual_image_path
322
323 # Map the image paths back to the expectations.
324 image_map[expected_image_path] = (False, expectation)
325 image_map[actual_image_path] = (True, expectation)
326
327 self.image_map = image_map
328
329 def _set_expected_hash(self, device_name, image_name, hash_value):
330 """Set the expected hash for the image of the given device. This always
331 writes directly to the expected results file of the given device
332
333 @param device_name The name of the device to write the hash to.
334 @param image_name The name of the image whose hash to set.
335 @param hash_value The value of the hash to set.
336 """
337
338 # Retrieve the expected results file as it is in the working tree
339 json_path = os.path.join(self._expectations_dir, device_name,
340 self._expected_name)
341 expectations = gm_json.LoadFromFile(json_path)
342
343 # Set the specified hash.
344 set_expected_hash_in_json(expectations, image_name, hash_value)
345
346 # Write it out to disk using gm_json to keep the formatting consistent.
347 gm_json.WriteToFile(expectations, json_path)
348
349 def use_hash_of(self, image_path):
350 """Determine the hash of the image at the path using the records, and
351 set it as the expected hash for its device and image config.
352
353 @param image_path The path of the image as it was stored in the output
354 of skpdiff_path
355 """
356
357 # Get the metadata about the image at the path.
358 is_actual, expectation = self.image_map[image_path]
359
360 expectation_hash = expectation.actual_hash if is_actual else\
361 expectation.expected_hash
362
363 # Write out that image's hash directly to the expected results file.
364 self._set_expected_hash(expectation.device_name, expectation.image_name,
365 expectation_hash)
366
367
zachr@google.com48b88912013-07-25 19:49:17 +0000368class SkPDiffHandler(BaseHTTPServer.BaseHTTPRequestHandler):
369 def send_file(self, file_path):
370 # Grab the extension if there is one
371 extension = os.path.splitext(file_path)[1]
372 if len(extension) >= 1:
373 extension = extension[1:]
374
375 # Determine the MIME type of the file from its extension
376 mime_type = MIME_TYPE_MAP.get(extension, MIME_TYPE_MAP[''])
377
378 # Open the file and send it over HTTP
zachr@google.com785ef5d2013-07-26 19:29:39 +0000379 if os.path.isfile(file_path):
380 with open(file_path, 'rb') as sending_file:
381 self.send_response(200)
382 self.send_header('Content-type', mime_type)
383 self.end_headers()
384 self.wfile.write(sending_file.read())
385 else:
386 self.send_error(404)
zachr@google.com48b88912013-07-25 19:49:17 +0000387
388 def serve_if_in_dir(self, dir_path, file_path):
389 # Determine if the file exists relative to the given dir_path AND exists
390 # under the dir_path. This is to prevent accidentally serving files
391 # outside the directory intended using symlinks, or '../'.
392 real_path = os.path.normpath(os.path.join(dir_path, file_path))
zachr@google.com48b88912013-07-25 19:49:17 +0000393 if os.path.commonprefix([real_path, dir_path]) == dir_path:
394 if os.path.isfile(real_path):
395 self.send_file(real_path)
396 return True
397 return False
398
399 def do_GET(self):
zachr@google.com48b88912013-07-25 19:49:17 +0000400 # Simple rewrite rule of the root path to 'viewer.html'
401 if self.path == '' or self.path == '/':
402 self.path = '/viewer.html'
403
404 # The [1:] chops off the leading '/'
405 file_path = self.path[1:]
406
zachr@google.com785ef5d2013-07-26 19:29:39 +0000407 # Handle skpdiff_output.json manually because it is was processed by the
408 # server when it was started and does not exist as a file.
409 if file_path == 'skpdiff_output.json':
410 self.send_response(200)
411 self.send_header('Content-type', MIME_TYPE_MAP['json'])
412 self.end_headers()
413 self.wfile.write(self.server.skpdiff_output_json)
zachr@google.com48b88912013-07-25 19:49:17 +0000414 return
415
zachr@google.com785ef5d2013-07-26 19:29:39 +0000416 # Attempt to send static asset files first.
417 if self.serve_if_in_dir(SCRIPT_DIR, file_path):
418 return
419
420 # WARNING: Serving any file the user wants is incredibly insecure. Its
421 # redeeming quality is that we only serve gm files on a white list.
422 if self.path in self.server.image_set:
423 self.send_file(self.path)
zachr@google.com48b88912013-07-25 19:49:17 +0000424 return
425
426 # If no file to send was found, just give the standard 404
427 self.send_error(404)
428
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000429 def do_POST(self):
430 if self.path == '/set_hash':
431 content_length = int(self.headers['Content-length'])
432 request_data = json.loads(self.rfile.read(content_length))
433 self.server.expectations_manager.use_hash_of(request_data['path'])
434 self.send_response(200)
435 self.send_header('Content-type', 'application/json')
436 self.end_headers()
437 self.wfile.write('{"success":true}')
438 return
zachr@google.com48b88912013-07-25 19:49:17 +0000439
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000440 # If the we have no handler for this path, give em' the 404
441 self.send_error(404)
442
443
444def run_server(expectations_manager, port=8080):
zachr@google.com785ef5d2013-07-26 19:29:39 +0000445 # Preload the skpdiff results file. This is so we can perform some
446 # processing on it.
447 skpdiff_output_json = ''
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000448 with open(expectations_manager.skpdiff_output_path, 'rb') as records_file:
zachr@google.com785ef5d2013-07-26 19:29:39 +0000449 skpdiff_output_json = records_file.read()
450
451 # It's important to parse the results file so that we can make a set of
452 # images that the web page might request.
453 skpdiff_records = json.loads(skpdiff_output_json)['records']
454 image_set = get_image_set_from_skpdiff(skpdiff_records)
455
456 # Add JSONP padding to the JSON because the web page expects it. It expects
457 # it because it was designed to run with or without a web server. Without a
458 # web server, the only way to load JSON is with JSONP.
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000459 skpdiff_output_json = ('var SkPDiffRecords = ' +
460 json.dumps({'records': skpdiff_records}) + ';')
zachr@google.com785ef5d2013-07-26 19:29:39 +0000461
zachr@google.com48b88912013-07-25 19:49:17 +0000462 # Do not bind to interfaces other than localhost because the server will
463 # attempt to serve files relative to the root directory as a last resort
464 # before 404ing. This means all of your files can be accessed from this
465 # server, so DO NOT let this server listen to anything but localhost.
zachr@google.com785ef5d2013-07-26 19:29:39 +0000466 server_address = ('127.0.0.1', port)
zachr@google.com48b88912013-07-25 19:49:17 +0000467 http_server = BaseHTTPServer.HTTPServer(server_address, SkPDiffHandler)
zachr@google.com785ef5d2013-07-26 19:29:39 +0000468 http_server.image_set = image_set
469 http_server.skpdiff_output_json = skpdiff_output_json
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000470 http_server.expectations_manager = expectations_manager
471 print('Navigate thine browser to: http://{}:{}/'.format(*server_address))
zachr@google.com48b88912013-07-25 19:49:17 +0000472 http_server.serve_forever()
473
zachr@google.com785ef5d2013-07-26 19:29:39 +0000474
475def main():
476 parser = argparse.ArgumentParser()
477 parser.add_argument('--port', '-p', metavar='PORT',
478 type=int,
479 default=8080,
480 help='port to bind the server to; ' +
481 'defaults to %(default)s',
482 )
483
484 parser.add_argument('--expectations-dir', metavar='EXPECTATIONS_DIR',
485 default=DEFAULT_GM_EXPECTATIONS_DIR,
486 help='path to the gm expectations; ' +
487 'defaults to %(default)s'
488 )
489
490 parser.add_argument('--expected',
491 metavar='EXPECTATIONS_FILE_NAME',
492 default='expected-results.json',
493 help='the file name of the expectations JSON; ' +
494 'defaults to %(default)s'
495 )
496
497 parser.add_argument('--updated',
498 metavar='UPDATED_FILE_NAME',
499 default='updated-results.json',
500 help='the file name of the updated expectations JSON;' +
501 ' defaults to %(default)s'
502 )
503
504 parser.add_argument('--skpdiff-path', metavar='SKPDIFF_PATH',
505 default=None,
506 help='the path to the skpdiff binary to use; ' +
507 'defaults to out/Release/skpdiff or out/Default/skpdiff'
508 )
509
510 args = vars(parser.parse_args()) # Convert args into a python dict
511
512 # Make sure we have access to an skpdiff binary
513 skpdiff_path = get_skpdiff_path(args['skpdiff_path'])
514 if skpdiff_path is None:
515 sys.exit(1)
516
zachr@google.com785ef5d2013-07-26 19:29:39 +0000517 # Print out the paths of things for easier debugging
518 print('script dir :', SCRIPT_DIR)
519 print('tools dir :', TOOLS_DIR)
520 print('root dir :', SKIA_ROOT_DIR)
521 print('expectations dir :', args['expectations_dir'])
522 print('skpdiff path :', skpdiff_path)
zachr@google.com785ef5d2013-07-26 19:29:39 +0000523
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000524 expectations_manager = ExpectationsManager(args['expectations_dir'],
525 args['expected'],
526 args['updated'],
527 skpdiff_path)
zachr@google.com785ef5d2013-07-26 19:29:39 +0000528
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000529 run_server(expectations_manager, port=args['port'])
zachr@google.com785ef5d2013-07-26 19:29:39 +0000530
zachr@google.com48b88912013-07-25 19:49:17 +0000531if __name__ == '__main__':
532 main()