blob: 253bf4e50d217b4de934e842370b3ec87004aa43 [file] [log] [blame]
junov@chromium.org777442d2012-06-12 14:56:36 +00001'''
2Compares the rendererings of serialized SkPictures to expected images.
3
4Launch with --help to see more information.
5
6
7Copyright 2012 Google Inc.
8
9Use of this source code is governed by a BSD-style license that can be
10found in the LICENSE file.
11'''
12# common Python modules
13import os
14import optparse
15import sys
16import shutil
17import tempfile
18
19USAGE_STRING = 'Usage: %s inputDir expectedDir [renderDir [diffDir]]'
20HELP_STRING = '''
21
22Compares the renderings of serialized SkPicture files in inputDir with the
23images in expectedDir.
24'''
25
26def RunCommand(command):
27 """Run a command.
28
29 @param command the command as a single string
30 """
31 print 'running command [%s]...' % command
32 os.system(command)
33
34
35def FindPathToProgram(program):
36 """Return path to an existing program binary, or raise an exception if we
37 cannot find one.
38
39 @param program the name of the program that is being looked for
40 """
41 trunk_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
42 os.pardir))
43 possible_paths = [os.path.join(trunk_path, 'out', 'Release', program),
44 os.path.join(trunk_path, 'out', 'Debug', program)]
45 for try_path in possible_paths:
46 if os.path.isfile(try_path):
47 return try_path
48 raise Exception('cannot find %s in paths %s; maybe you need to '
49 'build %s?' % (program, possible_paths, program))
50
51
52def RenderImages(input_dir, render_dir):
53 """Renders the serialized SkPictures.
54
55 Uses the render_pictures program to do the rendering.
56
57 @param input_dir the location to read the serlialized SkPictures
58 @param render_dir the location to write out the rendered images
59 """
60 renderer_path = FindPathToProgram('render_pictures')
61 RunCommand('%s %s %s' % (renderer_path, input_dir, render_dir))
62
63
64def DiffImages(expected_dir, comparison_dir, diff_dir):
65 """Diffs the rendered SkPicture images with the baseline images.
66
67 Uses the skdiff program to do the diffing.
68
69 @param expected_dir the location of the baseline images.
70 @param comparison_dir the location of the images to comapre with the
71 baseline
72 @param diff_dir the location to write out the diff results
73 """
74 skdiff_path = FindPathToProgram('skdiff')
75 RunCommand('%s %s %s %s' %
76 (skdiff_path, expected_dir, comparison_dir, diff_dir))
77
78
79def Cleanup(options, render_dir, diff_dir):
80 """Deletes any temporary folders and files created.
81
82 @param options The OptionParser object that parsed if render_dir or diff_dir
83 was set
84 @param render_dir the directory where the rendered images were written
85 @param diff_dir the directory where the diff results were written
86 """
87 if (not options.render_dir):
88 if (os.path.isdir(render_dir)):
89 shutil.rmtree(render_dir)
90 if (not options.diff_dir):
91 if (os.path.isdir(diff_dir)):
92 shutil.rmtree(diff_dir)
93
94
95def Main(args):
96 """Allow other scripts to call this script with fake command-line args.
97
98 @param The commandline argument list
99 """
100 parser = optparse.OptionParser(USAGE_STRING % '%prog' + HELP_STRING)
101 parser.add_option('--render_dir', dest='render_dir',
102 help = ("specify the location to output the rendered files."
103 " Default is a temp directory."))
104 parser.add_option('--diff_dir', dest='diff_dir',
105 help = ("specify the location to output the diff files."
106 " Default is a temp directory."))
107
108 options, arguments = parser.parse_args(args)
109
110 input_dir = arguments[1]
111 expected_dir = arguments[2]
112
113 if (options.render_dir):
114 render_dir = options.render_dir
115 else:
116 render_dir = tempfile.mkdtemp()
117
118 if (options.diff_dir):
119 diff_dir = options.diff_dir
120 else:
121 diff_dir = tempfile.mkdtemp()
122
123 try:
124 RenderImages(input_dir, render_dir)
125 DiffImages(expected_dir, render_dir, diff_dir)
126 finally:
127 Cleanup(options, render_dir, diff_dir)
128
129if __name__ == '__main__':
130 Main(sys.argv)