Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
|
epoger
2013/07/11 14:14:25
Some high-level, long-term thoughts:
A. This scri
| |
| 2 | |
| 3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 | |
| 8 """ Look through skia-autogen, searching for all checksums which should have | |
| 9 corresponding files in Google Storage, and verify that those files exist. """ | |
| 10 | |
| 11 | |
| 12 import json | |
| 13 import posixpath | |
| 14 import re | |
| 15 import subprocess | |
| 16 import sys | |
| 17 | |
| 18 | |
| 19 AUTOGEN_URL = 'http://skia-autogen.googlecode.com/svn/gm-actual' | |
| 20 GS_URL = 'gs://chromium-skia-gm/gm' | |
| 21 TEST_NAME_PATTERN = re.compile('(\S+)_(\S+).png') | |
| 22 | |
| 23 | |
| 24 def FileNameToGSURL(filename, hash_type, hash_value): | |
| 25 """ Convert a file name given in a checksum file to the URL of the | |
| 26 corresponding image file in Google Storage. | |
| 27 | |
| 28 filename: string; the file name to convert. Takes the form specified by | |
| 29 TEST_NAME_PATTERN. | |
| 30 hash_type: string; the type of the checksum. | |
| 31 hash_value: string; the checksum itself. | |
| 32 """ | |
| 33 test_name = TEST_NAME_PATTERN.match(filename).group(1) | |
| 34 if not test_name: | |
| 35 raise Exception('Invalid test name for file: %s' % filename) | |
| 36 return '%s/%s/%s/%s.png' % (GS_URL, hash_type, test_name, hash_value) | |
| 37 | |
| 38 | |
| 39 def FindURLSInJSON(json_file, gs_urls): | |
| 40 """ Extract Google Storage URLs from a JSON file in svn, adding them to the | |
| 41 gs_urls dictionary. | |
| 42 | |
| 43 json_file: string; URL of the JSON file. | |
| 44 gs_urls: dict; stores Google Storage URLs as keys and lists of the JSON files | |
|
epoger
2013/07/11 14:14:25
optional: I find that a small example of a valid d
| |
| 45 which reference them. | |
| 46 """ | |
| 47 output = subprocess.check_output(['svn', 'cat', json_file]) | |
| 48 json_content = json.loads(output) | |
| 49 for dict_type in ['actual-results']: | |
| 50 for result_type in json_content[dict_type]: | |
| 51 if json_content[dict_type][result_type]: | |
| 52 for result in json_content[dict_type][result_type].keys(): | |
| 53 hash_type, hash_value = json_content[dict_type][result_type][result] | |
| 54 gs_url = FileNameToGSURL(result, hash_type, str(hash_value)) | |
| 55 if gs_urls.get(gs_url): | |
| 56 gs_urls[gs_url].append(json_file) | |
| 57 else: | |
| 58 gs_urls[gs_url] = [json_file] | |
| 59 | |
| 60 | |
| 61 def _FindJSONFiles(url, json_files): | |
| 62 """ Helper function for FindJsonFiles. Recursively explore the repository, | |
| 63 adding JSON files to a list. | |
| 64 | |
| 65 url: string; URL of the repository (or subdirectory thereof) to explore. | |
| 66 json_files: list to which JSON file urls will be added. | |
| 67 """ | |
| 68 proc = subprocess.Popen(['svn', 'ls', url], stdout=subprocess.PIPE, | |
| 69 stderr=subprocess.STDOUT) | |
| 70 if proc.wait() != 0: | |
| 71 raise Exception('Failed to list svn directory.') | |
| 72 output = proc.communicate()[0].splitlines() | |
| 73 subdirs = [] | |
| 74 for item in output: | |
| 75 if item.endswith(posixpath.sep): | |
| 76 subdirs.append(item) | |
| 77 elif item.endswith('.json'): | |
| 78 json_files.append(posixpath.join(url, item)) | |
| 79 else: | |
| 80 print 'Warning: ignoring %s' % posixpath.join(url, item) | |
| 81 for subdir in subdirs: | |
| 82 _FindJSONFiles(posixpath.join(url, subdir), json_files) | |
| 83 | |
| 84 | |
| 85 def FindJSONFiles(url): | |
| 86 """ Recursively explore the given repository and return a list of the JSON | |
| 87 files it contains. | |
| 88 | |
| 89 url: string; URL of the repository to explore. | |
| 90 """ | |
| 91 print 'Searching for JSON files in %s' % url | |
| 92 json_files = [] | |
| 93 _FindJSONFiles(url, json_files) | |
| 94 return json_files | |
| 95 | |
| 96 | |
| 97 def FindURLs(url): | |
| 98 """ Find Google Storage URLs inside of JSON files in the given repository. | |
| 99 Returns a dictionary whose keys are Google Storage URLs and values are lists | |
| 100 of the JSON files which reference them. | |
| 101 | |
| 102 url: string; URL of the repository to explore. | |
| 103 """ | |
| 104 gs_urls = {} | |
| 105 for json_file in FindJSONFiles(url): | |
| 106 print 'Looking for checksums in %s' % json_file | |
| 107 FindURLSInJSON(json_file, gs_urls) | |
| 108 return gs_urls | |
| 109 | |
| 110 | |
| 111 def VerifyURL(url): | |
| 112 """ Verify that the given URL exists. | |
| 113 | |
| 114 url: string; the Google Storage URL of the image file in question. | |
| 115 """ | |
| 116 proc = subprocess.Popen(['gsutil', 'ls', url], stdout=subprocess.PIPE, | |
| 117 stderr=subprocess.STDOUT) | |
| 118 if proc.wait() != 0: | |
| 119 return False | |
| 120 return True | |
| 121 | |
| 122 | |
| 123 def VerifyURLs(urls): | |
| 124 """ Verify that each of the given URLs exists. Return a list of which URLs do | |
| 125 not exist. | |
| 126 | |
| 127 urls: dictionary; URLs of the image files in question. | |
| 128 """ | |
| 129 print 'Verifying that images exist for URLs...' | |
| 130 missing = [] | |
| 131 for url in urls.iterkeys(): | |
| 132 if not VerifyURL(url): | |
| 133 print 'Missing: %s, referenced by: \n %s' % (url, '\n '.join(urls[url])) | |
| 134 missing.append(url) | |
| 135 return missing | |
| 136 | |
| 137 | |
| 138 def Main(): | |
| 139 urls = FindURLs(AUTOGEN_URL) | |
| 140 missing = VerifyURLs(urls) | |
| 141 if missing: | |
| 142 print 'Found %d Missing files.' % len(missing) | |
| 143 return 1 | |
| 144 | |
| 145 | |
| 146 if __name__ == '__main__': | |
| 147 sys.exit(Main()) | |
| OLD | NEW |