2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
4 from RarslaveCommon import *
11 # This is a fairly generic class which does all of the major things that a PAR2
12 # set will need to have done to be verified and extracted. For most "normal" types
13 # you won't need to override hardly anything.
15 # It is ok to override other functions if the need arises, just make sure that you
16 # understand why things are done the way that they are in the original versions.
18 # Assumptions made about each of the run*() functions:
19 # ==============================================================================
20 # The state of self.name_matched_files and self.prot_matched_files will be consistent
21 # with the real, in-filesystem state at the time that they are called.
22 # (This is why runAll() calls update_matches() all the time.)
25 # ==============================================================================
26 # find_extraction_heads ()
27 # extraction_function ()
33 # ==========================================================================
34 # dir -- The directory this set lives in
35 # p2file -- The starting PAR2 file
36 # basename -- The basename of the set, guessed from the PAR2 file
37 # all_p2files -- All PAR2 files of the set, guessed from the PAR2 file name only
38 # name_matched_files -- Files in this set, guessed by name only
39 # prot_matched_files -- Files in this set, guessed by parsing the PAR2 only
41 def __init__ (self, dir, p2file):
42 assert os.path.isdir (dir)
43 assert os.path.isfile (os.path.join (dir, p2file))
45 # The real "meat" of the class
48 self.basename = get_basename (p2file)
50 # Find files that match by name only
51 self.name_matched_files = find_name_matches (self.dir, self.basename)
53 # Find all par2 files for this set using name matches
54 self.all_p2files = find_par2_files (self.name_matched_files)
56 # Try to get the protected files for this set
57 self.prot_matched_files = parse_all_par2 (self.dir, self.p2file, self.all_p2files)
59 # Setup the all_files combined set (for convenience only)
60 self.all_files = no_duplicates (self.name_matched_files + self.prot_matched_files)
62 def __eq__ (self, rhs):
63 return (self.dir == rhs.dir) and (self.basename == rhs.basename) and \
64 list_eq (self.name_matched_files, rhs.name_matched_files) and \
65 list_eq (self.prot_matched_files, rhs.prot_matched_files)
67 def update_matches (self):
68 """Updates the contents of instance variables which are likely to change after
69 running an operation, usually one which will create new files."""
71 self.name_matched_files = find_name_matches (self.dir, self.basename)
72 self.all_files = no_duplicates (self.name_matched_files + self.prot_matched_files)
74 def runVerifyAndRepair (self):
75 PAR2_CMD = config_get_value ('commands', 'par2repair')
77 # assemble the command
78 # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
79 command = "%s \"%s\" " % (PAR2_CMD, self.p2file)
81 for f in self.all_p2files:
83 command += "\"%s\" " % os.path.split (f)[1]
86 ret = run_command (command, self.dir)
90 logging.critical ('PAR2 Check / Repair failed: %s' % self.p2file)
95 def find_deleteable_files (self):
96 DELETE_REGEX = config_get_value ('regular expressions', 'delete_regex')
97 dregex = re.compile (DELETE_REGEX, re.IGNORECASE)
99 return [f for f in self.all_files if dregex.match (f)]
101 def delete_list_of_files (self, dir, files, interactive=False):
102 # Delete a list of files
104 assert os.path.isdir (dir)
107 valid_y = ['Y', 'YES']
108 valid_n = ['N', 'NO', '']
112 print 'Do you want to delete the following?:'
115 s = raw_input ('Delete [y/N]: ').upper()
117 if s in valid_y + valid_n:
125 os.remove (os.path.join (dir, f))
126 logging.debug ('Deleteing: %s' % os.path.join (dir, f))
128 logging.error ('Failed to delete: %s' % os.path.join (dir, f))
133 def runDelete (self):
134 deleteable_files = self.find_deleteable_files ()
135 ret = self.delete_list_of_files (self.dir, deleteable_files, \
136 options_get_value ('interactive'))
143 ret = self.runVerifyAndRepair ()
146 logging.critical ('Repair stage failed for: %s' % self.p2file)
149 self.update_matches ()
152 ret = self.runExtract ()
155 logging.critical ('Extraction stage failed for: %s' % self.p2file)
158 self.update_matches ()
161 ret = self.runDelete ()
164 logging.critical ('Deletion stage failed for: %s' % self.p2file)
167 logging.info ('Successfully completed: %s' % self.p2file)
170 def safe_create_directory (self, dir):
174 if os.path.isdir (dir):
179 logging.info ('Created directory: %s' % dir)
181 logging.critical ('FAILED to create directory: %s' % dir)
186 def runExtract (self, todir=None):
187 """Extract all heads of this set"""
189 # Extract to the head's dir if we don't care where to extract
193 # Create the directory $todir if it doesn't exist
194 ret = self.safe_create_directory (todir)
199 # Call the extraction function on each head
200 for h in self.find_extraction_heads ():
201 full_head = full_abspath (os.path.join (self.dir, h))
202 ret = self.extraction_function (full_head, todir)
203 logging.debug ('Extraction Function returned: %d' % ret)
207 logging.critical ('Failed extracting: %s' % h)
212 def find_extraction_heads (self):
213 assert False # You MUST override this on a per-type basis
215 def extraction_function (self, file, todir):
216 # NOTE: Please keep the prototype the same for all overridden functions.
217 # Doing so will guarantee that your life is made much easier.
219 # Also note that the todir given will always be valid for the current directory
220 # when the function is called.
222 assert False # You MUST override this on a per-type basis