xref: /aosp_15_r20/external/angle/build/android/pylib/output/remote_output_manager.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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
5import hashlib
6import os
7
8from pylib.base import output_manager
9from pylib.output import noop_output_manager
10from pylib.utils import logdog_helper
11from pylib.utils import google_storage_helper
12
13
14class 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().__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      try:
32        logdog_helper.get_logdog_client()
33        return LogdogArchivedFile(out_filename, out_subdir, datatype)
34      except RuntimeError:
35        return noop_output_manager.NoopArchivedFile()
36    else:
37      if self._bucket is None:
38        return noop_output_manager.NoopArchivedFile()
39      return GoogleStorageArchivedFile(
40          out_filename, out_subdir, datatype, self._bucket)
41
42
43class LogdogArchivedFile(output_manager.ArchivedFile):
44
45  def __init__(self, out_filename, out_subdir, datatype):
46    super().__init__(out_filename, out_subdir, datatype)
47    self._stream_name = '%s_%s' % (out_subdir, out_filename)
48
49  def _Link(self):
50    return logdog_helper.get_viewer_url(self._stream_name)
51
52  def _Archive(self):
53    with open(self.name, 'r') as f:
54      logdog_helper.text(self._stream_name, f.read())
55
56
57class GoogleStorageArchivedFile(output_manager.ArchivedFile):
58
59  def __init__(self, out_filename, out_subdir, datatype, bucket):
60    super().__init__(out_filename, out_subdir, datatype)
61    self._bucket = bucket
62    self._upload_path = None
63    self._content_addressed = None
64
65  def _PrepareArchive(self):
66    self._content_addressed = (self._datatype in (
67        output_manager.Datatype.HTML,
68        output_manager.Datatype.PNG,
69        output_manager.Datatype.JSON))
70    if self._content_addressed:
71      sha1 = hashlib.sha1()
72      with open(self.name, 'rb') as f:
73        sha1.update(f.read())
74      self._upload_path = sha1.hexdigest()
75    else:
76      self._upload_path = os.path.join(self._out_subdir, self._out_filename)
77
78  def _Link(self):
79    return google_storage_helper.get_url_link(
80        self._upload_path, self._bucket)
81
82  def _Archive(self):
83    if (self._content_addressed and
84        google_storage_helper.exists(self._upload_path, self._bucket)):
85      return
86
87    google_storage_helper.upload(
88        self._upload_path, self.name, self._bucket, content_type=self._datatype)
89