systrace: add args to update.py

This change adds command line options to the update.py script to allow the svn
checkout and the minifcation steps to be skipped.

Change-Id: Id0ddce2abc6e4d1d0158c29c0ec3a47cb140e6a1
diff --git a/update.py b/update.py
index ca911b7..92d0352 100755
--- a/update.py
+++ b/update.py
@@ -1,33 +1,43 @@
 #!/usr/bin/python2.6
 
-import httplib, json, os, urllib, shutil, subprocess, sys
+import httplib, json, optparse, os, urllib, shutil, subprocess, sys
 
-minified_css_file = 'style.css'
-minified_js_file = 'script.js'
+output_css_file = 'style.css'
+output_js_file = 'script.js'
 
 upstream_svn = 'http://trace-viewer.googlecode.com/svn/trunk/'
 
 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
 trace_viewer_dir = os.path.join(script_dir, 'trace-viewer')
 
-# Remove the old source
-shutil.rmtree(trace_viewer_dir, True)
+parser = optparse.OptionParser()
+parser.add_option('--local', dest='local_dir', metavar='DIR',
+                  help='use a local trace-viewer')
+parser.add_option('--no-min', dest='no_min', default=False, action='store_true',
+                  help='skip minification')
+options, args = parser.parse_args()
 
-# 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.'
-  sys.exit(1)
+if options.local_dir is None:
+  # Remove the old source
+  shutil.rmtree(trace_viewer_dir, 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')
+  # 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.'
+    sys.exit(1)
+
+  # 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')
+else:
+  trace_viewer_dir = options.local_dir
 
 # Generate the flattened JS and CSS
 build_dir = os.path.join(trace_viewer_dir, 'build')
@@ -36,51 +46,57 @@
 js_code = gen.generate_js()
 css_code = gen.generate_css()
 
-# 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'),
-])
+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
+  # 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)
+  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.' % minified_js_file
-  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(minified_js_file, 'wt').write(result['compiledCode'] + '\n')
-print 'Generated %s' % minified_js_file
+  open(output_js_file, 'wt').write(result['compiledCode'] + '\n')
+  print 'Generated %s' % output_js_file
 
-yuic_args = ['yui-compressor', '--type', 'css', '-o', minified_css_file]
-p = subprocess.Popen(yuic_args, stdin=subprocess.PIPE)
-p.communicate(input=css_code)
-if p.wait() != 0:
-  print 'Failed to generate %s.' % minified_css_file
-  sys.exit(1)
+  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' % minified_css_file
+  print 'Generated %s' % output_css_file