blob: 31e848b3c3cb04145884198f41a756707b5fca8b [file] [log] [blame]
edisonn@google.comc2c49812012-10-10 15:08:12 +00001'''
edisonn@google.comc2c49812012-10-10 15:08:12 +00002Copyright 2012 Google Inc.
3
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
commit-bot@chromium.org11f15622014-01-07 17:03:40 +00006
7Compares the rendererings of serialized SkPictures to expected result.
8
9TODO(epoger): Combine with overlapping tools/tests/render_pictures_test.py .
10See https://code.google.com/p/skia/issues/detail?id=1943#c2
edisonn@google.comc2c49812012-10-10 15:08:12 +000011'''
12# common Python modules
13import os
14import optparse
15import sys
16import shutil
17import tempfile
18
19USAGE_STRING = 'Usage: %s input... expectedDir render_app [reander_app_args]'
20HELP_STRING = '''
21
22Compares the renderings of serialized SkPicture files and directories specified
23by input with the files in expectedDir. Note, files in directoriers are
24expected to end with .skp.
25'''
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),
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")]
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
57def RenderSkps(inputs, render_dir, render_app, args):
58 """Renders the serialized SkPictures.
59
60 Uses the render_pictures program to do the rendering.
61
62 @param inputs the location(s) to read the serlialized SkPictures
63 @param render_dir the location to write out the rendered images
64 """
65 renderer_path = FindPathToProgram(render_app)
66 inputs_as_string = " ".join(inputs)
67 command = '%s %s %s' % (renderer_path, inputs_as_string, render_dir)
68
69 command += args
70
71 RunCommand(command)
72
73
74def DiffRenderings(expected_dir, comparison_dir, diff_dir):
75 """Diffs the rendered SkPicture files with the baseline files.
76
77 Uses the skdiff program to do the diffing.
78
79 @param expected_dir the location of the baseline images.
80 @param comparison_dir the location of the images to comapre with the
81 baseline
82 @param diff_dir the location to write out the diff results
83 """
84 skdiff_path = FindPathToProgram('skdiff')
85 RunCommand('%s %s %s %s %s' %
86 (skdiff_path, expected_dir, comparison_dir, diff_dir,
87 '--noprintdirs'))
88
89
90def Cleanup(render_dir_option, diff_dir_option, render_dir, diff_dir):
91 """Deletes any temporary folders and files created.
92
93 @param foo_option The OptionParser parsed render_dir or diff_dir vars.
94 If these variables are not passed by user we ended up creating
95 temporary directories (render_dir, diff_dir) which we will remove.
96 @param render_dir the directory where the rendered images were written
97 @param diff_dir the directory where the diff results were written
98 """
99 if (not render_dir_option):
100 if (os.path.isdir(render_dir)):
101 shutil.rmtree(render_dir)
102 if (not diff_dir_option):
103 if (os.path.isdir(diff_dir)):
104 shutil.rmtree(diff_dir)
105
106def TestRenderSkps(inputs, expected_dir, render_dir_option, diff_dir_option,
107 render_app, render_args):
108 if (render_dir_option):
109 render_dir = render_dir_option
110 else:
111 render_dir = tempfile.mkdtemp()
112
113 if (diff_dir_option):
114 diff_dir = diff_dir_option
115 else:
116 diff_dir = tempfile.mkdtemp()
117 try:
118 RenderSkps(inputs, render_dir, render_app, render_args)
119 DiffRenderings(expected_dir, render_dir, diff_dir)
120 finally:
121 Cleanup(render_dir_option, diff_dir_option, render_dir, diff_dir)