1'''
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
19# modules declared within this same directory
20import test_rendering
21
22USAGE_STRING = 'Usage: %s input... expectedDir'
23HELP_STRING = '''
24
25Takes input SkPicture files and renders them as PNG files, and then compares
26those resulting PNG files against PNG files found in expectedDir.
27
28Each instance of "input" can be either a file (name must end in .skp), or a
29directory (in which case this script will process all .skp files within the
30directory).
31'''
32
33def ModeParse(option, opt_str, value, parser):
34    """Parses the --mode option of the commandline.
35
36    The --mode option will either take in three parameters (if tile or
37    pow2tile) or a single parameter (otherwise).
38    """
39    result = [value]
40    if value == "tile":
41          if (len(parser.rargs) < 2):
42              raise optparse.OptionValueError(("--mode tile mising width"
43                                               " and/or height parameters"))
44          result.extend(parser.rargs[:2])
45          del parser.rargs[:2]
46    elif value == "pow2tile":
47          if (len(parser.rargs) < 2):
48              raise optparse.OptionValueError(("--mode pow2tile mising minWidth"
49                                               " and/or height parameters"))
50          result.extend(parser.rargs[:2])
51          del parser.rargs[:2]
52
53    setattr(parser.values, option.dest, result)
54
55
56def Main(args):
57    """Allow other scripts to call this script with fake command-line args.
58
59    @param The commandline argument list
60    """
61    parser = optparse.OptionParser(USAGE_STRING % '%prog' + HELP_STRING)
62    parser.add_option('--render_dir', dest='render_dir',
63                    help = ("specify the location to output the rendered files."
64                              " Default is a temp directory."))
65    parser.add_option('--diff_dir', dest='diff_dir',
66                    help = ("specify the location to output the diff files."
67                              " Default is a temp directory."))
68    parser.add_option('--mode', dest='mode', type='string',
69                      action="callback", callback=ModeParse,
70                      help = ("specify how rendering is to be done."))
71    parser.add_option('--device', dest='device',
72                      help = ("specify the device to render to."))
73
74    options, arguments = parser.parse_args(args)
75
76    if (len(arguments) < 3):
77        print("Expected at least one input and one ouput folder.")
78        parser.print_help()
79        sys.exit(-1)
80
81    inputs = arguments[1:-1]
82    expected_dir = arguments[-1]
83
84    extra_args = ''
85
86    if (options.mode is not None):
87        extra_args += ' --mode %s' % ' '.join(options.mode)
88
89    if (options.device is not None):
90        extra_args += ' --device %s' % options.device
91
92    test_rendering.TestRenderSkps(inputs, expected_dir, options.render_dir,
93                                  options.diff_dir, 'render_pictures',
94                                  extra_args)
95
96if __name__ == '__main__':
97    Main(sys.argv)
98