8ffac912cde3d18d7ed2d84102b5dfd8797e85b6
[rarslave2.git] / RarslaveCommon.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
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)"
7
8 # This module holds all of the common functions used throughout the rarslave project.
9
10 import os
11 import re
12 import logging
13
14 import RarslaveGlobals
15 import Par2Parser
16
17 # Global constants
18 (SUCCESS, ECHECK, EEXTRACT, EDELETE, ECREATE, EDETECT, EPARSE) = range(7)
19
20 def find_matches (regex, li, ignorecase=True):
21
22         if ignorecase:
23                 cregex = re.compile (regex, re.IGNORECASE)
24         else:
25                 cregex = re.compile (regex)
26
27         return [e for e in li if cregex.match (e)]
28
29 def has_a_match (regex, li, ignorecase=True):
30
31         if ignorecase:
32                 cregex = re.compile (regex, re.IGNORECASE)
33         else:
34                 cregex = re.compile (regex)
35
36         for e in li:
37                 if cregex.match (e):
38                         return True
39
40         return False
41
42 def no_duplicates (li):
43         """Removes all duplicates from a list"""
44         return list(set(li))
45
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.
49
50         pwd = os.getcwd ()
51
52         if indir != None:
53                 assert os.path.isdir (indir) # MUST be a directory!
54                 os.chdir (indir)
55
56         ret = os.system (cmd)
57         os.chdir (pwd)
58         return ret
59
60 def full_abspath (p):
61         return os.path.abspath (os.path.expanduser (p))
62
63 def find_par2_files (files):
64         """Find all par2 files in the list $files"""
65
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)]
69
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"""
73
74         assert os.path.isdir (dir)
75
76         ename = re.escape (basename)
77         regex = re.compile ('^%s.*$' % (ename, ))
78
79         return [f for f in os.listdir (dir) if regex.match (f)]
80
81 def parse_all_par2 (dir, p2head, p2files):
82         """Searches though p2files and tries to parse at least one of them"""
83         done = False
84         files = []
85
86         for f in p2files:
87
88                 # Exit early if we've found a good file
89                 if done:
90                         break
91
92                 try:
93                         files = Par2Parser.get_protected_files (dir, f)
94                         done = True
95                 except (EnvironmentError, OSError, OverflowError):
96                         logging.warning ('Corrupt PAR2 file: %s' % f)
97
98         # Now that we're out of the loop, check if we really finished
99         if not done:
100                 logging.critical ('All PAR2 files corrupt for: %s' % p2head)
101
102         # Return whatever we've got, empty or not
103         return files
104
105 def get_basename (name):
106         """Strips most kinds of endings from a filename"""
107
108         regex = config_get_value ('regular expressions', 'basename_regex')
109         r = re.compile (regex, re.IGNORECASE)
110         done = False
111
112         while not done:
113                 done = True
114
115                 if r.match (name):
116                         g = r.match (name).groups()
117                         name = g[0]
118                         done = False
119
120         return name
121
122 def list_eq (l1, l2):
123
124         if len(l1) != len(l2):
125                 return False
126
127         for e in l1:
128                 if e not in l2:
129                         return False
130
131         return True
132
133 # Convience functions for the config
134
135 def config_get_value (section, name):
136         return RarslaveGlobals.config.get_value (section, name)
137
138 # Convience functions for the options
139
140 def options_get_value (name):
141         return getattr (RarslaveGlobals.options, name)
142
143 def main ():
144         pass
145
146 if __name__ == '__main__':
147         main ()
148