junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Compares the rendererings of serialized SkPictures to expected images. |
| 3 | |
| 4 | Launch with --help to see more information. |
| 5 | |
| 6 | |
| 7 | Copyright 2012 Google Inc. |
| 8 | |
| 9 | Use of this source code is governed by a BSD-style license that can be |
| 10 | found in the LICENSE file. |
| 11 | ''' |
| 12 | # common Python modules |
| 13 | import os |
| 14 | import optparse |
| 15 | import sys |
| 16 | import shutil |
| 17 | import tempfile |
| 18 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 19 | USAGE_STRING = 'Usage: %s input... expectedDir' |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 20 | HELP_STRING = ''' |
| 21 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 22 | Compares the renderings of serialized SkPicture files and directories specified |
| 23 | by input with the images in expectedDir. Note, files in directoriers are |
| 24 | expected to end with .skp. |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 25 | ''' |
| 26 | |
| 27 | def 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 | |
| 36 | def 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.org | f0fed9f | 2012-08-20 17:33:06 +0000 | [diff] [blame] | 45 | 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.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 50 | 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.org | e599ab7 | 2012-08-23 21:51:11 +0000 | [diff] [blame] | 57 | def RenderImages(inputs, render_dir, options): |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 58 | """Renders the serialized SkPictures. |
| 59 | |
| 60 | Uses the render_pictures program to do the rendering. |
| 61 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 62 | @param inputs the location(s) to read the serlialized SkPictures |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 63 | @param render_dir the location to write out the rendered images |
| 64 | """ |
| 65 | renderer_path = FindPathToProgram('render_pictures') |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 66 | inputs_as_string = " ".join(inputs) |
keyar@chromium.org | e599ab7 | 2012-08-23 21:51:11 +0000 | [diff] [blame] | 67 | command = '%s %s %s' % (renderer_path, inputs_as_string, render_dir) |
| 68 | |
| 69 | if (options.mode is not None): |
| 70 | command += ' --mode %s' % ' '.join(options.mode) |
| 71 | |
| 72 | if (options.device is not None): |
| 73 | command += ' --device %s' % options.device |
| 74 | |
| 75 | RunCommand(command) |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def DiffImages(expected_dir, comparison_dir, diff_dir): |
| 79 | """Diffs the rendered SkPicture images with the baseline images. |
| 80 | |
| 81 | Uses the skdiff program to do the diffing. |
| 82 | |
| 83 | @param expected_dir the location of the baseline images. |
| 84 | @param comparison_dir the location of the images to comapre with the |
| 85 | baseline |
| 86 | @param diff_dir the location to write out the diff results |
| 87 | """ |
| 88 | skdiff_path = FindPathToProgram('skdiff') |
keyar@chromium.org | a631819 | 2012-07-09 21:01:50 +0000 | [diff] [blame] | 89 | RunCommand('%s %s %s %s %s' % |
| 90 | (skdiff_path, expected_dir, comparison_dir, diff_dir, |
| 91 | '--noprintdirs')) |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | def Cleanup(options, render_dir, diff_dir): |
| 95 | """Deletes any temporary folders and files created. |
| 96 | |
| 97 | @param options The OptionParser object that parsed if render_dir or diff_dir |
| 98 | was set |
| 99 | @param render_dir the directory where the rendered images were written |
| 100 | @param diff_dir the directory where the diff results were written |
| 101 | """ |
| 102 | if (not options.render_dir): |
| 103 | if (os.path.isdir(render_dir)): |
| 104 | shutil.rmtree(render_dir) |
| 105 | if (not options.diff_dir): |
| 106 | if (os.path.isdir(diff_dir)): |
| 107 | shutil.rmtree(diff_dir) |
| 108 | |
| 109 | |
keyar@chromium.org | e599ab7 | 2012-08-23 21:51:11 +0000 | [diff] [blame] | 110 | def ModeParse(option, opt_str, value, parser): |
| 111 | """Parses the --mode option of the commandline. |
| 112 | |
| 113 | The --mode option will either take in three parameters (if tile or |
| 114 | pow2tile) or a single parameter (otherwise). |
| 115 | """ |
| 116 | result = [value] |
| 117 | if value == "tile": |
| 118 | if (len(parser.rargs) < 2): |
| 119 | raise optparse.OptionValueError(("--mode tile mising width" |
| 120 | " and/or height parameters")) |
| 121 | result.extend(parser.rargs[:2]) |
| 122 | del parser.rargs[:2] |
| 123 | elif value == "pow2tile": |
| 124 | if (len(parser.rargs) < 2): |
| 125 | raise optparse.OptionValueError(("--mode pow2tile mising minWidth" |
| 126 | " and/or height parameters")) |
| 127 | result.extend(parser.rargs[:2]) |
| 128 | del parser.rargs[:2] |
| 129 | |
| 130 | setattr(parser.values, option.dest, result) |
| 131 | |
| 132 | |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 133 | def Main(args): |
| 134 | """Allow other scripts to call this script with fake command-line args. |
| 135 | |
| 136 | @param The commandline argument list |
| 137 | """ |
| 138 | parser = optparse.OptionParser(USAGE_STRING % '%prog' + HELP_STRING) |
| 139 | parser.add_option('--render_dir', dest='render_dir', |
| 140 | help = ("specify the location to output the rendered files." |
| 141 | " Default is a temp directory.")) |
| 142 | parser.add_option('--diff_dir', dest='diff_dir', |
| 143 | help = ("specify the location to output the diff files." |
| 144 | " Default is a temp directory.")) |
keyar@chromium.org | e599ab7 | 2012-08-23 21:51:11 +0000 | [diff] [blame] | 145 | parser.add_option('--mode', dest='mode', type='string', |
| 146 | action="callback", callback=ModeParse, |
| 147 | help = ("specify how rendering is to be done.")) |
| 148 | parser.add_option('--device', dest='device', |
| 149 | help = ("specify the device to render to.")) |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 150 | |
| 151 | options, arguments = parser.parse_args(args) |
| 152 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 153 | if (len(arguments) < 3): |
| 154 | print("Expected at least one input and one ouput folder.") |
| 155 | parser.print_help() |
| 156 | sys.exit(-1) |
| 157 | |
| 158 | inputs = arguments[1:-1] |
| 159 | expected_dir = arguments[-1] |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 160 | |
| 161 | if (options.render_dir): |
| 162 | render_dir = options.render_dir |
| 163 | else: |
| 164 | render_dir = tempfile.mkdtemp() |
| 165 | |
| 166 | if (options.diff_dir): |
| 167 | diff_dir = options.diff_dir |
| 168 | else: |
| 169 | diff_dir = tempfile.mkdtemp() |
| 170 | |
| 171 | try: |
keyar@chromium.org | e599ab7 | 2012-08-23 21:51:11 +0000 | [diff] [blame] | 172 | RenderImages(inputs, render_dir, options) |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 173 | DiffImages(expected_dir, render_dir, diff_dir) |
| 174 | finally: |
| 175 | Cleanup(options, render_dir, diff_dir) |
| 176 | |
| 177 | if __name__ == '__main__': |
| 178 | Main(sys.argv) |