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