Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / test / gpu / page_sets / PRESUBMIT.py
1 # Copyright 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
10 # Avoid leaking changes to global sys.path.
11 _old_sys_path = sys.path
12 try:
13   telemetry_dir = os.path.abspath(os.path.join(
14       os.pardir, os.pardir, os.pardir, os.pardir, 'tools', 'telemetry'))
15   sys.path.append(telemetry_dir)
16   from telemetry.page import cloud_storage
17 finally:
18   sys.path = _old_sys_path
19
20
21 def _SyncFilesToCloud(input_api, output_api):
22   """Searches for .sha1 files and uploads them to Cloud Storage.
23
24   It validates all the hashes and skips upload if not necessary.
25   """
26   # Look in both buckets, in case the user uploaded the file manually. But this
27   # script focuses on WPR archives, so it only uploads to the internal bucket.
28   hashes_in_cloud_storage = cloud_storage.List(cloud_storage.INTERNAL_BUCKET)
29   hashes_in_cloud_storage += cloud_storage.List(cloud_storage.PUBLIC_BUCKET)
30
31   results = []
32   for affected_file in input_api.AffectedFiles(include_deletes=False):
33     hash_path = affected_file.AbsoluteLocalPath()
34     file_path, extension = os.path.splitext(hash_path)
35     if extension != '.sha1':
36       continue
37
38     with open(hash_path, 'rb') as f:
39       file_hash = f.read(1024).rstrip()
40     if file_hash in hashes_in_cloud_storage:
41       results.append(output_api.PresubmitNotifyResult(
42           'File already in Cloud Storage, skipping upload: %s' % hash_path))
43       continue
44
45     if not re.match('^([A-Za-z0-9]{40})$', file_hash):
46       results.append(output_api.PresubmitError(
47           'Hash file does not contain a valid SHA-1 hash: %s' % hash_path))
48       continue
49     if not os.path.exists(file_path):
50       results.append(output_api.PresubmitError(
51           'Hash file exists, but file not found: %s' % hash_path))
52       continue
53     if cloud_storage.GetHash(file_path) != file_hash:
54       results.append(output_api.PresubmitError(
55           'Hash file does not match file\'s actual hash: %s' % hash_path))
56       continue
57
58     try:
59       cloud_storage.Insert(cloud_storage.INTERNAL_BUCKET, file_hash, file_path)
60       results.append(output_api.PresubmitNotifyResult(
61           'Uploaded file to Cloud Storage: %s' % hash_path))
62     except cloud_storage.CloudStorageError, e:
63       results.append(output_api.PresubmitError(
64           'Unable to upload to Cloud Storage: %s\n\n%s' % (hash_path, e)))
65
66   return results
67
68
69 def CheckChangeOnUpload(input_api, output_api):
70   return _SyncFilesToCloud(input_api, output_api)
71
72
73 def CheckChangeOnCommit(input_api, output_api):
74   return _SyncFilesToCloud(input_api, output_api)