2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
5 Module holding all of the common functions used throughout the rarslave project
8 __author__ = "Ira W. Snyder (devel@irasnyder.com)"
9 __copyright__ = "Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)"
10 __license__ = "GNU GPL v2 (or, at your option, any later version)"
12 # common.py -- holds all of the common functions used throughout the rarslave project
14 # Copyright (C) 2006,2007 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
36 import rsutil.par2parser
38 def find_matches (regex, li, ignorecase=True):
41 cregex = re.compile (regex, re.IGNORECASE)
43 cregex = re.compile (regex)
45 return [e for e in li if cregex.match (e)]
47 def has_a_match (regex, li, ignorecase=True):
50 cregex = re.compile (regex, re.IGNORECASE)
52 cregex = re.compile (regex)
60 def no_duplicates (li):
61 """Removes all duplicates from a list"""
64 def run_command (cmd, indir=None):
65 # Runs the specified command-line in the directory given (or, in the current directory
66 # if none is given). It returns the status code given by the application.
71 assert os.path.isdir (indir) # MUST be a directory!
73 print 'RUNNING COMMAND'
74 print 'Directory: %s' % indir
75 print 'Command: %s' % cmd[0]
79 ret = subprocess.Popen(cmd, cwd=indir).wait()
87 return os.path.abspath (os.path.expanduser (p))
89 def find_par2_files (files):
90 """Find all par2 files in the list $files"""
92 PAR2_REGEX = config_get_value ('regular expressions', 'par2_regex')
93 regex = re.compile (PAR2_REGEX, re.IGNORECASE)
94 return [f for f in files if regex.match (f)]
96 def find_name_matches (dir, basename):
97 """Finds files which are likely to be part of the set corresponding
98 to $name in the directory $dir"""
100 assert os.path.isdir (dir)
102 ename = re.escape (basename)
103 regex = re.compile ('^%s.*$' % (ename, ))
105 return [f for f in os.listdir (dir) if regex.match (f)]
107 def parse_all_par2 (dir, p2head, p2files):
108 """Searches though p2files and tries to parse at least one of them"""
114 # Exit early if we've found a good file
119 files = rsutil.par2parser.get_protected_files (dir, f)
121 except (EnvironmentError, OSError, OverflowError):
122 logging.warning ('Corrupt PAR2 file: %s' % f)
124 # Now that we're out of the loop, check if we really finished
126 logging.critical ('All PAR2 files corrupt for: %s' % p2head)
128 # Return whatever we've got, empty or not
131 def get_basename (name):
132 """Strips most kinds of endings from a filename"""
134 regex = config_get_value ('regular expressions', 'basename_regex')
135 r = re.compile (regex, re.IGNORECASE)
142 g = r.match (name).groups()
148 def list_eq (l1, l2):
150 if len(l1) != len(l2):
153 return set(l1) == set(l2)
155 # Convience functions for the config
157 def config_get_value (section, name):
158 return rsutil.globals.config.get_value (section, name)
160 # Convience functions for the options
162 def options_get_value (name):
163 return getattr (rsutil.globals.options, name)