| OLD | NEW |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. | 1 # Copyright 2017 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 """Helper functions to upload data to Google Storage. | 5 """Helper functions to upload data to Google Storage. |
| 6 | 6 |
| 7 Text data should be streamed to logdog using |logdog_helper| module. | 7 Text data should be streamed to logdog using |logdog_helper| module. |
| 8 Due to logdog not having image or HTML viewer, those instead should be uploaded | 8 Due to logdog not having image or HTML viewer, those instead should be uploaded |
| 9 to Google Storage directly using this module. | 9 to Google Storage directly using this module. |
| 10 """ | 10 """ |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 Web link to item uploaded to Google Storage bucket. | 46 Web link to item uploaded to Google Storage bucket. |
| 47 """ | 47 """ |
| 48 if bucket.startswith('gs://'): | 48 if bucket.startswith('gs://'): |
| 49 bucket = bucket[len('gs://'):] | 49 bucket = bucket[len('gs://'):] |
| 50 if bucket.endswith('/'): | 50 if bucket.endswith('/'): |
| 51 bucket = bucket[:-1] | 51 bucket = bucket[:-1] |
| 52 | 52 |
| 53 gs_path = 'gs://%s/%s' % (bucket, name) | 53 gs_path = 'gs://%s/%s' % (bucket, name) |
| 54 logging.info('Uploading %s to %s', filepath, gs_path) | 54 logging.info('Uploading %s to %s', filepath, gs_path) |
| 55 | 55 |
| 56 cmd = [_GSUTIL_PATH] | 56 cmd = [_GSUTIL_PATH, '-q'] |
| 57 if content_type: | 57 if content_type: |
| 58 cmd.extend(['-h', 'Content-Type:%s' % content_type]) | 58 cmd.extend(['-h', 'Content-Type:%s' % content_type]) |
| 59 cmd.extend(['cp', filepath, gs_path]) | 59 cmd.extend(['cp', filepath, gs_path]) |
| 60 | 60 |
| 61 cmd_helper.RunCmd(cmd) | 61 cmd_helper.RunCmd(cmd) |
| 62 | 62 |
| 63 url_template = _AUTHENTICATED_URL if authenticated_link else _PUBLIC_URL | 63 url_template = _AUTHENTICATED_URL if authenticated_link else _PUBLIC_URL |
| 64 return os.path.join(url_template % bucket, name) | 64 return os.path.join(url_template % bucket, name) |
| 65 | 65 |
| 66 | 66 |
| 67 def unique_name(basename, suffix='', timestamp=True, device=None): | 67 def unique_name(basename, suffix='', timestamp=True, device=None): |
| 68 """Helper function for creating a unique name for a file to store in GS. | 68 """Helper function for creating a unique name for a file to store in GS. |
| 69 | 69 |
| 70 Args: | 70 Args: |
| 71 basename: Base of the unique filename. | 71 basename: Base of the unique filename. |
| 72 suffix: Suffix of filename. | 72 suffix: Suffix of filename. |
| 73 timestamp: Whether or not to add a timestamp to name. | 73 timestamp: Whether or not to add a timestamp to name. |
| 74 device: Device to add device serial of to name. | 74 device: Device to add device serial of to name. |
| 75 """ | 75 """ |
| 76 return '%s%s%s%s' % ( | 76 return '%s%s%s%s' % ( |
| 77 basename, | 77 basename, |
| 78 '_%s' % time.strftime('%Y_%m_%d_T%H_%M_%S-UTC', time.gmtime()) | 78 '_%s' % time.strftime('%Y_%m_%d_T%H_%M_%S-UTC', time.gmtime()) |
| 79 if timestamp else '', | 79 if timestamp else '', |
| 80 '_%s' % device.serial if device else '', | 80 '_%s' % device.serial if device else '', |
| 81 suffix) | 81 suffix) |
| OLD | NEW |