blob: b4ae2bfd69b19645f10b815381fd6ee93819f8f9 [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
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000019USAGE_STRING = 'Usage: %s input... expectedDir'
junov@chromium.org777442d2012-06-12 14:56:36 +000020HELP_STRING = '''
21
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000022Compares the renderings of serialized SkPicture files and directories specified
23by input with the images in expectedDir. Note, files in directoriers are
24expected to end with .skp.
junov@chromium.org777442d2012-06-12 14:56:36 +000025'''
26
27def RunCommand(command):
28 """Run a command.
29
30 @param command the command as a single string
31 """
32 print 'running command [%s]...' % command
33 os.system(command)
34
35
36def FindPathToProgram(program):
37 """Return path to an existing program binary, or raise an exception if we
38 cannot find one.
39
40 @param program the name of the program that is being looked for
41 """
42 trunk_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
43 os.pardir))
44 possible_paths = [os.path.join(trunk_path, 'out', 'Release', program),
keyar@chromium.orgf0fed9f2012-08-20 17:33:06 +000045 os.path.join(trunk_path, 'out', 'Debug', program),
46 os.path.join(trunk_path, 'out', 'Release',
47 program + ".exe"),
48 os.path.join(trunk_path, 'out', 'Debug',
49 program + ".exe")]
junov@chromium.org777442d2012-06-12 14:56:36 +000050 for try_path in possible_paths:
51 if os.path.isfile(try_path):
52 return try_path
53 raise Exception('cannot find %s in paths %s; maybe you need to '
54 'build %s?' % (program, possible_paths, program))
55
56
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000057def RenderImages(inputs, render_dir):
junov@chromium.org777442d2012-06-12 14:56:36 +000058 """Renders the serialized SkPictures.
59
60 Uses the render_pictures program to do the rendering.
61
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000062 @param inputs the location(s) to read the serlialized SkPictures
junov@chromium.org777442d2012-06-12 14:56:36 +000063 @param render_dir the location to write out the rendered images
64 """
65 renderer_path = FindPathToProgram('render_pictures')
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000066 inputs_as_string = " ".join(inputs)
67 RunCommand('%s %s %s' % (renderer_path, inputs_as_string, render_dir))
junov@chromium.org777442d2012-06-12 14:56:36 +000068
69
70def DiffImages(expected_dir, comparison_dir, diff_dir):
71 """Diffs the rendered SkPicture images with the baseline images.
72
73 Uses the skdiff program to do the diffing.
74
75 @param expected_dir the location of the baseline images.
76 @param comparison_dir the location of the images to comapre with the
77 baseline
78 @param diff_dir the location to write out the diff results
79 """
80 skdiff_path = FindPathToProgram('skdiff')
keyar@chromium.orga6318192012-07-09 21:01:50 +000081 RunCommand('%s %s %s %s %s' %
82 (skdiff_path, expected_dir, comparison_dir, diff_dir,
83 '--noprintdirs'))
junov@chromium.org777442d2012-06-12 14:56:36 +000084
85
86def Cleanup(options, render_dir, diff_dir):
87 """Deletes any temporary folders and files created.
88
89 @param options The OptionParser object that parsed if render_dir or diff_dir
90 was set
91 @param render_dir the directory where the rendered images were written
92 @param diff_dir the directory where the diff results were written
93 """
94 if (not options.render_dir):
95 if (os.path.isdir(render_dir)):
96 shutil.rmtree(render_dir)
97 if (not options.diff_dir):
98 if (os.path.isdir(diff_dir)):
99 shutil.rmtree(diff_dir)
100
101
102def Main(args):
103 """Allow other scripts to call this script with fake command-line args.
104
105 @param The commandline argument list
106 """
107 parser = optparse.OptionParser(USAGE_STRING % '%prog' + HELP_STRING)
108 parser.add_option('--render_dir', dest='render_dir',
109 help = ("specify the location to output the rendered files."
110 " Default is a temp directory."))
111 parser.add_option('--diff_dir', dest='diff_dir',
112 help = ("specify the location to output the diff files."
113 " Default is a temp directory."))
114
115 options, arguments = parser.parse_args(args)
116
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000117 if (len(arguments) < 3):
118 print("Expected at least one input and one ouput folder.")
119 parser.print_help()
120 sys.exit(-1)
121
122 inputs = arguments[1:-1]
123 expected_dir = arguments[-1]
junov@chromium.org777442d2012-06-12 14:56:36 +0000124
125 if (options.render_dir):
126 render_dir = options.render_dir
127 else:
128 render_dir = tempfile.mkdtemp()
129
130 if (options.diff_dir):
131 diff_dir = options.diff_dir
132 else:
133 diff_dir = tempfile.mkdtemp()
134
135 try:
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000136 RenderImages(inputs, render_dir)
junov@chromium.org777442d2012-06-12 14:56:36 +0000137 DiffImages(expected_dir, render_dir, diff_dir)
138 finally:
139 Cleanup(options, render_dir, diff_dir)
140
141if __name__ == '__main__':
142 Main(sys.argv)