vspasova@webrtc.org | 400e7da | 2012-08-15 10:25:12 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | # |
| 4 | # Use of this source code is governed by a BSD-style license |
| 5 | # that can be found in the LICENSE file in the root of the source |
| 6 | # tree. An additional intellectual property rights grant can be found |
| 7 | # in the file PATENTS. All contributing project authors may |
| 8 | # be found in the AUTHORS file in the root of the source tree. |
| 9 | |
| 10 | import os |
| 11 | import subprocess |
| 12 | import sys |
| 13 | |
| 14 | _DEFAULT_PADDING = 4 |
| 15 | |
| 16 | |
| 17 | class HelperError(Exception): |
| 18 | """Exception raised for errors in the helper.""" |
| 19 | pass |
| 20 | |
| 21 | |
| 22 | def zero_pad(number, padding=_DEFAULT_PADDING): |
| 23 | """Converts an int into a zero padded string. |
| 24 | |
| 25 | Args: |
| 26 | number(int): The number to convert. |
| 27 | padding(int): The number of chars in the output. Note that if you pass for |
| 28 | example number=23456 and padding=4, the output will still be '23456', |
| 29 | i.e. it will not be cropped. If you pass number=2 and padding=4, the |
| 30 | return value will be '0002'. |
| 31 | Return: |
| 32 | (string): The zero padded number converted to string. |
| 33 | """ |
| 34 | return str(number).zfill(padding) |
| 35 | |
| 36 | |
vspasova@webrtc.org | 400e7da | 2012-08-15 10:25:12 +0000 | [diff] [blame] | 37 | def run_shell_command(command, msg=None): |
| 38 | """Executes a command. |
| 39 | |
| 40 | Args: |
| 41 | command(list): Command list to execute. |
| 42 | msg(string): Message describing the error in case the command fails. |
| 43 | |
| 44 | Return: |
| 45 | (string): The standard output from running the command. |
| 46 | |
| 47 | Raise: |
| 48 | HelperError: If command fails. |
| 49 | """ |
| 50 | cmd_list = [str(x) for x in command] |
| 51 | cmd = ' '.join(cmd_list) |
| 52 | |
| 53 | process = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, |
| 54 | stderr=subprocess.PIPE) |
| 55 | output, error = process.communicate() |
| 56 | if process.returncode != 0: |
| 57 | if msg: |
kjellander@webrtc.org | ccb52c2 | 2012-10-10 16:11:28 +0000 | [diff] [blame^] | 58 | print >> sys.stderr, msg |
vspasova@webrtc.org | 400e7da | 2012-08-15 10:25:12 +0000 | [diff] [blame] | 59 | raise HelperError('Failed to run %s: command returned %d and printed ' |
| 60 | '%s and %s' % (cmd, process.returncode, output, error)) |
| 61 | return output.strip() |
| 62 | |
| 63 | |
| 64 | def form_jars_string(path_to_zxing): |
| 65 | """Forms the the Zxing core and javase jars argument. |
| 66 | |
| 67 | Args: |
| 68 | path_to_zxing(string): The path to the Zxing checkout folder. |
| 69 | Return: |
| 70 | (string): The newly formed jars argument. |
| 71 | """ |
| 72 | javase_jar = os.path.join(path_to_zxing, "javase", "javase.jar") |
| 73 | core_jar = os.path.join(path_to_zxing, "core", "core.jar") |
| 74 | delimiter = ':' |
| 75 | if os.name != 'posix': |
| 76 | delimiter = ';' |
| 77 | return javase_jar + delimiter + core_jar |
| 78 | |
| 79 | |
| 80 | def perform_action_on_all_files(directory, file_pattern, file_extension, |
| 81 | start_number, action, **kwargs): |
| 82 | """Function that performs a given action on all files matching a pattern. |
| 83 | |
| 84 | It is assumed that the files are named file_patternxxxx.file_extension, where |
| 85 | xxxx are digits. The file names start from |
| 86 | file_patern0..start_number>.file_extension. |
| 87 | |
| 88 | Args: |
| 89 | directory(string): The directory where the files live. |
| 90 | file_pattern(string): The name pattern of the files. |
| 91 | file_extension(string): The files' extension. |
| 92 | start_number(int): From where to start to count frames. |
kjellander@webrtc.org | ccb52c2 | 2012-10-10 16:11:28 +0000 | [diff] [blame^] | 93 | action(function): The action to be performed over the files. Must return |
| 94 | False if the action failed, True otherwise. |
vspasova@webrtc.org | 400e7da | 2012-08-15 10:25:12 +0000 | [diff] [blame] | 95 | |
| 96 | Return: |
| 97 | (bool): Whether performing the action over all files was successful or not. |
| 98 | """ |
| 99 | file_prefix = os.path.join(directory, file_pattern) |
| 100 | file_exists = True |
| 101 | file_number = start_number |
| 102 | errors = False |
| 103 | |
| 104 | while file_exists: |
| 105 | zero_padded_file_number = zero_pad(file_number) |
| 106 | file_name = file_prefix + zero_padded_file_number + '.' + file_extension |
| 107 | if os.path.isfile(file_name): |
| 108 | if not action(file_name=file_name, **kwargs): |
| 109 | errors = True |
kjellander@webrtc.org | ccb52c2 | 2012-10-10 16:11:28 +0000 | [diff] [blame^] | 110 | break |
vspasova@webrtc.org | 400e7da | 2012-08-15 10:25:12 +0000 | [diff] [blame] | 111 | file_number += 1 |
| 112 | else: |
| 113 | file_exists = False |
| 114 | return not errors |
| 115 | |
| 116 | |