| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 import datetime | |
| 6 import logging | 5 import logging |
| 7 import os | 6 import os |
| 8 import random | |
| 9 import shutil | 7 import shutil |
| 10 import sys | 8 import sys |
| 11 import tempfile | 9 import tempfile |
| 12 | 10 |
| 13 from py_utils import cloud_storage # pylint: disable=import-error | 11 from py_utils import cloud_storage # pylint: disable=import-error |
| 14 | 12 |
| 15 from telemetry.internal.util import file_handle | 13 from telemetry.internal.util import file_handle |
| 16 from telemetry.timeline import trace_data as trace_data_module | 14 from telemetry.timeline import trace_data as trace_data_module |
| 17 from telemetry import value as value_module | 15 from telemetry import value as value_module |
| 18 from tracing.trace_data import trace_data as trace_data_module | 16 from tracing.trace_data import trace_data as trace_data_module |
| 19 | 17 |
| 20 | 18 |
| 21 class TraceValue(value_module.Value): | 19 class TraceValue(value_module.Value): |
| 22 def __init__(self, page, trace_data, important=False, description=None): | 20 def __init__(self, page, trace_data, important=False, description=None, |
| 21 file_path=None, remote_path=None, upload_bucket=None, |
| 22 cloud_url=None): |
| 23 """A value that contains a TraceData object and knows how to | 23 """A value that contains a TraceData object and knows how to |
| 24 output it. | 24 output it. |
| 25 | 25 |
| 26 Adding TraceValues and outputting as JSON will produce a directory full of | 26 Adding TraceValues and outputting as JSON will produce a directory full of |
| 27 HTML files called trace_files. Outputting as chart JSON will also produce | 27 HTML files called trace_files. Outputting as chart JSON will also produce |
| 28 an index, files.html, linking to each of these files. | 28 an index, files.html, linking to each of these files. |
| 29 """ | 29 """ |
| 30 super(TraceValue, self).__init__( | 30 super(TraceValue, self).__init__( |
| 31 page, name='trace', units='', important=important, | 31 page, name='trace', units='', important=important, |
| 32 description=description, tir_label=None, grouping_keys=None) | 32 description=description, tir_label=None, grouping_keys=None) |
| 33 self._temp_file = self._GetTempFileHandle(trace_data) | 33 self._temp_file = self._GetTempFileHandle(trace_data) |
| 34 self._cloud_url = None | 34 self._file_path = file_path |
| 35 self._remote_path = remote_path |
| 36 self._upload_bucket = upload_bucket |
| 37 self._cloud_url = cloud_url |
| 35 self._serialized_file_handle = None | 38 self._serialized_file_handle = None |
| 36 | 39 |
| 37 @property | 40 @property |
| 38 def value(self): | 41 def value(self): |
| 39 if self._cloud_url: | 42 if self._cloud_url: |
| 40 return self._cloud_url | 43 return self._cloud_url |
| 41 elif self._serialized_file_handle: | 44 elif self._serialized_file_handle: |
| 42 return self._serialized_file_handle.GetAbsPath() | 45 return self._serialized_file_handle.GetAbsPath() |
| 43 | 46 |
| 44 def _GetTraceParts(self, trace_data): | 47 def _GetTraceParts(self, trace_data): |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 def AsDict(self): | 118 def AsDict(self): |
| 116 if self._temp_file is None: | 119 if self._temp_file is None: |
| 117 raise ValueError('Tried to serialize TraceValue without tempfile.') | 120 raise ValueError('Tried to serialize TraceValue without tempfile.') |
| 118 d = super(TraceValue, self).AsDict() | 121 d = super(TraceValue, self).AsDict() |
| 119 if self._serialized_file_handle: | 122 if self._serialized_file_handle: |
| 120 d['file_id'] = self._serialized_file_handle.id | 123 d['file_id'] = self._serialized_file_handle.id |
| 121 if self._cloud_url: | 124 if self._cloud_url: |
| 122 d['cloud_url'] = self._cloud_url | 125 d['cloud_url'] = self._cloud_url |
| 123 return d | 126 return d |
| 124 | 127 |
| 125 def Serialize(self, dir_path): | 128 def Serialize(self): |
| 126 if self._temp_file is None: | 129 if self._temp_file is None or self._file_path is None: |
| 127 raise ValueError('Tried to serialize nonexistent trace.') | 130 raise ValueError('Tried to serialize nonexistent trace.') |
| 128 if self.page: | 131 shutil.copy(self._temp_file.GetAbsPath(), self._file_path) |
| 129 file_name = self.page.file_safe_name | 132 self._serialized_file_handle = file_handle.FromFilePath(self._file_path) |
| 130 else: | |
| 131 file_name = '' | |
| 132 file_name += str(self._temp_file.id) | |
| 133 file_name += datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') | |
| 134 file_name += self._temp_file.extension | |
| 135 file_path = os.path.abspath(os.path.join(dir_path, file_name)) | |
| 136 shutil.copy(self._temp_file.GetAbsPath(), file_path) | |
| 137 self._serialized_file_handle = file_handle.FromFilePath(file_path) | |
| 138 return self._serialized_file_handle | 133 return self._serialized_file_handle |
| 139 | 134 |
| 140 def UploadToCloud(self, bucket): | 135 def UploadToCloud(self): |
| 141 if self._temp_file is None: | 136 if self._temp_file is None: |
| 142 raise ValueError('Tried to upload nonexistent trace to Cloud Storage.') | 137 raise ValueError('Tried to upload nonexistent trace to Cloud Storage.') |
| 143 try: | 138 try: |
| 144 if self._serialized_file_handle: | 139 if self._serialized_file_handle: |
| 145 fh = self._serialized_file_handle | 140 fh = self._serialized_file_handle |
| 146 else: | 141 else: |
| 147 fh = self._temp_file | 142 fh = self._temp_file |
| 148 remote_path = ('trace-file-id_%s-%s-%d%s' % ( | 143 cloud_storage.Insert( |
| 149 fh.id, | 144 self._upload_bucket, self._remote_path, fh.GetAbsPath()) |
| 150 datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'), | |
| 151 random.randint(1, 100000), | |
| 152 fh.extension)) | |
| 153 self._cloud_url = cloud_storage.Insert( | |
| 154 bucket, remote_path, fh.GetAbsPath()) | |
| 155 sys.stderr.write( | 145 sys.stderr.write( |
| 156 'View generated trace files online at %s for story %s\n' % | 146 'View generated trace files online at %s for story %s\n' % |
| 157 (self._cloud_url, self.page.name if self.page else 'unknown')) | 147 (self._cloud_url, self.page.name if self.page else 'unknown')) |
| 158 return self._cloud_url | 148 return self._cloud_url |
| 159 except cloud_storage.PermissionError as e: | 149 except cloud_storage.PermissionError as e: |
| 160 logging.error('Cannot upload trace files to cloud storage due to ' | 150 logging.error('Cannot upload trace files to cloud storage due to ' |
| 161 ' permission error: %s' % e.message) | 151 ' permission error: %s' % e.message) |
| OLD | NEW |