blob: 74ff06452d66e76df3af68c342b3d5c18f9b207d [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
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +000037def 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:
58 print msg
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
64def 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
80def 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.
93 action(function): The action to be performed over the files.
94
95 Return:
96 (bool): Whether performing the action over all files was successful or not.
97 """
98 file_prefix = os.path.join(directory, file_pattern)
99 file_exists = True
100 file_number = start_number
101 errors = False
102
103 while file_exists:
104 zero_padded_file_number = zero_pad(file_number)
105 file_name = file_prefix + zero_padded_file_number + '.' + file_extension
106 if os.path.isfile(file_name):
107 if not action(file_name=file_name, **kwargs):
108 errors = True
109 file_number += 1
110 else:
111 file_exists = False
112 return not errors
113
114