Improve error handling for barcode_tools

These changes make the execution abort earlier on an error (like a tool is not found) and makes it easier to figure out what's wrong.

Made build_zxing.py executable.

BUG=None
TEST=Local runs of the PyAuto test src/chrome/test/functional/webrtc_video_quality.py in Chromium.

Review URL: https://webrtc-codereview.appspot.com/840005

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2899 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/tools/barcode_tools/barcode_decoder.py b/tools/barcode_tools/barcode_decoder.py
index 9c71c16..87b3ec2 100755
--- a/tools/barcode_tools/barcode_decoder.py
+++ b/tools/barcode_tools/barcode_decoder.py
@@ -9,7 +9,6 @@
 
 import optparse
 import os
-import subprocess
 import sys
 
 import helper_functions
@@ -43,7 +42,8 @@
     helper_functions.run_shell_command(
         command, msg='Error during YUV to PNG conversion')
   except helper_functions.HelperError, err:
-    print err
+    print >> sys.stderr, 'Error executing command: %s. Error: %s' % (command,
+                                                                     err)
     return False
   return True
 
@@ -101,10 +101,10 @@
     out = helper_functions.run_shell_command(
         command, msg='Error during decoding of %s' % file_name)
     if not 'Success' in out:
-      sys.stderr.write('Barcode in %s cannot be decoded\n' % file_name)
+      print >> sys.stderr, 'Barcode in %s cannot be decoded\n' % file_name
       return False
   except helper_functions.HelperError, err:
-    print err
+    print >> sys.stderr, err
     return False
   return True
 
@@ -155,7 +155,6 @@
   barcode_file = open(barcode_file_name, 'r')
   barcode = barcode_file.read()
   barcode_file.close()
-
   return barcode
 
 
@@ -270,16 +269,24 @@
   zxing_dir = os.path.join(script_dir, 'third_party', 'zxing')
 
   # Convert the overlaid YUV video into a set of PNG frames.
-  convert_yuv_to_png_files(options.yuv_file, options.yuv_frame_width,
-                           options.yuv_frame_height,
-                           output_directory=options.png_output_dir)
+  if not convert_yuv_to_png_files(options.yuv_file, options.yuv_frame_width,
+                                  options.yuv_frame_height,
+                                  output_directory=options.png_output_dir):
+    print >> sys.stderr, 'An error occurred converting from YUV to PNG frames.'
+    return -1
+
   # Decode the barcodes from the PNG frames.
-  decode_frames(options.barcode_width, options.barcode_height,
-                input_directory=options.png_input_dir, path_to_zxing=zxing_dir)
+  if not decode_frames(options.barcode_width, options.barcode_height,
+                       input_directory=options.png_input_dir,
+                       path_to_zxing=zxing_dir):
+    print >> sys.stderr, ('An error occurred decoding barcodes from PNG frames.'
+                          'Have you built the zxing library JAR files?')
+    return -2
+
   # Generate statistics file.
   _generate_stats_file(options.stats_file,
                        input_directory=options.png_input_dir)
-
+  return 0
 
 if __name__ == '__main__':
   sys.exit(_main())
diff --git a/tools/barcode_tools/barcode_encoder.py b/tools/barcode_tools/barcode_encoder.py
index 429866e..e2c1625 100755
--- a/tools/barcode_tools/barcode_encoder.py
+++ b/tools/barcode_tools/barcode_encoder.py
@@ -60,7 +60,7 @@
       helper_functions.run_shell_command(
           command, msg=('Error during barcode %s generation' % content))
     except helper_functions.HelperError, err:
-      print err
+      print >> sys.stderr, err
       errors = True
   return not errors
 
@@ -112,7 +112,7 @@
                        file_name));
     os.remove(file_name)
   except helper_functions.HelperError, err:
-    print err
+    print >> sys.stderr, err
     return False
   return True
 
@@ -355,4 +355,4 @@
 
 
 if __name__ == '__main__':
-  sys.exit(_main())
\ No newline at end of file
+  sys.exit(_main())
diff --git a/tools/barcode_tools/build_zxing.py b/tools/barcode_tools/build_zxing.py
old mode 100644
new mode 100755
index 62a29ef..9e737f2
--- a/tools/barcode_tools/build_zxing.py
+++ b/tools/barcode_tools/build_zxing.py
@@ -21,11 +21,12 @@
                                stderr=subprocess.PIPE)
     stdout, stderr = process.communicate()
     if process.returncode != 0:
-      print 'Failed to execute: %s\nError: %s' % (' '.join(cmd), stderr)
+      print >> sys.stderr, 'Failed to execute: %s\nError: %s' % (' '.join(cmd),
+                                                                 stderr)
     else:
       print stdout
   except Exception as e:
-    print 'Failed to execute: %s\nError: %s' % (' '.join(cmd), e)
+    print >> sys.stderr, 'Failed to execute: %s\nError: %s' % (' '.join(cmd), e)
 
 
 def _main():
@@ -38,4 +39,4 @@
 
 
 if __name__ == '__main__':
-  sys.exit(_main())
\ No newline at end of file
+  sys.exit(_main())
diff --git a/tools/barcode_tools/helper_functions.py b/tools/barcode_tools/helper_functions.py
index 74ff064..0342497 100644
--- a/tools/barcode_tools/helper_functions.py
+++ b/tools/barcode_tools/helper_functions.py
@@ -55,7 +55,7 @@
   output, error = process.communicate()
   if process.returncode != 0:
     if msg:
-      print msg
+      print >> sys.stderr, msg
     raise HelperError('Failed to run %s: command returned %d and printed '
                       '%s and %s' % (cmd, process.returncode, output, error))
   return output.strip()
@@ -90,7 +90,8 @@
     file_pattern(string): The name pattern of the files.
     file_extension(string): The files' extension.
     start_number(int): From where to start to count frames.
-    action(function): The action to be performed over the files.
+    action(function): The action to be performed over the files. Must return
+      False if the action failed, True otherwise.
 
   Return:
     (bool): Whether performing the action over all files was successful or not.
@@ -106,6 +107,7 @@
     if os.path.isfile(file_name):
       if not action(file_name=file_name, **kwargs):
         errors = True
+        break
       file_number += 1
     else:
       file_exists = False