blob: cdd0219abea425a419ddad531b75f6817eb3689e [file] [log] [blame]
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +00001#!/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
10import os
11import subprocess
12import sys
13
14_DEFAULT_PADDING = 4
15
16
17class HelperError(Exception):
18 """Exception raised for errors in the helper."""
19 pass
20
21
22def 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
37def delete_file(file_name):
38 """Deletes the file with file_name.
39
40 Args:
41 file_name(string): The file to be deleted.
42 Return:
43 (bool): True on success, False otherwise.
44 """
45 try:
46 subprocess.check_call(['rm', '%s' % file_name])
47 except subprocess.CalledProcessError, err:
48 sys.stderr.write('Error in deleting file %s' % file_name)
49 return False
50 return True
51
52
53def run_shell_command(command, msg=None):
54 """Executes a command.
55
56 Args:
57 command(list): Command list to execute.
58 msg(string): Message describing the error in case the command fails.
59
60 Return:
61 (string): The standard output from running the command.
62
63 Raise:
64 HelperError: If command fails.
65 """
66 cmd_list = [str(x) for x in command]
67 cmd = ' '.join(cmd_list)
68
69 process = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
70 stderr=subprocess.PIPE)
71 output, error = process.communicate()
72 if process.returncode != 0:
73 if msg:
74 print msg
75 raise HelperError('Failed to run %s: command returned %d and printed '
76 '%s and %s' % (cmd, process.returncode, output, error))
77 return output.strip()
78
79
80def form_jars_string(path_to_zxing):
81 """Forms the the Zxing core and javase jars argument.
82
83 Args:
84 path_to_zxing(string): The path to the Zxing checkout folder.
85 Return:
86 (string): The newly formed jars argument.
87 """
88 javase_jar = os.path.join(path_to_zxing, "javase", "javase.jar")
89 core_jar = os.path.join(path_to_zxing, "core", "core.jar")
90 delimiter = ':'
91 if os.name != 'posix':
92 delimiter = ';'
93 return javase_jar + delimiter + core_jar
94
95
96def perform_action_on_all_files(directory, file_pattern, file_extension,
97 start_number, action, **kwargs):
98 """Function that performs a given action on all files matching a pattern.
99
100 It is assumed that the files are named file_patternxxxx.file_extension, where
101 xxxx are digits. The file names start from
102 file_patern0..start_number>.file_extension.
103
104 Args:
105 directory(string): The directory where the files live.
106 file_pattern(string): The name pattern of the files.
107 file_extension(string): The files' extension.
108 start_number(int): From where to start to count frames.
109 action(function): The action to be performed over the files.
110
111 Return:
112 (bool): Whether performing the action over all files was successful or not.
113 """
114 file_prefix = os.path.join(directory, file_pattern)
115 file_exists = True
116 file_number = start_number
117 errors = False
118
119 while file_exists:
120 zero_padded_file_number = zero_pad(file_number)
121 file_name = file_prefix + zero_padded_file_number + '.' + file_extension
122 if os.path.isfile(file_name):
123 if not action(file_name=file_name, **kwargs):
124 errors = True
125 file_number += 1
126 else:
127 file_exists = False
128 return not errors
129
130