am 1f5d4e97: systrace: handle user builds more gracefully

* commit '1f5d4e97822266b682a8a599b532b3b6460f9e3f':
  systrace: handle user builds more gracefully
diff --git a/systrace.py b/systrace.py
index af82c6f..3e71bbe 100755
--- a/systrace.py
+++ b/systrace.py
@@ -35,7 +35,7 @@
   parser.add_option('-b', '--buf-size', dest='trace_buf_size', type='int',
                     help='use a trace buffer size of N KB', metavar='N')
   parser.add_option('-d', '--disk', dest='trace_disk', default=False,
-                    action='store_true', help='trace disk I/O')
+                    action='store_true', help='trace disk I/O (requires root)')
   parser.add_option('-f', '--cpu-freq', dest='trace_cpu_freq', default=False,
                     action='store_true', help='trace CPU frequency changes')
   parser.add_option('-i', '--cpu-idle', dest='trace_cpu_idle', default=False,
@@ -47,7 +47,8 @@
                     'scheduler (allows longer trace times by reducing data ' +
                     'rate into buffer)')
   parser.add_option('-w', '--workqueue', dest='trace_workqueue', default=False,
-                    action='store_true', help='trace the kernel workqueues')
+                    action='store_true', help='trace the kernel workqueues ' +
+                    '(requires root)')
   parser.add_option('--set-tags', dest='set_tags', action='store',
                     help='set the enabled trace tags and exit; set to a ' +
                     'comma separated list of: ' +
@@ -70,7 +71,7 @@
     try:
       subprocess.check_call(atrace_args)
     except subprocess.CalledProcessError, e:
-      print sys.stderr, 'unable to set tags: %s' % e
+      print >> sys.stderr, 'unable to set tags: %s' % e
     print '\nSet enabled tags to: %s\n' % ', '.join(tags)
     print ('You will likely need to restart the Android framework for this to ' +
           'take effect:\n\n    adb shell stop\n    adb shell ' +
@@ -113,8 +114,6 @@
     js = compiled_js_tag % (open(js_filename).read())
 
   html_filename = options.output_file
-  html_file = open(html_filename, 'w')
-  html_file.write(html_prefix % (css, js))
 
   trace_started = False
   leftovers = ''
@@ -143,6 +142,8 @@
             sys.stdout.write("downloading trace...")
             sys.stdout.flush()
             out = ''.join(lines[i+1:])
+            html_file = open(html_filename, 'w')
+            html_file.write(html_prefix % (css, js))
             trace_started = True
             break
           elif 'TRACE:'.startswith(line) and i == len(lines) - 1:
@@ -159,14 +160,17 @@
     if result is not None:
       break
   if result != 0:
-    print sys.stderr, 'adb returned error code %d' % result
-  else:
+    print >> sys.stderr, 'adb returned error code %d' % result
+  elif trace_started:
     html_out = dec.flush().replace('\n', '\\n\\\n').replace('\r', '')
     if len(html_out) > 0:
       html_file.write(html_out)
     html_file.write(html_suffix)
     html_file.close()
     print " done\n\n    wrote file://%s/%s\n" % (os.getcwd(), options.output_file)
+  else:
+    print >> sys.stderr, ('An error occured while capturing the trace.  Output ' +
+      'file was not written.')
 
 html_prefix = """<!DOCTYPE HTML>
 <html>