| epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Downloads the actual gm results most recently generated by the Skia buildbots, |
| 3 | and adds any new ones to SVN control. |
| 4 | |
| 5 | This tool makes it much easier to check in new baselines, via the following |
| 6 | steps: |
| 7 | |
| 8 | cd .../trunk |
| 9 | svn update |
| 10 | # make sure there are no files awaiting svn commit |
| 11 | tools/download-baselines.py gm/base-macmini-lion-fixed # or other gm/ subdir |
| 12 | # upload CL for review |
| 13 | # validate that the new images look right |
| 14 | # commit CL |
| 15 | |
| 16 | |
| 17 | Copyright 2011 Google Inc. |
| 18 | |
| 19 | Use of this source code is governed by a BSD-style license that can be |
| 20 | found in the LICENSE file. |
| 21 | ''' |
| 22 | |
| 23 | # common Python modules |
| 24 | import re |
| 25 | import sys |
| 26 | import urllib2 |
| 27 | |
| 28 | # modules declared within this same directory |
| 29 | import svn |
| 30 | |
| 31 | # Where to download recently generated baseline images for each baseline type. |
| 32 | # |
| 33 | # For now this only works for our Mac buildbots; our other buildbots aren't |
| 34 | # uploading their results to a web server yet. |
| 35 | # |
| 36 | # Note also that these will currently work only within the Google corporate |
| 37 | # network; that will also change soon. |
| 38 | ACTUALS_BY_BASELINE_SUBDIR = { |
| 39 | 'gm/base-macmini': |
| 40 | 'http://172.29.92.185/b/build/slave/Skia_Mac_Float_NoDebug/gm/actual', |
| 41 | 'gm/base-macmini-fixed': |
| 42 | 'http://172.29.92.185/b/build/slave/Skia_Mac_Fixed_NoDebug/gm/actual', |
| 43 | 'gm/base-macmini-lion-fixed': |
| 44 | 'http://172.29.92.179/b/build/slave/Skia_MacMiniLion_Fixed_NoDebug/gm/actual', |
| 45 | 'gm/base-macmini-lion-float': |
| 46 | 'http://172.29.92.179/b/build/slave/Skia_MacMiniLion_Float_NoDebug/gm/actual', |
| 47 | } |
| 48 | |
| 49 | IMAGE_REGEX = '.+\.png' |
| 50 | IMAGE_MIMETYPE = 'image/png' |
| 51 | |
| 52 | def GetPlatformUrl(baseline_subdir): |
| 53 | """Return URL within which the buildbots store generated baseline images, |
| 54 | as of multiple svn revisions. |
| 55 | |
| 56 | Raises KeyError if we don't have a URL matching this baseline_subdir. |
| 57 | |
| 58 | @param baseline_subdir indicates which platform we want images for |
| 59 | """ |
| 60 | try: |
| 61 | return ACTUALS_BY_BASELINE_SUBDIR[baseline_subdir] |
| 62 | except KeyError: |
| 63 | raise KeyError( |
| 64 | 'unknown baseline_subdir "%s", try one of these instead: %s' % ( |
| 65 | baseline_subdir, ACTUALS_BY_BASELINE_SUBDIR.keys())) |
| 66 | |
| 67 | def GetLatestResultsUrl(baseline_subdir): |
| 68 | """Return URL from which we can download the MOST RECENTLY generated |
| 69 | images for this baseline type. |
| 70 | |
| 71 | @param baseline_subdir indicates which platform we want images for |
| 72 | """ |
| 73 | base_platform_url = GetPlatformUrl(baseline_subdir) |
| 74 | print 'base_platform_url is %s' % base_platform_url |
| 75 | |
| 76 | # Find the most recently generated baseline images within base_platform_url |
| 77 | response = urllib2.urlopen(base_platform_url) |
| 78 | html = response.read() |
| 79 | link_regex = re.compile('<a href="(.*)">') |
| 80 | links = link_regex.findall(html) |
| 81 | last_link = links[-1] |
| 82 | most_recent_result_url = '%s/%s' % (base_platform_url, last_link) |
| 83 | print 'most_recent_result_url is %s' % most_recent_result_url |
| 84 | return most_recent_result_url |
| 85 | |
| 86 | def DownloadMatchingFiles(source_url, filename_regex, dest_dir): |
| 87 | """Download all files from source_url that match filename_regex, and save |
| 88 | them (with their original filenames) in dest_dir. |
| 89 | |
| 90 | @param source_url |
| 91 | @param filename_regex |
| 92 | @param dest_dir |
| 93 | """ |
| 94 | while source_url.endswith('/'): |
| 95 | source_url = source_url[:-1] |
| 96 | response = urllib2.urlopen(source_url) |
| 97 | html = response.read() |
| 98 | link_regex = re.compile('<a href="(%s)">' % filename_regex) |
| 99 | links = link_regex.findall(html) |
| 100 | for link in links: |
| 101 | DownloadBinaryFile('%s/%s' % (source_url, link), |
| 102 | '%s/%s' % (dest_dir, link)) |
| 103 | |
| 104 | def DownloadBinaryFile(source_url, dest_path): |
| 105 | """Download a single file from its source_url and save it to local disk |
| 106 | at dest_path. |
| 107 | |
| 108 | @param source_url |
| 109 | @param dest_path |
| 110 | """ |
| 111 | print 'DownloadBinaryFile: %s -> %s' % (source_url, dest_path) |
| 112 | url_fh = urllib2.urlopen(source_url) |
| 113 | local_fh = open(dest_path, 'wb') |
| 114 | local_fh.write(url_fh.read()) |
| 115 | local_fh.close() |
| 116 | |
| 117 | def Main(arglist): |
| 118 | """Download most recently generated baseline images for a given platform, |
| 119 | and add any new ones to SVN control. |
| 120 | |
| 121 | @param arglist sys.argv or equivalent |
| 122 | """ |
| 123 | num_args = len(arglist) |
| 124 | if num_args != 2: |
| 125 | raise Exception('usage: %s <baseline_subdir>' % __file__) |
| 126 | |
| 127 | baseline_subdir = arglist[1] |
| 128 | while baseline_subdir.endswith('/'): |
| 129 | baseline_subdir = baseline_subdir[:-1] |
| 130 | |
| 131 | results_url = GetLatestResultsUrl(baseline_subdir) |
| 132 | DownloadMatchingFiles(results_url, IMAGE_REGEX, baseline_subdir) |
| 133 | svn_handler = svn.Svn(baseline_subdir) |
| 134 | new_files = svn_handler.GetNewFiles() |
| 135 | if new_files: |
| 136 | svn_handler.AddFiles(new_files) |
| 137 | svn_handler.SetProperty(new_files, svn.PROPERTY_MIMETYPE, |
| 138 | IMAGE_MIMETYPE) |
| 139 | |
| 140 | if __name__ == '__main__': |
| 141 | Main(sys.argv) |