2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
4 from RarslaveCommon import *
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.
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.
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.)
26 # ==============================================================================
27 # find_extraction_heads ()
28 # extraction_function ()
31 class PAR2Set (object):
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
42 def __init__ (self, dir, p2file):
43 assert os.path.isdir (dir)
44 assert os.path.isfile (os.path.join (dir, p2file))
46 # The real "meat" of the class
49 self.basename = get_basename (p2file)
51 # Find files that match by name only
52 self.name_matched_files = find_name_matches (self.dir, self.basename)
54 # Find all par2 files for this set using name matches
55 self.all_p2files = find_par2_files (self.name_matched_files)
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)
60 # Setup the all_files combined set (for convenience only)
61 self.all_files = no_duplicates (self.name_matched_files + self.prot_matched_files)
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)
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."""
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)
75 def runVerifyAndRepair (self):
76 PAR2_CMD = config_get_value ('commands', 'par2repair')
78 # assemble the command
79 # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
80 command = "%s \"%s\" " % (PAR2_CMD, self.p2file)
82 for f in self.all_p2files:
84 command += "\"%s\" " % os.path.split (f)[1]
87 ret = run_command (command, self.dir)
91 logging.critical ('PAR2 Check / Repair failed: %s' % self.p2file)
96 def find_deleteable_files (self):
97 DELETE_REGEX = config_get_value ('regular expressions', 'delete_regex')
98 dregex = re.compile (DELETE_REGEX, re.IGNORECASE)
100 return [f for f in self.all_files if dregex.match (f)]
102 def delete_list_of_files (self, dir, files, interactive=False):
103 # Delete a list of files
105 assert os.path.isdir (dir)
108 valid_y = ['Y', 'YES']
109 valid_n = ['N', 'NO', '']
113 print 'Do you want to delete the following?:'
116 s = raw_input ('Delete [y/N]: ').upper()
118 if s in valid_y + valid_n:
126 os.remove (os.path.join (dir, f))
127 logging.debug ('Deleteing: %s' % os.path.join (dir, f))
129 logging.error ('Failed to delete: %s' % os.path.join (dir, f))
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'))
144 ret = self.runVerifyAndRepair ()
147 logging.critical ('Repair stage failed for: %s' % self.p2file)
150 self.update_matches ()
153 ret = self.runExtract ()
156 logging.critical ('Extraction stage failed for: %s' % self.p2file)
159 self.update_matches ()
162 ret = self.runDelete ()
165 logging.critical ('Deletion stage failed for: %s' % self.p2file)
168 logging.info ('Successfully completed: %s' % self.p2file)
171 def safe_create_directory (self, dir):
175 if os.path.isdir (dir):
180 logging.info ('Created directory: %s' % dir)
182 logging.critical ('FAILED to create directory: %s' % dir)
187 def runExtract (self, todir=None):
188 """Extract all heads of this set"""
190 # Extract to the head's dir if we don't care where to extract
194 # Create the directory $todir if it doesn't exist
195 ret = self.safe_create_directory (todir)
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)
208 logging.critical ('Failed extracting: %s' % h)
213 def find_extraction_heads (self):
214 assert False # You MUST override this on a per-type basis
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.
220 # Also note that the todir given will always be valid for the current directory
221 # when the function is called.
223 assert False # You MUST override this on a per-type basis