2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
10 # This is a fairly generic class which does all of the major things that a PAR2
11 # set will need to have done to be verified and extracted. For most "normal" types
12 # you won't need to override hardly anything.
14 # It is ok to override other functions if the need arises, just make sure that you
15 # understand why things are done the way that they are in the original versions.
17 # Assumptions made about each of the run*() functions:
18 # ==============================================================================
19 # The state of self.name_matched_files and self.prot_matched_files will be consistent
20 # with the real, in-filesystem state at the time that they are called.
21 # (This is why runAll() calls update_matches() all the time.)
24 # ==============================================================================
25 # find_extraction_heads ()
26 # extraction_function ()
32 # ==========================================================================
33 # dir -- The directory this set lives in
34 # p2file -- The starting PAR2 file
35 # basename -- The basename of the set, guessed from the PAR2 file
36 # all_p2files -- All PAR2 files of the set, guessed from the PAR2 file name only
37 # name_matched_files -- Files in this set, guessed by name only
38 # prot_matched_files -- Files in this set, guessed by parsing the PAR2 only
40 def __init__ (self, dir, p2file):
41 assert os.path.isdir (dir)
42 assert os.path.isfile (os.path.join (dir, p2file))
44 # The real "meat" of the class
47 self.basename = rsutil.common.get_basename (p2file)
49 # Find files that match by name only
50 self.name_matched_files = rsutil.common.find_name_matches (self.dir, self.basename)
52 # Find all par2 files for this set using name matches
53 self.all_p2files = rsutil.common.find_par2_files (self.name_matched_files)
55 # Try to get the protected files for this set
56 self.prot_matched_files = rsutil.common.parse_all_par2 (self.dir, self.p2file, self.all_p2files)
58 # Setup the all_files combined set (for convenience only)
59 self.all_files = rsutil.common.no_duplicates (self.name_matched_files + self.prot_matched_files)
61 def __eq__ (self, rhs):
62 return (self.dir == rhs.dir) and (self.basename == rhs.basename) and \
63 rsutil.common.list_eq (self.name_matched_files, rhs.name_matched_files) and \
64 rsutil.common.list_eq (self.prot_matched_files, rhs.prot_matched_files)
66 def update_matches (self):
67 """Updates the contents of instance variables which are likely to change after
68 running an operation, usually one which will create new files."""
70 self.name_matched_files = rsutil.common.find_name_matches (self.dir, self.basename)
71 self.all_files = rsutil.common.no_duplicates (self.name_matched_files + self.prot_matched_files)
73 def runVerifyAndRepair (self):
74 PAR2_CMD = rsutil.common.config_get_value ('commands', 'par2repair')
76 # assemble the command
77 # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
78 command = "%s \"%s\" " % (PAR2_CMD, self.p2file)
80 for f in self.all_p2files:
82 command += "\"%s\" " % os.path.split (f)[1]
85 ret = rsutil.common.run_command (command, self.dir)
89 logging.critical ('PAR2 Check / Repair failed: %s' % self.p2file)
90 return -rsutil.common.ECHECK
92 return rsutil.common.SUCCESS
94 def find_deleteable_files (self):
95 DELETE_REGEX = rsutil.common.config_get_value ('regular expressions', 'delete_regex')
96 dregex = re.compile (DELETE_REGEX, re.IGNORECASE)
98 return [f for f in self.all_files if dregex.match (f)]
100 def delete_list_of_files (self, dir, files, interactive=False):
101 # Delete a list of files
103 assert os.path.isdir (dir)
106 valid_y = ['Y', 'YES']
107 valid_n = ['N', 'NO', '']
111 print 'Do you want to delete the following?:'
114 s = raw_input ('Delete [y/N]: ').upper()
116 if s in valid_y + valid_n:
120 return rsutil.common.SUCCESS
124 os.remove (os.path.join (dir, f))
125 logging.debug ('Deleteing: %s' % os.path.join (dir, f))
127 logging.error ('Failed to delete: %s' % os.path.join (dir, f))
128 return -rsutil.common.EDELETE
130 return rsutil.common.SUCCESS
132 def runDelete (self):
133 deleteable_files = self.find_deleteable_files ()
134 ret = self.delete_list_of_files (self.dir, deleteable_files, \
135 rsutil.common.options_get_value ('interactive'))
142 ret = self.runVerifyAndRepair ()
144 if ret != rsutil.common.SUCCESS:
145 logging.critical ('Repair stage failed for: %s' % self.p2file)
146 return -rsutil.common.ECHECK
148 self.update_matches ()
151 ret = self.runExtract ()
153 if ret != rsutil.common.SUCCESS:
154 logging.critical ('Extraction stage failed for: %s' % self.p2file)
155 return -rsutil.common.EEXTRACT
157 self.update_matches ()
160 ret = self.runDelete ()
162 if ret != rsutil.common.SUCCESS:
163 logging.critical ('Deletion stage failed for: %s' % self.p2file)
164 return -rsutil.common.EDELETE
166 logging.info ('Successfully completed: %s' % self.p2file)
167 return rsutil.common.SUCCESS
169 def safe_create_directory (self, dir):
171 return rsutil.common.SUCCESS
173 if os.path.isdir (dir):
174 return rsutil.common.SUCCESS
178 logging.info ('Created directory: %s' % dir)
180 logging.critical ('FAILED to create directory: %s' % dir)
181 return -rsutil.common.ECREATE
183 return rsutil.common.SUCCESS
185 def runExtract (self, todir=None):
186 """Extract all heads of this set"""
188 # Extract to the head's dir if we don't care where to extract
192 # Create the directory $todir if it doesn't exist
193 ret = self.safe_create_directory (todir)
195 if ret != rsutil.common.SUCCESS:
196 return -rsutil.common.EEXTRACT
198 # Call the extraction function on each head
199 for h in self.find_extraction_heads ():
200 full_head = rsutil.common.full_abspath (os.path.join (self.dir, h))
201 ret = self.extraction_function (full_head, todir)
202 logging.debug ('Extraction Function returned: %d' % ret)
205 if ret != rsutil.common.SUCCESS:
206 logging.critical ('Failed extracting: %s' % h)
207 return -rsutil.common.EEXTRACT
209 return rsutil.common.SUCCESS
211 def find_extraction_heads (self):
212 assert False # You MUST override this on a per-type basis
214 def extraction_function (self, file, todir):
215 # NOTE: Please keep the prototype the same for all overridden functions.
216 # Doing so will guarantee that your life is made much easier.
218 # Also note that the todir given will always be valid for the current directory
219 # when the function is called.
221 assert False # You MUST override this on a per-type basis