blob: 11417be93f7ab12d1e68ba854f52fe2d1df93d52 [file] [log] [blame]
epoger@google.com27442af2011-12-29 21:13:08 +00001'''
2Downloads the actual gm results most recently generated by the Skia buildbots,
3and adds any new ones to SVN control.
4
5This tool makes it much easier to check in new baselines, via the following
6steps:
7
8cd .../trunk
9svn update
10# make sure there are no files awaiting svn commit
epoger@google.comd6256552012-01-10 14:10:34 +000011python tools/download-baselines.py gm/base-macmini-lion-fixed # or other gm/ subdir
epoger@google.com27442af2011-12-29 21:13:08 +000012# upload CL for review
13# validate that the new images look right
14# commit CL
15
epoger@google.comd6256552012-01-10 14:10:34 +000016Launch with --help to see more options.
17
epoger@google.com27442af2011-12-29 21:13:08 +000018
19Copyright 2011 Google Inc.
20
21Use of this source code is governed by a BSD-style license that can be
22found in the LICENSE file.
23'''
24
25# common Python modules
epoger@google.com20ad5ac2012-01-17 21:26:05 +000026import fnmatch
epoger@google.comd6256552012-01-10 14:10:34 +000027import optparse
28import os
epoger@google.com27442af2011-12-29 21:13:08 +000029import re
epoger@google.com20ad5ac2012-01-17 21:26:05 +000030import shutil
epoger@google.com27442af2011-12-29 21:13:08 +000031import sys
epoger@google.com20ad5ac2012-01-17 21:26:05 +000032import tempfile
epoger@google.com27442af2011-12-29 21:13:08 +000033
34# modules declared within this same directory
35import svn
36
epoger@google.com20ad5ac2012-01-17 21:26:05 +000037# Base URL of SVN repository where buildbots store actual gm image results.
38SVN_BASE_URL = 'http://skia-autogen.googlecode.com/svn/gm-actual'
epoger@google.com27442af2011-12-29 21:13:08 +000039
epoger@google.comd6256552012-01-10 14:10:34 +000040USAGE_STRING = 'usage: %s [options] <baseline_subdir>'
41OPTION_IGNORE_LOCAL_MODS = '--ignore-local-mods'
42OPTION_ADD_NEW_FILES = '--add-new-files'
43
epoger@google.com20ad5ac2012-01-17 21:26:05 +000044def GetLatestResultsSvnUrl(baseline_subdir):
45 """Return SVN URL from which we can check out the MOST RECENTLY generated
epoger@google.com27442af2011-12-29 21:13:08 +000046 images for this baseline type.
47
48 @param baseline_subdir indicates which platform we want images for
49 """
epoger@google.com20ad5ac2012-01-17 21:26:05 +000050 # trim off 'gm/' prefix
51 gm_prefix = 'gm%s' % os.sep
52 if not baseline_subdir.startswith(gm_prefix):
53 raise Exception('baseline_subdir "%s" should start with "%s"' % (
54 baseline_subdir, gm_prefix))
55 return '%s/%s' % (SVN_BASE_URL, baseline_subdir[len(gm_prefix):])
epoger@google.com27442af2011-12-29 21:13:08 +000056
epoger@google.com20ad5ac2012-01-17 21:26:05 +000057def CopyMatchingFiles(source_dir, dest_dir, filename_pattern,
58 only_copy_updates=False):
59 """Copy all files from source_dir that match filename_pattern, and
60 save them (with their original filenames) in dest_dir.
epoger@google.com27442af2011-12-29 21:13:08 +000061
epoger@google.com20ad5ac2012-01-17 21:26:05 +000062 @param source_dir
63 @param dest_dir where to save the copied files
64 @param filename_pattern only copy files that match this Unix-style filename
65 pattern (e.g., '*.jpg')
66 @param only_copy_updates if True, only copy files that are already
67 present in dest_dir
epoger@google.com27442af2011-12-29 21:13:08 +000068 """
epoger@google.com20ad5ac2012-01-17 21:26:05 +000069 all_filenames = os.listdir(source_dir)
70 matching_filenames = fnmatch.filter(all_filenames, filename_pattern)
71 for filename in matching_filenames:
72 source_path = os.path.join(source_dir, filename)
73 dest_path = os.path.join(dest_dir, filename)
74 if only_copy_updates and not os.path.isfile(dest_path):
epoger@google.comd6256552012-01-10 14:10:34 +000075 continue
epoger@google.com20ad5ac2012-01-17 21:26:05 +000076 shutil.copyfile(source_path, dest_path)
epoger@google.com27442af2011-12-29 21:13:08 +000077
epoger@google.comd6256552012-01-10 14:10:34 +000078def Main(options, args):
epoger@google.com27442af2011-12-29 21:13:08 +000079 """Download most recently generated baseline images for a given platform,
80 and add any new ones to SVN control.
81
epoger@google.comd6256552012-01-10 14:10:34 +000082 @param options
83 @param args
epoger@google.com27442af2011-12-29 21:13:08 +000084 """
epoger@google.comd6256552012-01-10 14:10:34 +000085 num_args = len(args)
86 if num_args != 1:
87 RaiseUsageException()
epoger@google.com27442af2011-12-29 21:13:08 +000088
epoger@google.com20ad5ac2012-01-17 21:26:05 +000089 # Create repo_to_modify to handle the SVN repository we will add files to.
90 baseline_subdir = args[0].rstrip(os.sep);
91 if not os.path.isdir(baseline_subdir):
92 raise Exception('could not find baseline_subdir "%s"' % baseline_subdir)
93 repo_to_modify = svn.Svn(baseline_subdir)
epoger@google.comd6256552012-01-10 14:10:34 +000094
95 # If there are any locally modified files in that directory, exit
96 # (so that we don't risk overwriting the user's previous work).
epoger@google.com20ad5ac2012-01-17 21:26:05 +000097 new_and_modified_files = repo_to_modify.GetNewAndModifiedFiles()
epoger@google.comd6256552012-01-10 14:10:34 +000098 if not options.ignore_local_mods:
99 if new_and_modified_files:
100 raise Exception('Exiting because there are already new and/or '
101 'modified files in %s. To continue in spite of '
102 'that, run with %s option.' % (
103 baseline_subdir, OPTION_IGNORE_LOCAL_MODS))
104
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000105 # Download actual gm images into a separate repo in a temporary directory.
106 tempdir = tempfile.mkdtemp()
107 download_repo = svn.Svn(tempdir)
108 download_repo.Checkout(GetLatestResultsSvnUrl(baseline_subdir), '.')
109
110 # Copy any of those files we are interested in into repo_to_modify,
111 # and then delete the temporary directory.
112 CopyMatchingFiles(source_dir=tempdir, dest_dir=baseline_subdir,
113 filename_pattern='*.png',
114 only_copy_updates=(not options.add_new_files))
115 shutil.rmtree(tempdir)
116 download_repo = None
epoger@google.comd6256552012-01-10 14:10:34 +0000117
118 # Add any new files to SVN control (if we are running with add_new_files).
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000119 new_files = repo_to_modify.GetNewFiles()
epoger@google.comd6256552012-01-10 14:10:34 +0000120 if new_files and options.add_new_files:
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000121 repo_to_modify.AddFiles(new_files)
122
123 # Set the mimetype property on *all* image files in baseline_subdir, even
124 # the ones that were already there (in case that property wasn't properly
125 # set already).
126 repo_to_modify.SetPropertyByFilenamePattern(
127 '*.png', svn.PROPERTY_MIMETYPE, 'image/png')
128 repo_to_modify.SetPropertyByFilenamePattern(
129 '*.pdf', svn.PROPERTY_MIMETYPE, 'application/pdf')
epoger@google.com27442af2011-12-29 21:13:08 +0000130
epoger@google.comd6256552012-01-10 14:10:34 +0000131def RaiseUsageException():
132 raise Exception(USAGE_STRING % __file__)
133
epoger@google.com27442af2011-12-29 21:13:08 +0000134if __name__ == '__main__':
epoger@google.comd6256552012-01-10 14:10:34 +0000135 parser = optparse.OptionParser(USAGE_STRING % '%prog')
136 parser.add_option(OPTION_IGNORE_LOCAL_MODS,
137 action='store_true', default=False,
138 help='allow tool to run even if there are already '
139 'local modifications in the baseline_subdir')
140 parser.add_option(OPTION_ADD_NEW_FILES,
141 action='store_true', default=False,
142 help='in addition to downloading new versions of '
143 'existing baselines, also download baselines that are '
144 'not under SVN control yet')
145 (options, args) = parser.parse_args()
146 Main(options, args)