Update to latest trace viewer

Upstream trace viewer has changed substantially since last pull.

First, the old flattening into js + css + html workflow has been replaced with
a new flatten into single html file workflow.

Second, trace viewer has moved to git.

Some pieces that were previously only in systrace are now upstream as well.
In particular, minification is now upstream. Thus, the minifying features in
systrace can be removed.

Change-Id: Ibc6a46fa3dccff8b771a95aae1909cf178157264
diff --git a/update.py b/update.py
index 92d0352..95d0bed 100755
--- a/update.py
+++ b/update.py
@@ -1,11 +1,10 @@
-#!/usr/bin/python2.6
+#!/usr/bin/env python
 
-import httplib, json, optparse, os, urllib, shutil, subprocess, sys
+import codecs, httplib, json, optparse, os, urllib, shutil, subprocess, sys
 
-output_css_file = 'style.css'
-output_js_file = 'script.js'
+output_html_file = 'systrace_trace_viewer.html'
 
-upstream_svn = 'http://trace-viewer.googlecode.com/svn/trunk/'
+upstream_git = 'https://github.com/google/trace-viewer.git'
 
 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
 trace_viewer_dir = os.path.join(script_dir, 'trace-viewer')
@@ -17,86 +16,45 @@
                   help='skip minification')
 options, args = parser.parse_args()
 
+# Update the source if needed.
 if options.local_dir is None:
-  # Remove the old source
+  # Remove the old source tree.
   shutil.rmtree(trace_viewer_dir, True)
 
-  # Pull the latest source from the upstream svn
-  svn_co_args = ['svn', 'co', upstream_svn, trace_viewer_dir]
-  p = subprocess.Popen(svn_co_args, stdout=subprocess.PIPE)
-  svn_output = ''
-  while p.poll() is None:
-    svn_output += p.stdout.read()
-  if p.returncode != 0:
-    print 'Failed to checkout source from upstream svn.'
+  # Pull the latest source from the upstream svn.
+  git_args = ['git', 'clone', upstream_git, trace_viewer_dir]
+  p = subprocess.Popen(git_args, stdout=subprocess.PIPE, cwd=script_dir)
+  p.communicate()
+  if p.wait() != 0:
+    print 'Failed to checkout source from upstream git.'
     sys.exit(1)
+  
+  shutil.rmtree(os.path.join(trace_viewer_dir, '.git'), True)
 
   # Update the UPSTREAM_REVISION file
-  rev_str = svn_output.split('\n')[-2]
-  if not rev_str.startswith('Checked out revision '):
-    print 'Unrecognized revision string: %q' % rev_str
-  open('UPSTREAM_REVISION', 'wt').write(rev_str[21:-1] + '\n')
+  git_args = ['git', 'rev-parse', 'HEAD']
+  p = subprocess.Popen(git_args, stdout=subprocess.PIPE, cwd=trace_viewer_dir)
+  out, err = p.communicate()
+  if p.wait() != 0:
+    print 'Failed to get revision.'
+    sys.exit(1)
+
+  rev = out.strip()
+  with open('UPSTREAM_REVISION', 'wt') as f:
+    f.write(rev + '\n')
 else:
   trace_viewer_dir = options.local_dir
 
-# Generate the flattened JS and CSS
-build_dir = os.path.join(trace_viewer_dir, 'build')
+
+# Generate the vulcanized result.
+build_dir = os.path.join(trace_viewer_dir)
 sys.path.append(build_dir)
-gen = __import__('generate_standalone_timeline_view', {}, {})
-js_code = gen.generate_js()
-css_code = gen.generate_css()
 
-if options.no_min:
-  open(output_js_file, 'wt').write(js_code)
-  print 'Generated %s' % output_js_file
-  open(output_css_file, 'wt').write(css_code)
-  print 'Generated %s' % output_css_file
-else:
-  # Define the parameters for the POST request and encode them in
-  # a URL-safe format.
-  params = urllib.urlencode([
-    ('js_code', js_code),
-    ('language', 'ECMASCRIPT5'),
-    ('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
-    ('output_format', 'json'),
-    ('output_info', 'errors'),
-    ('output_info', 'compiled_code'),
-  ])
-
-  # Always use the following value for the Content-type header.
-  headers = { "Content-type": "application/x-www-form-urlencoded" }
-  conn = httplib.HTTPConnection('closure-compiler.appspot.com')
-  conn.request('POST', '/compile', params, headers)
-  response = conn.getresponse()
-  data = response.read()
-  conn.close
-
-  if response.status != 200:
-    print sys.stderr, "error returned from JS compile service: %d" % response.status
-    sys.exit(1)
-
-  result = json.loads(data)
-  if 'errors' in result:
-    print 'Encountered error minifying Javascript.  Writing intermediate code to flat_script.js'
-    open('flat_script.js', 'wt').write(js_code)
-    for e in result['errors']:
-      filenum = int(e['file'][6:])
-      filename = 'flat_script.js'
-      lineno = e['lineno']
-      charno = e['charno']
-      err = e['error']
-      print '%s:%d:%d: %s' % (filename, lineno, charno, err)
-    print 'Failed to generate %s.' % output_js_file
-    sys.exit(1)
-
-  open(output_js_file, 'wt').write(result['compiledCode'] + '\n')
-  print 'Generated %s' % output_js_file
-
-  yuic_args = ['yui-compressor', '--type', 'css', '-o', output_css_file]
-  p = subprocess.Popen(yuic_args, stdin=subprocess.PIPE)
-  p.communicate(input=css_code)
-  if p.wait() != 0:
-    print 'Failed to generate %s.' % output_css_file
-    sys.exit(1)
-
-  print 'Generated %s' % output_css_file
+from trace_viewer.build import vulcanize_trace_viewer
+with codecs.open(output_html_file, encoding='utf-8', mode='w') as f:
+  vulcanize_trace_viewer.WriteTraceViewer(
+      f,
+      config_name='systrace',
+      minify=(not options.no_min),
+      output_html_head_and_body=False)
+print 'Generated %s' % output_html_file