Add support for Join sets where the parity protects the split files
[rarslave2.git] / PAR2Set / utils.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=80:
3
4 """
5 Module holding some utilities for use in the PAR2Set classes
6 """
7
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)"
11
12 #    utilities.py -- some common utilities for use in PAR2Set classes
13 #
14 #    Copyright (C) 2008 Ira W. Snyder (devel@irasnyder.com)
15 #
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.
20 #
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.
25 #
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
28
29 import os, re, logging, subprocess
30
31 ################################################################################
32
33 # Strip most types of endings from a filename
34 def getBasename(fileName):
35
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)
40
41     # Strip off the suffixes one at a time until
42     while True:
43         match = r.match(fileName)
44
45         if match == None:
46             break
47
48         fileName = match.groups()[0]
49
50     # We've stripped everything, return the baseName
51     return fileName
52
53 ################################################################################
54
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):
58
59     ename = re.escape(baseName)
60     regex = re.compile(r'^%s.*$' % ename)
61     files = os.listdir(directory)
62
63     return [f for f in files if regex.match(f)]
64
65 ################################################################################
66
67 def findMatches(regex, iterateable, ignoreCase=True):
68
69     if ignoreCase:
70         compiledRegex = re.compile(regex, re.IGNORECASE)
71     else:
72         compiledRegex = re.compile(regex)
73
74     return [e for e in iterateable if compiledRegex.match(e)]
75
76 ################################################################################
77
78 def hasAMatch(regex, iterateable, ignoreCase=True):
79
80     if ignoreCase:
81         compiledRegex = re.compile(regex, re.IGNORECASE)
82     else:
83         compiledRegex = re.compile(regex)
84
85     for e in iterateable:
86         if compiledRegex.match(e):
87             return True
88
89     return False
90
91 ################################################################################
92
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
98 #
99 # Exceptions:
100 # subprocess.CalledProcessError when the called process return code is not 0
101 def runCommand(cmd, directory, stdout=None):
102
103     logging.debug('===== BEGIN runCommand() DEBUG =====')
104     logging.debug('Directory: %s' % directory)
105     logging.debug('Command: %s'% cmd[0])
106     for arg in cmd[1:]:
107         logging.debug('-> %s' % arg)
108     logging.debug('===== END runCommand() DEBUG =====')
109
110     return subprocess.check_call(cmd, cwd=directory, stdout=stdout)
111
112 ################################################################################
113
114 # Return the canonical absolute path from a relative path
115 def absolutePath(path):
116     return os.path.abspath(os.path.expanduser(path))
117
118 ################################################################################
119