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
34 import rsutil.par2parser
37 (SUCCESS, ECHECK, EEXTRACT, EDELETE, ECREATE, EDETECT, EPARSE) = range(7)
39 def find_matches (regex, li, ignorecase=True):
42 cregex = re.compile (regex, re.IGNORECASE)
44 cregex = re.compile (regex)
46 return [e for e in li if cregex.match (e)]
48 def has_a_match (regex, li, ignorecase=True):
51 cregex = re.compile (regex, re.IGNORECASE)
53 cregex = re.compile (regex)
61 def no_duplicates (li):
62 """Removes all duplicates from a list"""
65 def run_command (cmd, indir=None):
66 # Runs the specified command-line in the directory given (or, in the current directory
67 # if none is given). It returns the status code given by the application.
72 assert os.path.isdir (indir) # MUST be a directory!
80 return os.path.abspath (os.path.expanduser (p))
82 def find_par2_files (files):
83 """Find all par2 files in the list $files"""
85 PAR2_REGEX = config_get_value ('regular expressions', 'par2_regex')
86 regex = re.compile (PAR2_REGEX, re.IGNORECASE)
87 return [f for f in files if regex.match (f)]
89 def find_name_matches (dir, basename):
90 """Finds files which are likely to be part of the set corresponding
91 to $name in the directory $dir"""
93 assert os.path.isdir (dir)
95 ename = re.escape (basename)
96 regex = re.compile ('^%s.*$' % (ename, ))
98 return [f for f in os.listdir (dir) if regex.match (f)]
100 def parse_all_par2 (dir, p2head, p2files):
101 """Searches though p2files and tries to parse at least one of them"""
107 # Exit early if we've found a good file
112 files = rsutil.par2parser.get_protected_files (dir, f)
114 except (EnvironmentError, OSError, OverflowError):
115 logging.warning ('Corrupt PAR2 file: %s' % f)
117 # Now that we're out of the loop, check if we really finished
119 logging.critical ('All PAR2 files corrupt for: %s' % p2head)
121 # Return whatever we've got, empty or not
124 def get_basename (name):
125 """Strips most kinds of endings from a filename"""
127 regex = config_get_value ('regular expressions', 'basename_regex')
128 r = re.compile (regex, re.IGNORECASE)
135 g = r.match (name).groups()
141 def list_eq (l1, l2):
143 if len(l1) != len(l2):
152 # Convience functions for the config
154 def config_get_value (section, name):
155 return rsutil.globals.config.get_value (section, name)
157 # Convience functions for the options
159 def options_get_value (name):
160 return getattr (rsutil.globals.options, name)