OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """ | 3 """ |
4 Copyright 2014 Google Inc. | 4 Copyright 2014 Google Inc. |
5 | 5 |
6 Use of this source code is governed by a BSD-style license that can be | 6 Use of this source code is governed by a BSD-style license that can be |
7 found in the LICENSE file. | 7 found in the LICENSE file. |
8 | 8 |
9 Download actual GM results for a particular builder. | 9 Download actual GM results for a particular builder. |
10 """ | 10 """ |
(...skipping 23 matching lines...) Expand all Loading... | |
34 TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) | 34 TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
35 GM_DIRECTORY = os.path.join(TRUNK_DIRECTORY, 'gm') | 35 GM_DIRECTORY = os.path.join(TRUNK_DIRECTORY, 'gm') |
36 TOOLS_DIRECTORY = os.path.join(TRUNK_DIRECTORY, 'tools') | 36 TOOLS_DIRECTORY = os.path.join(TRUNK_DIRECTORY, 'tools') |
37 if GM_DIRECTORY not in sys.path: | 37 if GM_DIRECTORY not in sys.path: |
38 sys.path.append(GM_DIRECTORY) | 38 sys.path.append(GM_DIRECTORY) |
39 if TOOLS_DIRECTORY not in sys.path: | 39 if TOOLS_DIRECTORY not in sys.path: |
40 sys.path.append(TOOLS_DIRECTORY) | 40 sys.path.append(TOOLS_DIRECTORY) |
41 import buildbot_globals | 41 import buildbot_globals |
42 import gm_json | 42 import gm_json |
43 | 43 |
44 DEFAULT_ACTUALS_BASE_URL = posixpath.join( | 44 # Imports from third-party code |
45 buildbot_globals.Get('autogen_svn_url'), 'gm-actual') | 45 APICLIENT_DIRECTORY = os.path.join( |
46 TRUNK_DIRECTORY, 'third_party', 'externals', 'google-api-python-client') | |
47 if APICLIENT_DIRECTORY not in sys.path: | |
48 sys.path.append(APICLIENT_DIRECTORY) | |
49 from googleapiclient.discovery import build as build_service | |
50 | |
51 | |
52 GM_SUMMARIES_BUCKET = buildbot_globals.Get('gm_summaries_bucket') | |
53 DEFAULT_ACTUALS_BASE_URL = ( | |
54 'http://storage.googleapis.com/%s' % GM_SUMMARIES_BUCKET) | |
46 DEFAULT_JSON_FILENAME = 'actual-results.json' | 55 DEFAULT_JSON_FILENAME = 'actual-results.json' |
47 | 56 |
48 | 57 |
49 class Download(object): | 58 class Download(object): |
50 | 59 |
51 def __init__(self, actuals_base_url=DEFAULT_ACTUALS_BASE_URL, | 60 def __init__(self, actuals_base_url=DEFAULT_ACTUALS_BASE_URL, |
52 json_filename=DEFAULT_JSON_FILENAME, | 61 json_filename=DEFAULT_JSON_FILENAME, |
53 gm_actuals_root_url=gm_json.GM_ACTUALS_ROOT_HTTP_URL): | 62 gm_actuals_root_url=gm_json.GM_ACTUALS_ROOT_HTTP_URL): |
54 """ | 63 """ |
55 Args: | 64 Args: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 results_of_this_type = actual_results_dict[result_type] | 98 results_of_this_type = actual_results_dict[result_type] |
90 if not results_of_this_type: | 99 if not results_of_this_type: |
91 continue | 100 continue |
92 for image_name in sorted(results_of_this_type.keys()): | 101 for image_name in sorted(results_of_this_type.keys()): |
93 (test, config) = self._image_filename_re.match(image_name).groups() | 102 (test, config) = self._image_filename_re.match(image_name).groups() |
94 (hash_type, hash_digest) = results_of_this_type[image_name] | 103 (hash_type, hash_digest) = results_of_this_type[image_name] |
95 source_url = gm_json.CreateGmActualUrl( | 104 source_url = gm_json.CreateGmActualUrl( |
96 test_name=test, hash_type=hash_type, hash_digest=hash_digest, | 105 test_name=test, hash_type=hash_type, hash_digest=hash_digest, |
97 gm_actuals_root_url=self._gm_actuals_root_url) | 106 gm_actuals_root_url=self._gm_actuals_root_url) |
98 dest_path = os.path.join(dest_dir, config, test + '.png') | 107 dest_path = os.path.join(dest_dir, config, test + '.png') |
108 # TODO(epoger): To speed this up, we should only download files that | |
109 # we don't already have on local disk. | |
99 copy_contents(source_url=source_url, dest_path=dest_path, | 110 copy_contents(source_url=source_url, dest_path=dest_path, |
100 create_subdirs_if_needed=True) | 111 create_subdirs_if_needed=True) |
101 | 112 |
102 | 113 |
103 def create_filepath_url(filepath): | 114 def create_filepath_url(filepath): |
104 """ Returns a file:/// URL pointing at the given filepath on local disk. | 115 """ Returns a file:/// URL pointing at the given filepath on local disk. |
105 | 116 |
106 For now, this is only used by unittests, but I anticipate it being useful | 117 For now, this is only used by unittests, but I anticipate it being useful |
107 in production, as a way for developers to run rebaseline_server over locally | 118 in production, as a way for developers to run rebaseline_server over locally |
108 generated images. | 119 generated images. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 """ | 155 """ |
145 if create_subdirs_if_needed: | 156 if create_subdirs_if_needed: |
146 dest_dir = os.path.dirname(dest_path) | 157 dest_dir = os.path.dirname(dest_path) |
147 if not os.path.exists(dest_dir): | 158 if not os.path.exists(dest_dir): |
148 os.makedirs(dest_dir) | 159 os.makedirs(dest_dir) |
149 with contextlib.closing(urllib.urlopen(source_url)) as source_handle: | 160 with contextlib.closing(urllib.urlopen(source_url)) as source_handle: |
150 with open(dest_path, 'wb') as dest_handle: | 161 with open(dest_path, 'wb') as dest_handle: |
151 shutil.copyfileobj(fsrc=source_handle, fdst=dest_handle) | 162 shutil.copyfileobj(fsrc=source_handle, fdst=dest_handle) |
152 | 163 |
153 | 164 |
165 def gcs_list_bucket_contents(bucket, subdir=None): | |
166 """ Returns files in the Google Cloud Storage bucket as a (dirs, files) tuple. | |
167 | |
168 Uses the API documented at | |
169 https://developers.google.com/storage/docs/json_api/v1/objects/list | |
170 | |
171 Args: | |
172 bucket: name of the Google Storage bucket | |
173 subdir: directory within the bucket to list, or None for root directory | |
174 """ | |
175 # The GCS command relies on the subdir name (if any) ending with a slash. | |
176 if subdir and not subdir.endswith('/'): | |
177 subdir += '/' | |
178 subdir_length = len(subdir) if subdir else 0 | |
179 | |
180 storage = build_service('storage', 'v1') | |
181 command = storage.objects().list( | |
182 bucket=bucket, delimiter='/', fields='items(name),prefixes', | |
183 prefix=subdir) | |
184 results = command.execute() | |
185 | |
186 # The GCS command returned two subdicts: | |
187 # prefixes: the full path of every directory within subdir, with trailing '/' | |
188 # items: property dict for each file object within subdir | |
189 # (including 'name', which is full path of the object) | |
190 dirs = [] | |
191 for dir_fullpath in results.get('prefixes', []): | |
epoger
2014/06/03 17:08:32
Ravi- I hope this is easier to follow.
| |
192 dir_basename = dir_fullpath[subdir_length:] | |
193 dirs.append(dir_basename[:-1]) # strip trailing slash | |
194 files = [] | |
195 for file_properties in results.get('items', []): | |
196 file_fullpath = file_properties['name'] | |
197 file_basename = file_fullpath[subdir_length:] | |
198 files.append(file_basename) | |
199 return (dirs, files) | |
200 | |
201 | |
154 def main(): | 202 def main(): |
155 parser = optparse.OptionParser() | 203 parser = optparse.OptionParser() |
156 required_params = [] | 204 required_params = [] |
157 parser.add_option('--actuals-base-url', | 205 parser.add_option('--actuals-base-url', |
158 action='store', type='string', | 206 action='store', type='string', |
159 default=DEFAULT_ACTUALS_BASE_URL, | 207 default=DEFAULT_ACTUALS_BASE_URL, |
160 help=('Base URL from which to read files containing JSON ' | 208 help=('Base URL from which to read files containing JSON ' |
161 'summaries of actual GM results; defaults to ' | 209 'summaries of actual GM results; defaults to ' |
162 '"%default". To get a specific revision (useful for ' | 210 '"%default".')) |
163 'trybots) replace "svn" with "svn-history/r123".')) | |
164 # TODO(epoger): Rather than telling the user to run "svn ls" to get the list | |
165 # of builders, add a --list-builders option that will print the list. | |
166 required_params.append('builder') | 211 required_params.append('builder') |
212 # TODO(epoger): Before https://codereview.chromium.org/309653005 , when this | |
213 # tool downloaded the JSON summaries from skia-autogen, it had the ability | |
214 # to get results as of a specific revision number. We should add similar | |
215 # functionality when retrieving the summaries from Google Storage. | |
167 parser.add_option('--builder', | 216 parser.add_option('--builder', |
168 action='store', type='string', | 217 action='store', type='string', |
169 help=('REQUIRED: Which builder to download results for. ' | 218 help=('REQUIRED: Which builder to download results for. ' |
170 'To see a list of builders, run "svn ls %s".' % | 219 'To see a list of builders, run with the ' |
171 DEFAULT_ACTUALS_BASE_URL)) | 220 '--list-builders option set.')) |
172 required_params.append('dest_dir') | 221 required_params.append('dest_dir') |
173 parser.add_option('--dest-dir', | 222 parser.add_option('--dest-dir', |
174 action='store', type='string', | 223 action='store', type='string', |
175 help=('REQUIRED: Directory where all images should be ' | 224 help=('REQUIRED: Directory where all images should be ' |
176 'written. If this directory does not exist yet, it ' | 225 'written. If this directory does not exist yet, it ' |
177 'will be created.')) | 226 'will be created.')) |
178 parser.add_option('--json-filename', | 227 parser.add_option('--json-filename', |
179 action='store', type='string', | 228 action='store', type='string', |
180 default=DEFAULT_JSON_FILENAME, | 229 default=DEFAULT_JSON_FILENAME, |
181 help=('JSON summary filename to read for each builder; ' | 230 help=('JSON summary filename to read for each builder; ' |
182 'defaults to "%default".')) | 231 'defaults to "%default".')) |
232 parser.add_option('--list-builders', action='store_true', | |
233 help=('List all available builders.')) | |
183 (params, remaining_args) = parser.parse_args() | 234 (params, remaining_args) = parser.parse_args() |
184 | 235 |
236 if params.list_builders: | |
237 dirs, _ = gcs_list_bucket_contents(bucket=GM_SUMMARIES_BUCKET) | |
238 print '\n'.join(dirs) | |
239 return | |
240 | |
185 # Make sure all required options were set, | 241 # Make sure all required options were set, |
186 # and that there were no items left over in the command line. | 242 # and that there were no items left over in the command line. |
187 for required_param in required_params: | 243 for required_param in required_params: |
188 if not getattr(params, required_param): | 244 if not getattr(params, required_param): |
189 raise Exception('required option \'%s\' was not set' % required_param) | 245 raise Exception('required option \'%s\' was not set' % required_param) |
190 if len(remaining_args) is not 0: | 246 if len(remaining_args) is not 0: |
191 raise Exception('extra items specified in the command line: %s' % | 247 raise Exception('extra items specified in the command line: %s' % |
192 remaining_args) | 248 remaining_args) |
193 | 249 |
194 downloader = Download(actuals_base_url=params.actuals_base_url) | 250 downloader = Download(actuals_base_url=params.actuals_base_url) |
195 downloader.fetch(builder_name=params.builder, | 251 downloader.fetch(builder_name=params.builder, |
196 dest_dir=params.dest_dir) | 252 dest_dir=params.dest_dir) |
197 | 253 |
198 | 254 |
199 | 255 |
200 if __name__ == '__main__': | 256 if __name__ == '__main__': |
201 main() | 257 main() |
OLD | NEW |