summaryrefslogtreecommitdiffstats
path: root/tools/deep_memory_profiler/visualizer/template.py
blob: 0c9c5f53df4ca45ae9f6271c83adbd65234be3db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import subprocess
import sys
import tempfile

from string import Template


_TEMPLATE = """<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="../visualizer/static/index.css">
<link rel="stylesheet"
  href="../visualizer/static/third_party/jqTree/jqtree.css">

<script src="../../../third_party/flot/jquery.min.js"></script>
<script src="../../../third_party/flot/jquery.flot.min.js"></script>
<script src="../../../third_party/flot/jquery.flot.stack.min.js"></script>
<script src="../visualizer/static/third_party/jqTree/tree.jquery.js"></script>
<script src="../visualizer/static/utility.js"></script>
<script src="../visualizer/static/profiler.js"></script>
<script src="../visualizer/static/graph-view.js"></script>
<script src="../visualizer/static/dropdown-view.js"></script>
<script src="../visualizer/static/menu-view.js"></script>
<script type="text/javascript">
  $(function() {
  var data = $DATA;
  var profiler = new Profiler(data);
  var graphView = new GraphView(profiler);
  var dropdownView = new DropdownView(profiler);
  var menuView = new MenuView(profiler);

  profiler.reparse();
});
</script>

<body>
  <h2>Deep Memory Profiler Visualizer</h2>
  <div id="graph-div"></div>
  <div id="info-div">
    <div id="category-menu"></div>
    <div id="subs-dropdown"></div>
  </div>
</body>
"""


def main(argv):
  # Read json data.
  with open(argv[1]) as data_file:
    data = data_file.read()

  # Fill in the template of index.js.
  dmprof_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  html_dir = os.path.join(dmprof_path, 'graphs')
  if not os.path.exists(html_dir):
    os.mkdir(html_dir)

  html_handle, html_path = tempfile.mkstemp('.html', 'graph', html_dir)
  html_file = os.fdopen(html_handle, 'w')
  html_file.write(Template(_TEMPLATE).safe_substitute({ 'DATA': data }))
  html_file.close()

  # Open index page in chrome automatically if permitted.
  if sys.platform.startswith('linux'):
    try:
      subprocess.call(['xdg-open', html_path])
    except OSError, exception:
      print >> sys.stderr, 'xdg-open failed:', exception
      print 'generated html file is at ' + html_path
  else:
    print 'generated html file is at ' + html_path


if __name__ == '__main__':
  sys.exit(main(sys.argv))