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
This commit is contained in:
@@ -9,7 +9,6 @@
|
||||
|
||||
import optparse
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import helper_functions
|
||||
@@ -43,7 +42,8 @@ def convert_yuv_to_png_files(yuv_file_name, yuv_frame_width, yuv_frame_height,
|
||||
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 @@ def _decode_barcode_in_file(file_name, barcode_width, barcode_height, jars,
|
||||
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 @@ def _read_barcode_from_text_file(barcode_file_name):
|
||||
barcode_file = open(barcode_file_name, 'r')
|
||||
barcode = barcode_file.read()
|
||||
barcode_file.close()
|
||||
|
||||
return barcode
|
||||
|
||||
|
||||
@@ -270,16 +269,24 @@ def _main():
|
||||
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())
|
||||
|
@@ -60,7 +60,7 @@ def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
|
||||
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 @@ def _convert_to_yuv_and_delete(output_directory, file_name, pattern):
|
||||
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 @@ def _main():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(_main())
|
||||
sys.exit(_main())
|
||||
|
7
tools/barcode_tools/build_zxing.py
Normal file → Executable file
7
tools/barcode_tools/build_zxing.py
Normal file → Executable file
@@ -21,11 +21,12 @@ def run_ant_build_command(path_to_ant_build_file):
|
||||
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 @@ def _main():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(_main())
|
||||
sys.exit(_main())
|
||||
|
@@ -55,7 +55,7 @@ def run_shell_command(command, msg=None):
|
||||
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 @@ def perform_action_on_all_files(directory, file_pattern, file_extension,
|
||||
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 @@ def perform_action_on_all_files(directory, file_pattern, file_extension,
|
||||
if os.path.isfile(file_name):
|
||||
if not action(file_name=file_name, **kwargs):
|
||||
errors = True
|
||||
break
|
||||
file_number += 1
|
||||
else:
|
||||
file_exists = False
|
||||
|
Reference in New Issue
Block a user