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 import os | 5 import os |
5 import re | 6 import re |
6 import sys | 7 import sys |
7 | 8 |
8 | 9 |
9 def _SyncFilesToCloud(input_api, output_api): | 10 def _SyncFilesToCloud(input_api, output_api): |
10 """Searches for .sha1 files and uploads them to Cloud Storage. | 11 """Searches for .sha1 files and uploads them to Cloud Storage. |
11 | 12 |
12 It validates all the hashes and skips upload if not necessary. | 13 It validates all the hashes and skips upload if not necessary. |
13 """ | 14 """ |
14 # Because this script will be called from a magic PRESUBMIT demon, | 15 # Because this script will be called from a magic PRESUBMIT demon, |
15 # avoid angering it; don't pollute its sys.path. | 16 # avoid angering it; don't pollute its sys.path. |
16 old_sys_path = sys.path | 17 old_sys_path = sys.path |
17 try: | 18 try: |
18 sys.path = [os.path.join(os.pardir, os.pardir, 'telemetry')] + sys.path | 19 sys.path = [os.path.join(os.pardir, os.pardir, 'telemetry')] + sys.path |
19 from telemetry.page import cloud_storage | 20 from telemetry.page import cloud_storage |
20 finally: | 21 finally: |
21 sys.path = old_sys_path | 22 sys.path = old_sys_path |
22 | 23 |
23 hashes_in_cloud_storage = cloud_storage.List(cloud_storage.DEFAULT_BUCKET) | 24 # Look in both buckets, in case the user uploaded the file manually. But this |
| 25 # script focuses on WPR archives, so it only uploads to the internal bucket. |
| 26 hashes_in_cloud_storage = cloud_storage.List(cloud_storage.INTERNAL_BUCKET) |
| 27 hashes_in_cloud_storage += cloud_storage.List(cloud_storage.PUBLIC_BUCKET) |
24 | 28 |
25 results = [] | 29 results = [] |
26 for hash_path in input_api.AbsoluteLocalPaths(): | 30 for hash_path in input_api.AbsoluteLocalPaths(): |
27 file_path, extension = os.path.splitext(hash_path) | 31 file_path, extension = os.path.splitext(hash_path) |
28 if extension != '.sha1': | 32 if extension != '.sha1': |
29 continue | 33 continue |
30 | 34 |
31 with open(hash_path, 'rb') as f: | 35 with open(hash_path, 'rb') as f: |
32 file_hash = f.read(1024).rstrip() | 36 file_hash = f.read(1024).rstrip() |
33 if file_hash in hashes_in_cloud_storage: | 37 if file_hash in hashes_in_cloud_storage: |
34 results.append(output_api.PresubmitNotifyResult( | 38 results.append(output_api.PresubmitNotifyResult( |
35 'File already in Cloud Storage, skipping upload: %s' % hash_path)) | 39 'File already in Cloud Storage, skipping upload: %s' % hash_path)) |
36 continue | 40 continue |
37 | 41 |
38 if not re.match('^([A-Za-z0-9]{40})$', file_hash): | 42 if not re.match('^([A-Za-z0-9]{40})$', file_hash): |
39 results.append(output_api.PresubmitError( | 43 results.append(output_api.PresubmitError( |
40 'Hash file does not contain a valid SHA-1 hash: %s' % hash_path)) | 44 'Hash file does not contain a valid SHA-1 hash: %s' % hash_path)) |
41 continue | 45 continue |
42 if not os.path.exists(file_path): | 46 if not os.path.exists(file_path): |
43 results.append(output_api.PresubmitError( | 47 results.append(output_api.PresubmitError( |
44 'Hash file exists, but file not found: %s' % hash_path)) | 48 'Hash file exists, but file not found: %s' % hash_path)) |
45 continue | 49 continue |
46 if cloud_storage.GetHash(file_path) != file_hash: | 50 if cloud_storage.GetHash(file_path) != file_hash: |
47 results.append(output_api.PresubmitError( | 51 results.append(output_api.PresubmitError( |
48 'Hash file does not match file\'s actual hash: %s' % hash_path)) | 52 'Hash file does not match file\'s actual hash: %s' % hash_path)) |
49 continue | 53 continue |
50 | 54 |
51 try: | 55 try: |
52 cloud_storage.Insert(cloud_storage.DEFAULT_BUCKET, file_hash, file_path) | 56 cloud_storage.Insert(cloud_storage.INTERNAL_BUCKET, file_hash, file_path) |
53 except cloud_storage.CloudStorageError: | 57 except cloud_storage.CloudStorageError: |
54 results.append(output_api.PresubmitError( | 58 results.append(output_api.PresubmitError( |
55 'Unable to upload to Cloud Storage: %s' % hash_path)) | 59 'Unable to upload to Cloud Storage: %s' % hash_path)) |
56 | 60 |
57 return results | 61 return results |
58 | 62 |
59 | 63 |
60 def CheckChangeOnCommit(input_api, output_api): | 64 def CheckChangeOnCommit(input_api, output_api): |
61 results = [] | 65 results = [] |
62 results += _SyncFilesToCloud(input_api, output_api) | 66 results += _SyncFilesToCloud(input_api, output_api) |
63 return results | 67 return results |
OLD | NEW |