1 #!/usr/bin/env python 2 # Copyright 2013 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 import os 7 import subprocess 8 import sys 9 import tempfile 10 11 from string import Template 12 13 14 _TEMPLATE = """<!DOCTYPE html> 15 <meta charset="utf-8"> 16 <link rel="stylesheet" href="../visualizer/static/index.css"> 17 <link rel="stylesheet" 18 href="../visualizer/static/third_party/jqTree/jqtree.css"> 19 20 <script src="../../../third_party/flot/jquery.min.js"></script> 21 <script src="../../../third_party/flot/jquery.flot.min.js"></script> 22 <script src="../../../third_party/flot/jquery.flot.stack.min.js"></script> 23 <script src="../visualizer/static/third_party/jqTree/tree.jquery.js"></script> 24 <script src="../visualizer/static/utility.js"></script> 25 <script src="../visualizer/static/profiler.js"></script> 26 <script src="../visualizer/static/graph-view.js"></script> 27 <script src="../visualizer/static/dropdown-view.js"></script> 28 <script src="../visualizer/static/menu-view.js"></script> 29 <script type="text/javascript"> 30 $(function() { 31 var data = $DATA; 32 var profiler = new Profiler(data); 33 var graphView = new GraphView(profiler); 34 var dropdownView = new DropdownView(profiler); 35 var menuView = new MenuView(profiler); 36 37 profiler.reparse(); 38 }); 39 </script> 40 41 <body> 42 <h2>Deep Memory Profiler Visualizer</h2> 43 <div id="graph-div"></div> 44 <div id="info-div"> 45 <div id="category-menu"></div> 46 <div id="subs-dropdown"></div> 47 </div> 48 </body> 49 """ 50 51 52 def main(argv): 53 # Read json data. 54 with open(argv[1]) as data_file: 55 data = data_file.read() 56 57 # Fill in the template of index.js. 58 dmprof_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 59 html_dir = os.path.join(dmprof_path, 'graphs') 60 if not os.path.exists(html_dir): 61 os.mkdir(html_dir) 62 63 html_handle, html_path = tempfile.mkstemp('.html', 'graph', html_dir) 64 html_file = os.fdopen(html_handle, 'w') 65 html_file.write(Template(_TEMPLATE).safe_substitute({ 'DATA': data })) 66 html_file.close() 67 68 # Open index page in chrome automatically if permitted. 69 if sys.platform.startswith('linux'): 70 try: 71 subprocess.call(['xdg-open', html_path]) 72 except OSError, exception: 73 print >> sys.stderr, 'xdg-open failed:', exception 74 print 'generated html file is at ' + html_path 75 else: 76 print 'generated html file is at ' + html_path 77 78 79 if __name__ == '__main__': 80 sys.exit(main(sys.argv)) 81