blob: bb0b167334c51f447f2b75cca2d2163ce1aa56d4 [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
kjellander@webrtc.org38ebf982013-03-08 10:58:21 +000037def run_shell_command(cmd_list, fail_msg=None):
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +000038 """Executes a command.
39
40 Args:
kjellander@webrtc.org38ebf982013-03-08 10:58:21 +000041 cmd_list(list): Command list to execute.
42 fail_msg(string): Message describing the error in case the command fails.
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +000043
44 Return:
45 (string): The standard output from running the command.
46
47 Raise:
48 HelperError: If command fails.
49 """
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +000050 process = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
51 stderr=subprocess.PIPE)
52 output, error = process.communicate()
53 if process.returncode != 0:
kjellander@webrtc.org38ebf982013-03-08 10:58:21 +000054 if fail_msg:
55 print >> sys.stderr, fail_msg
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +000056 raise HelperError('Failed to run %s: command returned %d and printed '
kjellander@webrtc.org38ebf982013-03-08 10:58:21 +000057 '%s and %s' % (' '.join(cmd_list), process.returncode,
58 output, error))
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +000059 return output.strip()
60
61
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +000062def perform_action_on_all_files(directory, file_pattern, file_extension,
63 start_number, action, **kwargs):
64 """Function that performs a given action on all files matching a pattern.
65
66 It is assumed that the files are named file_patternxxxx.file_extension, where
67 xxxx are digits. The file names start from
68 file_patern0..start_number>.file_extension.
69
70 Args:
71 directory(string): The directory where the files live.
72 file_pattern(string): The name pattern of the files.
73 file_extension(string): The files' extension.
74 start_number(int): From where to start to count frames.
kjellander@webrtc.orgccb52c22012-10-10 16:11:28 +000075 action(function): The action to be performed over the files. Must return
76 False if the action failed, True otherwise.
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +000077
78 Return:
79 (bool): Whether performing the action over all files was successful or not.
80 """
81 file_prefix = os.path.join(directory, file_pattern)
82 file_exists = True
83 file_number = start_number
84 errors = False
85
86 while file_exists:
87 zero_padded_file_number = zero_pad(file_number)
88 file_name = file_prefix + zero_padded_file_number + '.' + file_extension
89 if os.path.isfile(file_name):
90 if not action(file_name=file_name, **kwargs):
91 errors = True
kjellander@webrtc.orgccb52c22012-10-10 16:11:28 +000092 break
vspasova@webrtc.org400e7da2012-08-15 10:25:12 +000093 file_number += 1
94 else:
95 file_exists = False
96 return not errors