2 # vim: set ts=4 sts=4 sw=4 textwidth=80:
5 Module holding some utilities for use in the PAR2Set classes
8 __author__ = "Ira W. Snyder (devel@irasnyder.com)"
9 __copyright__ = "Copyright (c) 2008, Ira W. Snyder (devel@irasnyder.com)"
10 __license__ = "GNU GPL v2 (or, at your option, any later version)"
12 # utilities.py -- some common utilities for use in PAR2Set classes
14 # Copyright (C) 2008 Ira W. Snyder (devel@irasnyder.com)
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
29 import os, re, logging, subprocess
31 ################################################################################
33 # Strip most types of endings from a filename
34 def getBasename(fileName):
36 # This regular expression should do pretty good at stripping all of the
37 # common suffixes of of the files that rarslave is intended to work with
38 regex = r'^(.+)\.(par2|vol\d+\+\d+|\d\d\d|part\d+|rar|zip|avi|mp4|mkv|ogm)$'
39 r = re.compile (regex, re.IGNORECASE)
41 # Strip off the suffixes one at a time until
43 match = r.match(fileName)
48 fileName = match.groups()[0]
50 # We've stripped everything, return the baseName
53 ################################################################################
55 # Find all of the files in the given directory that have baseName at the
56 # beginning of their name
57 def findFileNameMatches(directory, baseName):
59 ename = re.escape(baseName)
60 regex = re.compile(r'^%s.*$' % ename)
61 files = os.listdir(directory)
63 return [f for f in files if regex.match(f)]
65 ################################################################################
67 def findMatches(regex, iterateable, ignoreCase=True):
70 compiledRegex = re.compile(regex, re.IGNORECASE)
72 compiledRegex = re.compile(regex)
74 return [e for e in iterateable if compiledRegex.match(e)]
76 ################################################################################
78 def hasAMatch(regex, iterateable, ignoreCase=True):
81 compiledRegex = re.compile(regex, re.IGNORECASE)
83 compiledRegex = re.compile(regex)
86 if compiledRegex.match(e):
91 ################################################################################
93 # Run the specified command-list in the given directory
94 # @cmd a list formatted for the subprocess module
95 # @directory the directory in which to run the command
96 # @stdout the stdout file descriptor, following the rules of the subprocess module
97 # @return the status code returned by the command
100 # subprocess.CalledProcessError when the called process return code is not 0
101 def runCommand(cmd, directory, stdout=None):
103 logging.debug('===== BEGIN runCommand() DEBUG =====')
104 logging.debug('Directory: %s' % directory)
105 logging.debug('Command: %s'% cmd[0])
107 logging.debug('-> %s' % arg)
108 logging.debug('===== END runCommand() DEBUG =====')
110 return subprocess.check_call(cmd, cwd=directory, stdout=stdout)
112 ################################################################################
114 # Return the canonical absolute path from a relative path
115 def absolutePath(path):
116 return os.path.abspath(os.path.expanduser(path))
118 ################################################################################