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 import hashlib |
| 6 import os |
| 7 |
| 8 from pylib.base import output_manager |
| 9 from pylib.output import noop_output_manager |
| 10 from pylib.utils import logdog_helper |
| 11 from pylib.utils import google_storage_helper |
| 12 |
| 13 |
| 14 class RemoteOutputManager(output_manager.OutputManager): |
| 15 |
| 16 def __init__(self, bucket): |
| 17 """Uploads output files to Google Storage or LogDog. |
| 18 |
| 19 Files will either be uploaded directly to Google Storage or LogDog |
| 20 depending on the datatype. |
| 21 |
| 22 Args |
| 23 bucket: Bucket to use when saving to Google Storage. |
| 24 """ |
| 25 super(RemoteOutputManager, self).__init__() |
| 26 self._bucket = bucket |
| 27 |
| 28 #override |
| 29 def _CreateArchivedFile(self, out_filename, out_subdir, datatype): |
| 30 if datatype == output_manager.Datatype.TEXT: |
| 31 return LogdogArchivedFile(out_filename, out_subdir, datatype) |
| 32 else: |
| 33 if self._bucket is None: |
| 34 return noop_output_manager.NoopArchivedFile() |
| 35 return GoogleStorageArchivedFile( |
| 36 out_filename, out_subdir, datatype, self._bucket) |
| 37 |
| 38 |
| 39 class LogdogArchivedFile(output_manager.ArchivedFile): |
| 40 |
| 41 def __init__(self, out_filename, out_subdir, datatype): |
| 42 super(LogdogArchivedFile, self).__init__(out_filename, out_subdir, datatype) |
| 43 self._stream_name = '%s_%s' % (out_subdir, out_filename) |
| 44 |
| 45 def _Link(self): |
| 46 return logdog_helper.get_viewer_url(self._stream_name) |
| 47 |
| 48 def _Archive(self): |
| 49 with open(self.name, 'r') as f: |
| 50 logdog_helper.text(self._stream_name, f.read()) |
| 51 |
| 52 |
| 53 class GoogleStorageArchivedFile(output_manager.ArchivedFile): |
| 54 |
| 55 def __init__(self, out_filename, out_subdir, datatype, bucket): |
| 56 super(GoogleStorageArchivedFile, self).__init__( |
| 57 out_filename, out_subdir, datatype) |
| 58 self._bucket = bucket |
| 59 self._upload_path = None |
| 60 self._content_addressed = None |
| 61 |
| 62 def _PrepareArchive(self): |
| 63 self._content_addressed = (self._datatype in ( |
| 64 output_manager.Datatype.HTML, |
| 65 output_manager.Datatype.IMAGE)) |
| 66 if self._content_addressed: |
| 67 sha1 = hashlib.sha1() |
| 68 with open(self.name, 'rb') as f: |
| 69 sha1.update(f.read()) |
| 70 self._upload_path = sha1.hexdigest() |
| 71 else: |
| 72 self._upload_path = os.path.join(self._out_subdir, self._out_filename) |
| 73 |
| 74 def _Link(self): |
| 75 return google_storage_helper.get_url_link( |
| 76 self._upload_path, self._bucket) |
| 77 |
| 78 def _Archive(self): |
| 79 if (self._content_addressed and |
| 80 google_storage_helper.exists(self._upload_path, self._bucket)): |
| 81 return |
| 82 |
| 83 content_type = None |
| 84 if self._datatype == output_manager.Datatype.HTML: |
| 85 content_type = 'text/html' |
| 86 google_storage_helper.upload( |
| 87 self._upload_path, self.name, self._bucket, content_type) |
OLD | NEW |