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 3 # found in the LICENSE file. 4 5 import os 6 import re 7 import sys 8 9 def _GetTelemetryPath(input_api): 10 return os.path.join( 11 os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname( 12 input_api.PresubmitLocalPath())))), 'tools', 'telemetry') 13 14 def LoadSupport(input_api): 15 if 'cloud_storage' not in globals(): 16 # Avoid leaking changes to global sys.path. 17 _old_sys_path = sys.path 18 try: 19 telemetry_path = _GetTelemetryPath(input_api) 20 sys.path = [telemetry_path] + sys.path 21 from telemetry.util import cloud_storage 22 globals()['cloud_storage'] = cloud_storage 23 finally: 24 sys.path = _old_sys_path 25 26 return globals()['cloud_storage'] 27 28 29 def _GetFilesNotInCloud(input_api): 30 """Searches for .sha1 files and checks to see if they have already 31 been uploaded Cloud Storage. Returns a list of those that have not. 32 """ 33 hash_paths = [] 34 for affected_file in input_api.AffectedFiles(include_deletes=False): 35 hash_path = affected_file.AbsoluteLocalPath() 36 _, extension = os.path.splitext(hash_path) 37 if extension == '.sha1': 38 hash_paths.append(hash_path) 39 if not hash_paths: 40 return [] 41 42 cloud_storage = LoadSupport(input_api) 43 44 # Look in both buckets, in case the user uploaded the file manually. 45 hashes_in_cloud_storage = cloud_storage.List(cloud_storage.PUBLIC_BUCKET) 46 try: 47 hashes_in_cloud_storage += cloud_storage.List(cloud_storage.INTERNAL_BUCKET) 48 except (cloud_storage.PermissionError, cloud_storage.CredentialsError): 49 pass 50 51 files = [] 52 for hash_path in hash_paths: 53 file_hash = cloud_storage.ReadHash(hash_path) 54 if file_hash not in hashes_in_cloud_storage: 55 files.append((hash_path, file_hash)) 56 57 return files 58 59 60 def _VerifyFilesInCloud(input_api, output_api): 61 """Fails presubmit if any .sha1 files have not been previously uploaded to 62 Cloud storage. 63 """ 64 results = [] 65 hash_paths = _GetFilesNotInCloud(input_api) 66 file_paths = [] 67 for hash_path, _ in hash_paths: 68 results.append(output_api.PresubmitError( 69 'Attemping to commit hash file, but corresponding ' 70 'data file is not in Cloud Storage: %s' % hash_path)) 71 file_paths.append(os.path.splitext(hash_path)[0]) 72 73 if len(file_paths) > 0: 74 upload_script_path = os.path.join( 75 _GetTelemetryPath(input_api), 'cloud_storage') 76 results.append(output_api.PresubmitError( 77 'To upload missing files, Run: \n' 78 '%s upload %s google-only' % 79 (upload_script_path, ' '.join(file_paths)))) 80 return results 81 82 83 def CheckChangeOnUpload(input_api, output_api): 84 results = _VerifyFilesInCloud(input_api, output_api) 85 return results 86 87 88 def CheckChangeOnCommit(input_api, output_api): 89 results = _VerifyFilesInCloud(input_api, output_api) 90 return results 91