diff --git a/tools/barcode_tools/barcode_decoder.py b/tools/barcode_tools/barcode_decoder.py index 9c71c16d4..87b3ec299 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 @@ 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()) diff --git a/tools/barcode_tools/barcode_encoder.py b/tools/barcode_tools/barcode_encoder.py index 429866e4f..e2c162576 100755 --- a/tools/barcode_tools/barcode_encoder.py +++ b/tools/barcode_tools/barcode_encoder.py @@ -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()) \ 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 62a29efae..9e737f2f7 --- a/tools/barcode_tools/build_zxing.py +++ b/tools/barcode_tools/build_zxing.py @@ -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()) \ 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 74ff06452..034249701 100644 --- a/tools/barcode_tools/helper_functions.py +++ b/tools/barcode_tools/helper_functions.py @@ -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