[LOGGER] Switch to built-in logging class
[rarslave2.git] / PAR2Set.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
4 from RarslaveCommon import *
5 import RarslaveLogger
6 import Par2Parser
7
8 import re
9 import os
10 import logging
11
12 # This is a fairly generic class which does all of the major things that a PAR2
13 # set will need to have done to be verified and extracted. For most "normal" types
14 # you won't need to override hardly anything.
15 #
16 # It is ok to override other functions if the need arises, just make sure that you
17 # understand why things are done the way that they are in the original versions.
18 #
19 # Assumptions made about each of the run*() functions:
20 # ==============================================================================
21 # The state of self.name_matched_files and self.prot_matched_files will be consistent
22 # with the real, in-filesystem state at the time that they are called.
23 # (This is why runAll() calls update_matches() all the time.)
24 #
25 # Required overrides:
26 # ==============================================================================
27 # find_extraction_heads ()
28 # extraction_function ()
29 #
30
31 class PAR2Set (object):
32
33         # Instance Variables
34         # ==========================================================================
35         # dir                                   -- The directory this set lives in
36         # p2file                                -- The starting PAR2 file
37         # basename                              -- The basename of the set, guessed from the PAR2 file
38         # all_p2files                   -- All PAR2 files of the set, guessed from the PAR2 file name only
39         # name_matched_files    -- Files in this set, guessed by name only
40         # prot_matched_files    -- Files in this set, guessed by parsing the PAR2 only
41
42         def __init__ (self, dir, p2file):
43                 assert os.path.isdir (dir)
44                 assert os.path.isfile (os.path.join (dir, p2file))
45
46                 # The real "meat" of the class
47                 self.dir = dir
48                 self.p2file = p2file
49                 self.basename = get_basename (p2file)
50
51                 # Find files that match by name only
52                 self.name_matched_files = find_name_matches (self.dir, self.basename)
53
54                 # Find all par2 files for this set using name matches
55                 self.all_p2files = find_par2_files (self.name_matched_files)
56
57                 # Try to get the protected files for this set
58                 self.prot_matched_files = parse_all_par2 (self.dir, self.p2file, self.all_p2files)
59
60                 # Setup the all_files combined set (for convenience only)
61                 self.all_files = no_duplicates (self.name_matched_files + self.prot_matched_files)
62
63         def __eq__ (self, rhs):
64                 return (self.dir == rhs.dir) and (self.basename == rhs.basename) and \
65                                 list_eq (self.name_matched_files, rhs.name_matched_files) and \
66                                 list_eq (self.prot_matched_files, rhs.prot_matched_files)
67
68         def update_matches (self):
69                 """Updates the contents of instance variables which are likely to change after
70                 running an operation, usually one which will create new files."""
71
72                 self.name_matched_files = find_name_matches (self.dir, self.basename)
73                 self.all_files = no_duplicates (self.name_matched_files + self.prot_matched_files)
74
75         def runVerifyAndRepair (self):
76                 PAR2_CMD = config_get_value ('commands', 'par2repair')
77
78                 # assemble the command
79                 # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
80                 command = "%s \"%s\" " % (PAR2_CMD, self.p2file)
81
82                 for f in self.all_p2files:
83                         if f != self.p2file:
84                                 command += "\"%s\" " % os.path.split (f)[1]
85
86                 # run the command
87                 ret = run_command (command, self.dir)
88
89                 # check the result
90                 if ret != 0:
91                         logging.critical ('PAR2 Check / Repair failed: %s' % self.p2file)
92                         return -ECHECK
93
94                 return SUCCESS
95
96         def find_deleteable_files (self):
97                 DELETE_REGEX = config_get_value ('regular expressions', 'delete_regex')
98                 dregex = re.compile (DELETE_REGEX, re.IGNORECASE)
99
100                 return [f for f in self.all_files if dregex.match (f)]
101
102         def delete_list_of_files (self, dir, files, interactive=False):
103                 # Delete a list of files
104
105                 assert os.path.isdir (dir)
106
107                 done = False
108                 valid_y = ['Y', 'YES']
109                 valid_n = ['N', 'NO', '']
110
111                 if interactive:
112                         while not done:
113                                 print 'Do you want to delete the following?:'
114                                 for f in files:
115                                         print f
116                                 s = raw_input ('Delete [y/N]: ').upper()
117
118                                 if s in valid_y + valid_n:
119                                         done = True
120
121                         if s in valid_n:
122                                 return SUCCESS
123
124                 for f in files:
125                         try:
126                                 os.remove (os.path.join (dir, f))
127                                 logging.debug ('Deleteing: %s' % os.path.join (dir, f))
128                         except:
129                                 logging.error ('Failed to delete: %s' % os.path.join (dir, f))
130                                 return -EDELETE
131
132                 return SUCCESS
133
134         def runDelete (self):
135                 deleteable_files = self.find_deleteable_files ()
136                 ret = self.delete_list_of_files (self.dir, deleteable_files, \
137                                 options_get_value ('interactive'))
138
139                 return ret
140
141         def runAll (self):
142
143                 # Repair Stage
144                 ret = self.runVerifyAndRepair ()
145
146                 if ret != SUCCESS:
147                         logging.critical ('Repair stage failed for: %s' % self.p2file)
148                         return -ECHECK
149
150                 self.update_matches ()
151
152                 # Extraction Stage
153                 ret = self.runExtract ()
154
155                 if ret != SUCCESS:
156                         logging.critical ('Extraction stage failed for: %s' % self.p2file)
157                         return -EEXTRACT
158
159                 self.update_matches ()
160
161                 # Deletion Stage
162                 ret = self.runDelete ()
163
164                 if ret != SUCCESS:
165                         logging.critical ('Deletion stage failed for: %s' % self.p2file)
166                         return -EDELETE
167
168                 logging.info ('Successfully completed: %s' % self.p2file)
169                 return SUCCESS
170
171         def safe_create_directory (self, dir):
172                 if dir == None:
173                         return SUCCESS
174
175                 if os.path.isdir (dir):
176                         return SUCCESS
177
178                 try:
179                         os.makedirs (dir)
180                         logging.info ('Created directory: %s' % dir)
181                 except OSError:
182                         logging.critical ('FAILED to create directory: %s' % dir)
183                         return -ECREATE
184
185                 return SUCCESS
186
187         def runExtract (self, todir=None):
188                 """Extract all heads of this set"""
189
190                 # Extract to the head's dir if we don't care where to extract
191                 if todir == None:
192                         todir = self.dir
193
194                 # Create the directory $todir if it doesn't exist
195                 ret = self.safe_create_directory (todir)
196
197                 if ret != SUCCESS:
198                         return -EEXTRACT
199
200                 # Call the extraction function on each head
201                 for h in self.find_extraction_heads ():
202                         full_head = full_abspath (os.path.join (self.dir, h))
203                         ret = self.extraction_function (full_head, todir)
204                         logging.debug ('Extraction Function returned: %d' % ret)
205
206                         # Check error code
207                         if ret != SUCCESS:
208                                 logging.critical ('Failed extracting: %s' % h)
209                                 return -EEXTRACT
210
211                 return SUCCESS
212
213         def find_extraction_heads (self):
214                 assert False # You MUST override this on a per-type basis
215
216         def extraction_function (self, file, todir):
217                 # NOTE: Please keep the prototype the same for all overridden functions.
218                 # Doing so will guarantee that your life is made much easier.
219                 #
220                 # Also note that the todir given will always be valid for the current directory
221                 # when the function is called.
222
223                 assert False # You MUST override this on a per-type basis
224