Home | History | Annotate | Download | only in barcode_tools
      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 
     37 def run_shell_command(cmd_list, fail_msg=None):
     38   """Executes a command.
     39 
     40   Args:
     41     cmd_list(list): Command list to execute.
     42     fail_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   process = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
     51                              stderr=subprocess.PIPE)
     52   output, error = process.communicate()
     53   if process.returncode != 0:
     54     if fail_msg:
     55       print >> sys.stderr, fail_msg
     56     raise HelperError('Failed to run %s: command returned %d and printed '
     57                       '%s and %s' % (' '.join(cmd_list), process.returncode,
     58                                      output, error))
     59   return output.strip()
     60 
     61 
     62 def 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.
     75     action(function): The action to be performed over the files. Must return
     76       False if the action failed, True otherwise.
     77 
     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
     92         break
     93       file_number += 1
     94     else:
     95       file_exists = False
     96   return not errors
     97