Extract prefix and suffix html blocks to separate files DO NOT MERGE

The Monitor tool in SDK has a duplicate copy of the prefix and suffix
html blocks, which necessitates changes in both places whenever the
prefix block changes. Moving the html out of the script allows
Monitor to reuse the same content.

Change-Id: I012df9ef3423b546a2ced213bee1ccbba9d4c9cc
diff --git a/prefix.html b/prefix.html
new file mode 100644
index 0000000..e8ceca1
--- /dev/null
+++ b/prefix.html
@@ -0,0 +1,37 @@
+<!DOCTYPE HTML>
+<html>
+<head i18n-values="dir:textdirection;">
+<meta charset="utf-8"/>
+<title>Android System Trace</title>
+%s
+%s
+<script language="javascript">
+document.addEventListener('DOMContentLoaded', function() {
+  if (!linuxPerfData)
+    return;
+
+  var m = new tracing.Model(linuxPerfData);
+  var timelineViewEl = document.querySelector('.view');
+  tracing.ui.decorate(timelineViewEl, tracing.TimelineView);
+  timelineViewEl.model = m;
+  timelineViewEl.tabIndex = 1;
+  timelineViewEl.timeline.focusElement = timelineViewEl;
+});
+</script>
+<style>
+  .view {
+    overflow: hidden;
+    position: absolute;
+    top: 0;
+    bottom: 0;
+    left: 0;
+    right: 0;
+  }
+</style>
+</head>
+<body>
+  <div class="view">
+  </div>
+<!-- BEGIN TRACE -->
+  <script>
+  var linuxPerfData = "\
diff --git a/suffix.html b/suffix.html
new file mode 100644
index 0000000..f770848
--- /dev/null
+++ b/suffix.html
@@ -0,0 +1,5 @@
+\n";
+  </script>
+<!-- END TRACE -->
+</body>
+</html>
diff --git a/systrace-legacy.py b/systrace-legacy.py
index 98eeb87..b969aac 100755
--- a/systrace-legacy.py
+++ b/systrace-legacy.py
@@ -174,6 +174,7 @@
             sys.stdout.write("downloading trace...")
             sys.stdout.flush()
             out = ''.join(lines[i+1:])
+            html_prefix = read_asset(script_dir, 'prefix.html')
             html_file = open(html_filename, 'w')
             html_file.write(html_prefix % (css, js))
             trace_started = True
@@ -197,6 +198,7 @@
     html_out = dec.flush().replace('\n', '\\n\\\n').replace('\r', '')
     if len(html_out) > 0:
       html_file.write(html_out)
+    html_suffix = read_asset(script_dir, 'suffix.html')
     html_file.write(html_suffix)
     html_file.close()
     print " done\n\n    wrote file://%s/%s\n" % (os.getcwd(), options.output_file)
@@ -204,6 +206,9 @@
     print >> sys.stderr, ('An error occured while capturing the trace.  Output ' +
       'file was not written.')
 
+def read_asset(src_dir, filename):
+  return open(os.path.join(src_dir, filename)).read()
+
 def get_assets(src_dir, build_dir):
   sys.path.append(build_dir)
   gen = __import__('generate_standalone_timeline_view', {}, {})
@@ -225,51 +230,6 @@
 
   return (js_files, js_flattenizer, css_files)
 
-html_prefix = """<!DOCTYPE HTML>
-<html>
-<head i18n-values="dir:textdirection;">
-<title>Android System Trace</title>
-%s
-%s
-<script language="javascript">
-document.addEventListener('DOMContentLoaded', function() {
-  if (!linuxPerfData)
-    return;
-
-  var m = new tracing.Model(linuxPerfData);
-  var timelineViewEl = document.querySelector('.view');
-  tracing.ui.decorate(timelineViewEl, tracing.TimelineView);
-  timelineViewEl.model = m;
-  timelineViewEl.tabIndex = 1;
-  timelineViewEl.timeline.focusElement = timelineViewEl;
-});
-</script>
-<style>
-  .view {
-    overflow: hidden;
-    position: absolute;
-    top: 0;
-    bottom: 0;
-    left: 0;
-    right: 0;
-  }
-</style>
-</head>
-<body>
-  <div class="view">
-  </div>
-<!-- BEGIN TRACE -->
-  <script>
-  var linuxPerfData = "\\
-"""
-
-html_suffix = """\\n";
-  </script>
-<!-- END TRACE -->
-</body>
-</html>
-"""
-
 compiled_css_tag = """<style type="text/css">%s</style>"""
 compiled_js_tag = """<script language="javascript">%s</script>"""
 
diff --git a/systrace.py b/systrace.py
index baafc6f..492f53a 100755
--- a/systrace.py
+++ b/systrace.py
@@ -217,6 +217,9 @@
         # Indicate to the user that the data download is complete.
         print " done\n"
 
+      html_prefix = read_asset(script_dir, 'prefix.html')
+      html_suffix = read_asset(script_dir, 'suffix.html')
+
       html_file = open(html_filename, 'w')
       html_file.write(html_prefix % (css, js))
 
@@ -237,6 +240,9 @@
     print >> sys.stderr, 'adb returned error code %d' % result
     sys.exit(1)
 
+def read_asset(src_dir, filename):
+  return open(os.path.join(src_dir, filename)).read()
+
 def get_assets(src_dir, build_dir):
   sys.path.append(build_dir)
   gen = __import__('generate_standalone_timeline_view', {}, {})
@@ -258,52 +264,6 @@
 
   return (js_files, js_flattenizer, css_files)
 
-html_prefix = """<!DOCTYPE HTML>
-<html>
-<head i18n-values="dir:textdirection;">
-<meta charset="utf-8"/>
-<title>Android System Trace</title>
-%s
-%s
-<script language="javascript">
-document.addEventListener('DOMContentLoaded', function() {
-  if (!linuxPerfData)
-    return;
-
-  var m = new tracing.Model(linuxPerfData);
-  var timelineViewEl = document.querySelector('.view');
-  tracing.ui.decorate(timelineViewEl, tracing.TimelineView);
-  timelineViewEl.model = m;
-  timelineViewEl.tabIndex = 1;
-  timelineViewEl.timeline.focusElement = timelineViewEl;
-});
-</script>
-<style>
-  .view {
-    overflow: hidden;
-    position: absolute;
-    top: 0;
-    bottom: 0;
-    left: 0;
-    right: 0;
-  }
-</style>
-</head>
-<body>
-  <div class="view">
-  </div>
-<!-- BEGIN TRACE -->
-  <script>
-  var linuxPerfData = "\\
-"""
-
-html_suffix = """\\n";
-  </script>
-<!-- END TRACE -->
-</body>
-</html>
-"""
-
 compiled_css_tag = """<style type="text/css">%s</style>"""
 compiled_js_tag = """<script language="javascript">%s</script>"""