1# Copyright 2017 The Chromium Authors 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 logdog.""" 6 7import logging 8import os 9import sys 10 11from pylib import constants 12from pylib.utils import decorators 13 14sys.path.insert( 15 0, 16 os.path.abspath( 17 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'logdog'))) 18from logdog import bootstrap # pylint: disable=import-error 19 20 21@decorators.NoRaiseException(default_return_value='', 22 exception_message=('Ignore this exception. ' 23 'crbug.com/675666')) 24def text(name, data, content_type=None): 25 """Uploads text to logdog. 26 27 Args: 28 name: Name of the logdog stream. 29 data: String with data you want to upload. 30 content_type: The optional content type of the stream. If None, a 31 default content type will be chosen. 32 33 Returns: 34 Link to view uploaded text in logdog viewer. 35 """ 36 logging.info('Writing text to logdog stream, %s', name) 37 with get_logdog_client().text(name, content_type=content_type) as stream: 38 stream.write(data) 39 return stream.get_viewer_url() 40 41 42@decorators.NoRaiseException(default_return_value=None, 43 exception_message=('Ignore this exception. ' 44 'crbug.com/675666')) 45def open_text(name): 46 """Returns a file like object which you can write to. 47 48 Args: 49 name: Name of the logdog stream. 50 51 Returns: 52 A file like object. close() file when done. 53 """ 54 logging.info('Opening text logdog stream, %s', name) 55 return get_logdog_client().open_text(name) 56 57 58@decorators.NoRaiseException(default_return_value='', 59 exception_message=('Ignore this exception. ' 60 'crbug.com/675666')) 61def binary(name, binary_path): 62 """Uploads binary to logdog. 63 64 Args: 65 name: Name of the logdog stream. 66 binary_path: Path to binary you want to upload. 67 68 Returns: 69 Link to view uploaded binary in logdog viewer. 70 """ 71 logging.info('Writing binary to logdog stream, %s', name) 72 with get_logdog_client().binary(name) as stream: 73 with open(binary_path, 'r') as f: 74 stream.write(f.read()) 75 return stream.get_viewer_url() 76 77 78@decorators.NoRaiseException(default_return_value='', 79 exception_message=('Ignore this exception. ' 80 'crbug.com/675666')) 81def get_viewer_url(name): 82 """Get Logdog viewer URL. 83 84 Args: 85 name: Name of the logdog stream. 86 87 Returns: 88 Link to view uploaded binary in logdog viewer. 89 """ 90 return get_logdog_client().get_viewer_url(name) 91 92 93@decorators.Memoize 94def get_logdog_client(): 95 logging.info('Getting logdog client.') 96 return bootstrap.ButlerBootstrap.probe().stream_client() 97