Major Update
[rarslave2.git] / PAR2Set / utils.py
diff --git a/PAR2Set/utils.py b/PAR2Set/utils.py
new file mode 100644 (file)
index 0000000..8cd9408
--- /dev/null
@@ -0,0 +1,118 @@
+#!/usr/bin/env python
+# vim: set ts=4 sts=4 sw=4 textwidth=80:
+
+"""
+Module holding some utilities for use in the PAR2Set classes
+"""
+
+__author__    = "Ira W. Snyder (devel@irasnyder.com)"
+__copyright__ = "Copyright (c) 2008, Ira W. Snyder (devel@irasnyder.com)"
+__license__   = "GNU GPL v2 (or, at your option, any later version)"
+
+#    utilities.py -- some common utilities for use in PAR2Set classes
+#
+#    Copyright (C) 2008 Ira W. Snyder (devel@irasnyder.com)
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+
+import os, re, logging, subprocess
+
+################################################################################
+
+# Strip most types of endings from a filename
+def getBasename(fileName):
+
+    # This regular expression should do pretty good at stripping all of the
+    # common suffixes of of the files that rarslave is intended to work with
+    regex = r'^(.+)\.(par2|vol\d+\+\d+|\d\d\d|part\d+|rar|zip|avi|mp4|mkv|ogm)$'
+    r = re.compile (regex, re.IGNORECASE)
+
+    # Strip off the suffixes one at a time until
+    while True:
+        match = r.match(fileName)
+
+        if match == None:
+            break
+
+        fileName = match.groups()[0]
+
+    # We've stripped everything, return the baseName
+    return fileName
+
+################################################################################
+
+# Find all of the files in the given directory that have baseName at the
+# beginning of their name
+def findFileNameMatches(directory, baseName):
+
+    ename = re.escape(baseName)
+    regex = re.compile(r'^%s.*$' % ename)
+    files = os.listdir(directory)
+
+    return [f for f in files if regex.match(f)]
+
+################################################################################
+
+def findMatches(regex, iterateable, ignoreCase=True):
+
+    if ignoreCase:
+        compiledRegex = re.compile(regex, re.IGNORECASE)
+    else:
+        compiledRegex = re.compile(regex)
+
+    return [e for e in iterateable if compiledRegex.match(e)]
+
+################################################################################
+
+def hasAMatch(regex, iterateable, ignoreCase=True):
+
+    if ignoreCase:
+        compiledRegex = re.compile(regex, re.IGNORECASE)
+    else:
+        compiledRegex = re.compile(regex)
+
+    for e in iterateable:
+        if compiledRegex.match(e):
+            return True
+
+    return False
+
+################################################################################
+
+# Run the specified command-list in the given directory
+# @cmd a list formatted for the subprocess module
+# @directory the directory in which to run the command
+# @return the status code returned by the command
+#
+# Exceptions:
+# subprocess.CalledProcessError when the called process return code is not 0
+def runCommand(cmd, directory):
+
+    logging.debug('===== BEGIN runCommand() DEBUG =====')
+    logging.debug('Directory: %s' % directory)
+    logging.debug('Command: %s'% cmd[0])
+    for arg in cmd[1:]:
+        logging.debug('-> %s' % arg)
+    logging.debug('===== END runCommand() DEBUG =====')
+
+    return subprocess.check_call(cmd, cwd=directory)
+
+################################################################################
+
+# Return the canonical absolute path from a relative path
+def absolutePath(path):
+    return os.path.abspath(os.path.expanduser(path))
+
+################################################################################
+