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