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.
9 def _GetTelemetryPath(input_api
):
11 os
.path
.dirname(os
.path
.dirname(os
.path
.dirname(os
.path
.dirname(
12 input_api
.PresubmitLocalPath())))), 'tools', 'telemetry')
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
19 telemetry_path
= _GetTelemetryPath(input_api
)
20 sys
.path
= [telemetry_path
] + sys
.path
21 from catapult_base
import cloud_storage
22 globals()['cloud_storage'] = cloud_storage
24 sys
.path
= _old_sys_path
26 return globals()['cloud_storage']
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.
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
)
42 cloud_storage
= LoadSupport(input_api
)
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
)
47 hashes_in_cloud_storage
+= cloud_storage
.List(cloud_storage
.INTERNAL_BUCKET
)
48 except (cloud_storage
.PermissionError
, cloud_storage
.CredentialsError
):
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
))
60 def _VerifyFilesInCloud(input_api
, output_api
):
61 """Fails presubmit if any .sha1 files have not been previously uploaded to
65 hash_paths
= _GetFilesNotInCloud(input_api
)
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])
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
))))
83 def CheckChangeOnUpload(input_api
, output_api
):
84 results
= _VerifyFilesInCloud(input_api
, output_api
)
88 def CheckChangeOnCommit(input_api
, output_api
):
89 results
= _VerifyFilesInCloud(input_api
, output_api
)