display_in_browser.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright 2021 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. """Displays a trace file in a browser.
  5. """
  6. import logging
  7. import os
  8. import subprocess
  9. import sys
  10. import webbrowser
  11. import flag_utils
  12. def DisplayInBrowser(trace_file, trace_format='proto'):
  13. """Displays trace in browser.
  14. Args:
  15. trace_file: Saved trace filename.
  16. trace_format: Storage format of trace file.
  17. """
  18. if trace_format == 'json':
  19. raise Exception('The --view option and --trace_format=json are not'
  20. 'supported together')
  21. if trace_format == 'proto':
  22. flag_utils.GetTracingLogger().info('Opening trace in browser')
  23. open_trace_ui_path = os.path.join(
  24. os.path.dirname(__file__), os.pardir, os.pardir,
  25. 'third_party/perfetto/tools/open_trace_in_ui')
  26. trace_file_path = os.path.abspath(trace_file)
  27. cmd = [open_trace_ui_path, '-i', trace_file_path]
  28. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  29. stderr = p.communicate()[1]
  30. if p.returncode != 0:
  31. raise RuntimeError('failed: ' + stderr)
  32. elif sys.platform == 'darwin':
  33. os.system('/usr/bin/open %s' % os.path.abspath(trace_file))
  34. else:
  35. webbrowser.open(trace_file)