2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
4 __author__ = "Ira W. Snyder (devel@irasnyder.com)"
5 __copyright__ = "Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)"
6 __license__ = "GNU GPL v2 (or, at your option, any later version)"
8 # This module holds all of the common functions used throughout the rarslave project.
13 import RarslaveGlobals
18 (SUCCESS, ECHECK, EEXTRACT, EDELETE, ECREATE, EDETECT, EPARSE) = range(7)
20 def find_matches (regex, li, ignorecase=True):
23 cregex = re.compile (regex, re.IGNORECASE)
25 cregex = re.compile (regex)
27 return [e for e in li if cregex.match (e)]
29 def has_a_match (regex, li, ignorecase=True):
32 cregex = re.compile (regex, re.IGNORECASE)
34 cregex = re.compile (regex)
42 def no_duplicates (li):
43 """Removes all duplicates from a list"""
46 def run_command (cmd, indir=None):
47 # Runs the specified command-line in the directory given (or, in the current directory
48 # if none is given). It returns the status code given by the application.
53 assert os.path.isdir (indir) # MUST be a directory!
61 return os.path.abspath (os.path.expanduser (p))
63 def find_par2_files (files):
64 """Find all par2 files in the list $files"""
66 PAR2_REGEX = config_get_value ('regular expressions', 'par2_regex')
67 regex = re.compile (PAR2_REGEX, re.IGNORECASE)
68 return [f for f in files if regex.match (f)]
70 def find_name_matches (dir, basename):
71 """Finds files which are likely to be part of the set corresponding
72 to $name in the directory $dir"""
74 assert os.path.isdir (dir)
76 ename = re.escape (basename)
77 regex = re.compile ('^%s.*$' % (ename, ))
79 return [f for f in os.listdir (dir) if regex.match (f)]
81 def parse_all_par2 (dir, p2head, p2files):
82 """Searches though p2files and tries to parse at least one of them"""
88 # Exit early if we've found a good file
93 files = Par2Parser.get_protected_files (dir, f)
95 except (EnvironmentError, OSError, OverflowError):
96 verboseMessage ('Corrupt PAR2 file: %s' % f)
98 # Now that we're out of the loop, check if we really finished
100 fatalMessage ('All PAR2 files corrupt for: %s' % p2head)
102 # Return whatever we've got, empty or not
105 def get_basename (name):
106 """Strips most kinds of endings from a filename"""
108 regex = config_get_value ('regular expressions', 'basename_regex')
109 r = re.compile (regex, re.IGNORECASE)
116 g = r.match (name).groups()
122 def list_eq (l1, l2):
124 if len(l1) != len(l2):
133 # Convience functions for the logger
135 def fatalMessage (msg):
136 RarslaveGlobals.logger.addMessage (msg, RarslaveLogger.MessageType.Fatal)
138 def normalMessage (msg):
139 RarslaveGlobals.logger.addMessage (msg, RarslaveLogger.MessageType.Normal)
141 def verboseMessage (msg):
142 RarslaveGlobals.logger.addMessage (msg, RarslaveLogger.MessageType.Verbose)
144 def debugMessage (msg):
145 RarslaveGlobals.logger.addMessage (msg, RarslaveLogger.MessageType.Debug)
147 # Convience functions for the config
149 def config_get_value (section, name):
150 return RarslaveGlobals.config.get_value (section, name)
152 # Convience functions for the options
154 def options_get_value (name):
155 return getattr (RarslaveGlobals.options, name)
160 if __name__ == '__main__':