| 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 gzip | |
| 6 import os | 5 import os |
| 7 import shutil | |
| 8 import sys | |
| 9 import zipfile | |
| 10 | 6 |
| 7 from chrome_profiler import trace_packager |
| 11 from chrome_profiler import ui | 8 from chrome_profiler import ui |
| 12 from chrome_profiler import util | |
| 13 | 9 |
| 14 from pylib import constants | 10 from pylib import constants |
| 15 | 11 |
| 16 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, | |
| 17 'third_party', | |
| 18 'trace-viewer')) | |
| 19 # pylint: disable=F0401 | |
| 20 from trace_viewer.build import trace2html | |
| 21 | |
| 22 | |
| 23 def _CompressFile(host_file, output): | |
| 24 with gzip.open(output, 'wb') as out: | |
| 25 with open(host_file, 'rb') as input_file: | |
| 26 out.write(input_file.read()) | |
| 27 os.unlink(host_file) | |
| 28 | |
| 29 | |
| 30 def _ArchiveFiles(host_files, output): | |
| 31 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as z: | |
| 32 for host_file in host_files: | |
| 33 z.write(host_file) | |
| 34 os.unlink(host_file) | |
| 35 | |
| 36 | |
| 37 def _PackageTracesAsHtml(trace_files, html_file): | |
| 38 with open(html_file, 'w') as f: | |
| 39 trace2html.WriteHTMLForTracesToFile(trace_files, f) | |
| 40 for trace_file in trace_files: | |
| 41 os.unlink(trace_file) | |
| 42 | |
| 43 | 12 |
| 44 def _StartTracing(controllers, interval): | 13 def _StartTracing(controllers, interval): |
| 45 for controller in controllers: | 14 for controller in controllers: |
| 46 controller.StartTracing(interval) | 15 controller.StartTracing(interval) |
| 47 | 16 |
| 48 | 17 |
| 49 def _StopTracing(controllers): | 18 def _StopTracing(controllers): |
| 50 for controller in controllers: | 19 for controller in controllers: |
| 51 controller.StopTracing() | 20 controller.StopTracing() |
| 52 | 21 |
| 53 | 22 |
| 54 def _PullTraces(controllers, output, compress, write_json): | 23 def _PullTraces(controllers, output, compress, write_json): |
| 55 ui.PrintMessage('Downloading...', eol='') | 24 ui.PrintMessage('Downloading...', eol='') |
| 56 trace_files = [] | 25 trace_files = [controller.PullTrace() for controller in controllers] |
| 57 for controller in controllers: | 26 result = trace_packager.PackageTraces(trace_files, |
| 58 trace_files.append(controller.PullTrace()) | 27 output=output, |
| 59 | 28 compress=compress, |
| 60 if not write_json: | 29 write_json=write_json) |
| 61 html_file = os.path.splitext(trace_files[0])[0] + '.html' | |
| 62 _PackageTracesAsHtml(trace_files, html_file) | |
| 63 trace_files = [html_file] | |
| 64 | |
| 65 if compress and len(trace_files) == 1: | |
| 66 result = output or trace_files[0] + '.gz' | |
| 67 _CompressFile(trace_files[0], result) | |
| 68 elif len(trace_files) > 1: | |
| 69 result = output or 'chrome-combined-trace-%s.zip' % util.GetTraceTimestamp() | |
| 70 _ArchiveFiles(trace_files, result) | |
| 71 elif output: | |
| 72 result = output | |
| 73 shutil.move(trace_files[0], result) | |
| 74 else: | |
| 75 result = trace_files[0] | |
| 76 | |
| 77 ui.PrintMessage('done') | 30 ui.PrintMessage('done') |
| 78 ui.PrintMessage('Trace written to file://%s' % os.path.abspath(result)) | 31 ui.PrintMessage('Trace written to file://%s' % os.path.abspath(result)) |
| 79 return result | 32 return result |
| 80 | 33 |
| 81 | 34 |
| 82 def GetSupportedBrowsers(): | 35 def GetSupportedBrowsers(): |
| 83 """Returns the package names of all supported browsers.""" | 36 """Returns the package names of all supported browsers.""" |
| 84 # Add aliases for backwards compatibility. | 37 # Add aliases for backwards compatibility. |
| 85 supported_browsers = { | 38 supported_browsers = { |
| 86 'stable': constants.PACKAGE_INFO['chrome_stable'], | 39 'stable': constants.PACKAGE_INFO['chrome_stable'], |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 else: | 74 else: |
| 122 ui.PrintMessage('Capturing %s. Press Enter to stop...' % \ | 75 ui.PrintMessage('Capturing %s. Press Enter to stop...' % \ |
| 123 trace_type, eol='') | 76 trace_type, eol='') |
| 124 raw_input() | 77 raw_input() |
| 125 finally: | 78 finally: |
| 126 _StopTracing(controllers) | 79 _StopTracing(controllers) |
| 127 if interval: | 80 if interval: |
| 128 ui.PrintMessage('done') | 81 ui.PrintMessage('done') |
| 129 | 82 |
| 130 return _PullTraces(controllers, output, compress, write_json) | 83 return _PullTraces(controllers, output, compress, write_json) |
| OLD | NEW |