Home | History | Annotate | Download | only in tools
      1 '''
      2 Compares the rendererings of serialized SkPictures to expected result.
      3 
      4 Copyright 2012 Google Inc.
      5 
      6 Use of this source code is governed by a BSD-style license that can be
      7 found in the LICENSE file.
      8 '''
      9 # common Python modules
     10 import os
     11 import optparse
     12 import sys
     13 import shutil
     14 import tempfile
     15 
     16 USAGE_STRING = 'Usage: %s input... expectedDir render_app [reander_app_args]'
     17 HELP_STRING = '''
     18 
     19 Compares the renderings of serialized SkPicture files and directories specified
     20 by input with the files in expectedDir. Note, files in directoriers are
     21 expected to end with .skp.
     22 '''
     23 
     24 def RunCommand(command):
     25     """Run a command.
     26 
     27     @param command the command as a single string
     28     """
     29     print 'running command [%s]...' % command
     30     os.system(command)
     31 
     32 
     33 def FindPathToProgram(program):
     34     """Return path to an existing program binary, or raise an exception if we
     35     cannot find one.
     36 
     37     @param program the name of the program that is being looked for
     38     """
     39     trunk_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
     40                                               os.pardir))
     41     possible_paths = [os.path.join(trunk_path, 'out', 'Release', program),
     42                       os.path.join(trunk_path, 'out', 'Debug', program),
     43                       os.path.join(trunk_path, 'out', 'Release',
     44                                    program + ".exe"),
     45                       os.path.join(trunk_path, 'out', 'Debug',
     46                                    program + ".exe")]
     47     for try_path in possible_paths:
     48         if os.path.isfile(try_path):
     49             return try_path
     50     raise Exception('cannot find %s in paths %s; maybe you need to '
     51                     'build %s?' % (program, possible_paths, program))
     52 
     53 
     54 def RenderSkps(inputs, render_dir, render_app, args):
     55     """Renders the serialized SkPictures.
     56 
     57     Uses the render_pictures program to do the rendering.
     58 
     59     @param inputs the location(s) to read the serlialized SkPictures
     60     @param render_dir the location to write out the rendered images
     61     """
     62     renderer_path = FindPathToProgram(render_app)
     63     inputs_as_string = " ".join(inputs)
     64     command = '%s %s %s' % (renderer_path, inputs_as_string, render_dir)
     65 
     66     command += args
     67 
     68     RunCommand(command)
     69 
     70 
     71 def DiffRenderings(expected_dir, comparison_dir, diff_dir):
     72     """Diffs the rendered SkPicture files with the baseline files.
     73 
     74     Uses the skdiff program to do the diffing.
     75 
     76     @param expected_dir the location of the baseline images.
     77     @param comparison_dir the location of the images to comapre with the
     78            baseline
     79     @param diff_dir the location to write out the diff results
     80     """
     81     skdiff_path = FindPathToProgram('skdiff')
     82     RunCommand('%s %s %s %s %s' %
     83                (skdiff_path, expected_dir, comparison_dir, diff_dir,
     84                 '--noprintdirs'))
     85 
     86 
     87 def Cleanup(render_dir_option, diff_dir_option, render_dir, diff_dir):
     88     """Deletes any temporary folders and files created.
     89 
     90     @param foo_option The OptionParser parsed render_dir or diff_dir vars.
     91             If these variables are not passed by user we ended up creating
     92             temporary directories (render_dir, diff_dir) which we will remove.
     93     @param render_dir the directory where the rendered images were written
     94     @param diff_dir the directory where the diff results were written
     95     """
     96     if (not render_dir_option):
     97         if (os.path.isdir(render_dir)):
     98             shutil.rmtree(render_dir)
     99     if (not diff_dir_option):
    100         if (os.path.isdir(diff_dir)):
    101             shutil.rmtree(diff_dir)
    102 
    103 def TestRenderSkps(inputs, expected_dir, render_dir_option, diff_dir_option,
    104                    render_app, render_args):
    105     if (render_dir_option):
    106         render_dir = render_dir_option
    107     else:
    108         render_dir = tempfile.mkdtemp()
    109 
    110     if (diff_dir_option):
    111         diff_dir = diff_dir_option
    112     else:
    113         diff_dir = tempfile.mkdtemp()
    114     try:    
    115         RenderSkps(inputs, render_dir, render_app, render_args)
    116         DiffRenderings(expected_dir, render_dir, diff_dir)
    117     finally:
    118         Cleanup(render_dir_option, diff_dir_option, render_dir, diff_dir)
    119