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 optparse
|
||||||
import os
|
import os
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import helper_functions
|
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(
|
helper_functions.run_shell_command(
|
||||||
command, msg='Error during YUV to PNG conversion')
|
command, msg='Error during YUV to PNG conversion')
|
||||||
except helper_functions.HelperError, err:
|
except helper_functions.HelperError, err:
|
||||||
print err
|
print >> sys.stderr, 'Error executing command: %s. Error: %s' % (command,
|
||||||
|
err)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -101,10 +101,10 @@ def _decode_barcode_in_file(file_name, barcode_width, barcode_height, jars,
|
|||||||
out = helper_functions.run_shell_command(
|
out = helper_functions.run_shell_command(
|
||||||
command, msg='Error during decoding of %s' % file_name)
|
command, msg='Error during decoding of %s' % file_name)
|
||||||
if not 'Success' in out:
|
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
|
return False
|
||||||
except helper_functions.HelperError, err:
|
except helper_functions.HelperError, err:
|
||||||
print err
|
print >> sys.stderr, err
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -155,7 +155,6 @@ def _read_barcode_from_text_file(barcode_file_name):
|
|||||||
barcode_file = open(barcode_file_name, 'r')
|
barcode_file = open(barcode_file_name, 'r')
|
||||||
barcode = barcode_file.read()
|
barcode = barcode_file.read()
|
||||||
barcode_file.close()
|
barcode_file.close()
|
||||||
|
|
||||||
return barcode
|
return barcode
|
||||||
|
|
||||||
|
|
||||||
@@ -270,16 +269,24 @@ def _main():
|
|||||||
zxing_dir = os.path.join(script_dir, 'third_party', 'zxing')
|
zxing_dir = os.path.join(script_dir, 'third_party', 'zxing')
|
||||||
|
|
||||||
# Convert the overlaid YUV video into a set of PNG frames.
|
# Convert the overlaid YUV video into a set of PNG frames.
|
||||||
convert_yuv_to_png_files(options.yuv_file, options.yuv_frame_width,
|
if not convert_yuv_to_png_files(options.yuv_file, options.yuv_frame_width,
|
||||||
options.yuv_frame_height,
|
options.yuv_frame_height,
|
||||||
output_directory=options.png_output_dir)
|
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 the barcodes from the PNG frames.
|
||||||
decode_frames(options.barcode_width, options.barcode_height,
|
if not decode_frames(options.barcode_width, options.barcode_height,
|
||||||
input_directory=options.png_input_dir, path_to_zxing=zxing_dir)
|
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 statistics file.
|
||||||
_generate_stats_file(options.stats_file,
|
_generate_stats_file(options.stats_file,
|
||||||
input_directory=options.png_input_dir)
|
input_directory=options.png_input_dir)
|
||||||
|
return 0
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(_main())
|
sys.exit(_main())
|
||||||
|
@@ -60,7 +60,7 @@ def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
|
|||||||
helper_functions.run_shell_command(
|
helper_functions.run_shell_command(
|
||||||
command, msg=('Error during barcode %s generation' % content))
|
command, msg=('Error during barcode %s generation' % content))
|
||||||
except helper_functions.HelperError, err:
|
except helper_functions.HelperError, err:
|
||||||
print err
|
print >> sys.stderr, err
|
||||||
errors = True
|
errors = True
|
||||||
return not errors
|
return not errors
|
||||||
|
|
||||||
@@ -112,7 +112,7 @@ def _convert_to_yuv_and_delete(output_directory, file_name, pattern):
|
|||||||
file_name));
|
file_name));
|
||||||
os.remove(file_name)
|
os.remove(file_name)
|
||||||
except helper_functions.HelperError, err:
|
except helper_functions.HelperError, err:
|
||||||
print err
|
print >> sys.stderr, err
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
5
tools/barcode_tools/build_zxing.py
Normal file → Executable file
5
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)
|
stderr=subprocess.PIPE)
|
||||||
stdout, stderr = process.communicate()
|
stdout, stderr = process.communicate()
|
||||||
if process.returncode != 0:
|
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:
|
else:
|
||||||
print stdout
|
print stdout
|
||||||
except Exception as e:
|
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():
|
def _main():
|
||||||
|
@@ -55,7 +55,7 @@ def run_shell_command(command, msg=None):
|
|||||||
output, error = process.communicate()
|
output, error = process.communicate()
|
||||||
if process.returncode != 0:
|
if process.returncode != 0:
|
||||||
if msg:
|
if msg:
|
||||||
print msg
|
print >> sys.stderr, msg
|
||||||
raise HelperError('Failed to run %s: command returned %d and printed '
|
raise HelperError('Failed to run %s: command returned %d and printed '
|
||||||
'%s and %s' % (cmd, process.returncode, output, error))
|
'%s and %s' % (cmd, process.returncode, output, error))
|
||||||
return output.strip()
|
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_pattern(string): The name pattern of the files.
|
||||||
file_extension(string): The files' extension.
|
file_extension(string): The files' extension.
|
||||||
start_number(int): From where to start to count frames.
|
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:
|
Return:
|
||||||
(bool): Whether performing the action over all files was successful or not.
|
(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 os.path.isfile(file_name):
|
||||||
if not action(file_name=file_name, **kwargs):
|
if not action(file_name=file_name, **kwargs):
|
||||||
errors = True
|
errors = True
|
||||||
|
break
|
||||||
file_number += 1
|
file_number += 1
|
||||||
else:
|
else:
|
||||||
file_exists = False
|
file_exists = False
|
||||||
|
Reference in New Issue
Block a user