| OLD | NEW |
| (Empty) |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Helper functions to upload data to Google Storage. | |
| 6 | |
| 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 | |
| 9 to Google Storage directly using this module. | |
| 10 """ | |
| 11 | |
| 12 import logging | |
| 13 import os | |
| 14 import sys | |
| 15 import time | |
| 16 | |
| 17 from devil.utils import cmd_helper | |
| 18 from pylib.constants import host_paths | |
| 19 from pylib.utils import decorators | |
| 20 | |
| 21 sys.path.append(os.path.join(host_paths.DIR_SOURCE_ROOT, 'build')) | |
| 22 import find_depot_tools # pylint: disable=import-error | |
| 23 | |
| 24 _URL_TEMPLATE = 'https://storage.googleapis.com/%s/' | |
| 25 | |
| 26 | |
| 27 @decorators.NoRaiseException(default_return_value='') | |
| 28 def upload(name, filepath, bucket): | |
| 29 """Uploads data to Google Storage. | |
| 30 | |
| 31 Args: | |
| 32 name: Name of the file on Google Storage. | |
| 33 filepath: Path to file you want to upload. | |
| 34 bucket: Bucket to upload file to. | |
| 35 """ | |
| 36 gs_path = os.path.join('gs://%s/' % bucket, name) | |
| 37 logging.info('Uploading %s to %s', filepath, gs_path) | |
| 38 cmd_helper.RunCmd( | |
| 39 [os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gsutil.py'), 'cp', | |
| 40 filepath, gs_path]) | |
| 41 | |
| 42 return os.path.join(_URL_TEMPLATE % bucket, name) | |
| 43 | |
| 44 | |
| 45 def unique_name(basename, timestamp=True, device=None): | |
| 46 """Helper function for creating a unique name for a logdog stream. | |
| 47 | |
| 48 Args: | |
| 49 basename: Base of the unique name. | |
| 50 timestamp: Whether or not to add a timestamp to name. | |
| 51 device: Device to add device serial of to name. | |
| 52 """ | |
| 53 return '%s%s%s' % ( | |
| 54 basename, | |
| 55 '_%s' % time.strftime('%Y%m%dT%H%M%S', time.localtime()) | |
| 56 if timestamp else '', | |
| 57 '_%s' % device.serial if device else '') | |
| OLD | NEW |