| OLD | NEW |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Presubmit script for changes affecting content/test/gpu/page_sets/. |
| 6 |
| 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 for more details about the presubmit API built into depot_tools. |
| 9 """ |
| 10 |
| 11 import os |
| 5 import sys | 12 import sys |
| 6 | 13 |
| 7 | 14 |
| 8 def _GetChromiumSrcDir(input_api): | 15 def _GetChromiumSrcDir(input_api): |
| 9 return input_api.os_path.abspath(input_api.os_path.join( | 16 return input_api.os_path.abspath(input_api.os_path.join( |
| 10 input_api.PresubmitLocalPath(), '..', '..', '..', '..')) | 17 input_api.PresubmitLocalPath(), '..', '..', '..', '..')) |
| 11 | 18 |
| 12 | 19 |
| 13 def LoadSupport(input_api): | 20 def _CheckWprShaFiles(input_api, output_api): |
| 14 if 'cloud_storage' not in globals(): | 21 """Check whether the wpr sha files have matching URLs.""" |
| 15 # Avoid leaking changes to global sys.path. | 22 old_sys_path = sys.path |
| 16 _old_sys_path = sys.path | 23 try: |
| 17 try: | 24 catapult_base_path = input_api.os_path.join( |
| 18 catapult_base_path = input_api.os_path.join( | 25 _GetChromiumSrcDir(input_api), 'third_party', 'catapult', |
| 19 _GetChromiumSrcDir(input_api), 'third_party', 'catapult', | 26 'catapult_base') |
| 20 'catapult_base') | 27 sys.path.insert(1, catapult_base_path) |
| 21 sys.path = [catapult_base_path] + sys.path | 28 from catapult_base import cloud_storage # pylint: disable=import-error |
| 22 from catapult_base import cloud_storage | 29 finally: |
| 23 globals()['cloud_storage'] = cloud_storage | 30 sys.path = old_sys_path |
| 24 finally: | |
| 25 sys.path = _old_sys_path | |
| 26 | 31 |
| 27 return globals()['cloud_storage'] | 32 results = [] |
| 33 for affected_file in input_api.AffectedFiles(include_deletes=False): |
| 34 filename = affected_file.AbsoluteLocalPath() |
| 35 if not filename.endswith('wpr.sha1'): |
| 36 continue |
| 37 expected_hash = cloud_storage.ReadHash(filename) |
| 38 is_wpr_file_uploaded = any( |
| 39 cloud_storage.Exists(bucket, expected_hash) |
| 40 for bucket in cloud_storage.BUCKET_ALIASES.itervalues()) |
| 41 if not is_wpr_file_uploaded: |
| 42 wpr_filename = filename[:-5] |
| 43 results.append(output_api.PresubmitError( |
| 44 'The file matching %s is not in Cloud Storage yet.\n' |
| 45 'You can upload your new WPR archive file with the command:\n' |
| 46 'depot_tools/upload_to_google_storage.py --bucket ' |
| 47 '<Your pageset\'s bucket> %s.\nFor more info: see ' |
| 48 'http://www.chromium.org/developers/telemetry/' |
| 49 'record_a_page_set#TOC-Upload-the-recording-to-Cloud-Storage' % |
| 50 (filename, wpr_filename))) |
| 51 return results |
| 28 | 52 |
| 29 | 53 |
| 30 def _GetFilesNotInCloud(input_api): | 54 def _CheckJson(input_api, output_api): |
| 31 """Searches for .sha1 files and checks to see if they have already | 55 """Checks whether JSON files in this change can be parsed.""" |
| 32 been uploaded Cloud Storage. Returns a list of those that have not. | |
| 33 """ | |
| 34 hash_paths = [] | |
| 35 for affected_file in input_api.AffectedFiles(include_deletes=False): | 56 for affected_file in input_api.AffectedFiles(include_deletes=False): |
| 36 hash_path = affected_file.AbsoluteLocalPath() | 57 filename = affected_file.AbsoluteLocalPath() |
| 37 _, extension = input_api.os_path.splitext(hash_path) | 58 if os.path.splitext(filename)[1] != '.json': |
| 38 if extension == '.sha1': | 59 continue |
| 39 hash_paths.append(hash_path) | 60 try: |
| 40 if not hash_paths: | 61 input_api.json.load(open(filename)) |
| 41 return [] | 62 except ValueError: |
| 42 | 63 return [output_api.PresubmitError('Error parsing JSON in %s!' % filename)] |
| 43 cloud_storage = LoadSupport(input_api) | 64 return [] |
| 44 | |
| 45 # Look in both buckets, in case the user uploaded the file manually. | |
| 46 hashes_in_cloud_storage = cloud_storage.List(cloud_storage.PUBLIC_BUCKET) | |
| 47 try: | |
| 48 hashes_in_cloud_storage += cloud_storage.List(cloud_storage.INTERNAL_BUCKET) | |
| 49 except (cloud_storage.PermissionError, cloud_storage.CredentialsError): | |
| 50 pass | |
| 51 | |
| 52 files = [] | |
| 53 for hash_path in hash_paths: | |
| 54 file_hash = cloud_storage.ReadHash(hash_path) | |
| 55 if file_hash not in hashes_in_cloud_storage: | |
| 56 files.append((hash_path, file_hash)) | |
| 57 | |
| 58 return files | |
| 59 | 65 |
| 60 | 66 |
| 61 def _VerifyFilesInCloud(input_api, output_api): | 67 def _CommonChecks(input_api, output_api): |
| 62 """Fails presubmit if any .sha1 files have not been previously uploaded to | 68 """Performs common checks, which includes running pylint.""" |
| 63 Cloud storage. | |
| 64 """ | |
| 65 results = [] | 69 results = [] |
| 66 hash_paths = _GetFilesNotInCloud(input_api) | 70 results.extend(_CheckWprShaFiles(input_api, output_api)) |
| 67 file_paths = [] | 71 results.extend(_CheckJson(input_api, output_api)) |
| 68 for hash_path, _ in hash_paths: | |
| 69 results.append(output_api.PresubmitError( | |
| 70 'Attemping to commit hash file, but corresponding ' | |
| 71 'data file is not in Cloud Storage: %s' % hash_path)) | |
| 72 file_paths.append(input_api.os_path.splitext(hash_path)[0]) | |
| 73 | |
| 74 if len(file_paths) > 0: | |
| 75 upload_script_path = input_api.os_path.join( | |
| 76 _GetChromiumSrcDir(input_api), 'tools', 'telemetry', 'cloud_storage') | |
| 77 results.append(output_api.PresubmitError( | |
| 78 'To upload missing files, Run: \n' | |
| 79 '%s upload %s google-only' % | |
| 80 (upload_script_path, ' '.join(file_paths)))) | |
| 81 return results | 72 return results |
| 82 | 73 |
| 83 | 74 |
| 84 def CheckChangeOnUpload(input_api, output_api): | 75 def CheckChangeOnUpload(input_api, output_api): |
| 85 results = _VerifyFilesInCloud(input_api, output_api) | 76 results = [] |
| 77 results.extend(_CommonChecks(input_api, output_api)) |
| 86 return results | 78 return results |
| 87 | 79 |
| 88 | 80 |
| 89 def CheckChangeOnCommit(input_api, output_api): | 81 def CheckChangeOnCommit(input_api, output_api): |
| 90 results = _VerifyFilesInCloud(input_api, output_api) | 82 results = [] |
| 83 results.extend(_CommonChecks(input_api, output_api)) |
| 91 return results | 84 return results |
| OLD | NEW |