Convert all return values to use rsutil package
[rarslave2.git] / PAR2Set / Base.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
4 import rsutil.common
5
6 import re
7 import os
8 import logging
9
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.
13 #
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.
16 #
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.)
22 #
23 # Required overrides:
24 # ==============================================================================
25 # find_extraction_heads ()
26 # extraction_function ()
27 #
28
29 class Base (object):
30
31         # Instance Variables
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
39
40         def __init__ (self, dir, p2file):
41                 assert os.path.isdir (dir)
42                 assert os.path.isfile (os.path.join (dir, p2file))
43
44                 # The real "meat" of the class
45                 self.dir = dir
46                 self.p2file = p2file
47                 self.basename = rsutil.common.get_basename (p2file)
48
49                 # Find files that match by name only
50                 self.name_matched_files = rsutil.common.find_name_matches (self.dir, self.basename)
51
52                 # Find all par2 files for this set using name matches
53                 self.all_p2files = rsutil.common.find_par2_files (self.name_matched_files)
54
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)
57
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)
60
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)
65
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."""
69
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)
72
73         def runVerifyAndRepair (self):
74                 PAR2_CMD = rsutil.common.config_get_value ('commands', 'par2repair')
75
76                 # assemble the command
77                 # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
78                 command = "%s \"%s\" " % (PAR2_CMD, self.p2file)
79
80                 for f in self.all_p2files:
81                         if f != self.p2file:
82                                 command += "\"%s\" " % os.path.split (f)[1]
83
84                 # run the command
85                 ret = rsutil.common.run_command (command, self.dir)
86
87                 # check the result
88                 if ret != 0:
89                         logging.critical ('PAR2 Check / Repair failed: %s' % self.p2file)
90                         return -rsutil.common.ECHECK
91
92                 return rsutil.common.SUCCESS
93
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)
97
98                 return [f for f in self.all_files if dregex.match (f)]
99
100         def delete_list_of_files (self, dir, files, interactive=False):
101                 # Delete a list of files
102
103                 assert os.path.isdir (dir)
104
105                 done = False
106                 valid_y = ['Y', 'YES']
107                 valid_n = ['N', 'NO', '']
108
109                 if interactive:
110                         while not done:
111                                 print 'Do you want to delete the following?:'
112                                 for f in files:
113                                         print f
114                                 s = raw_input ('Delete [y/N]: ').upper()
115
116                                 if s in valid_y + valid_n:
117                                         done = True
118
119                         if s in valid_n:
120                                 return rsutil.common.SUCCESS
121
122                 for f in files:
123                         try:
124                                 os.remove (os.path.join (dir, f))
125                                 logging.debug ('Deleteing: %s' % os.path.join (dir, f))
126                         except:
127                                 logging.error ('Failed to delete: %s' % os.path.join (dir, f))
128                                 return -rsutil.common.EDELETE
129
130                 return rsutil.common.SUCCESS
131
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'))
136
137                 return ret
138
139         def runAll (self):
140
141                 # Repair Stage
142                 ret = self.runVerifyAndRepair ()
143
144                 if ret != rsutil.common.SUCCESS:
145                         logging.critical ('Repair stage failed for: %s' % self.p2file)
146                         return -rsutil.common.ECHECK
147
148                 self.update_matches ()
149
150                 # Extraction Stage
151                 ret = self.runExtract ()
152
153                 if ret != rsutil.common.SUCCESS:
154                         logging.critical ('Extraction stage failed for: %s' % self.p2file)
155                         return -rsutil.common.EEXTRACT
156
157                 self.update_matches ()
158
159                 # Deletion Stage
160                 ret = self.runDelete ()
161
162                 if ret != rsutil.common.SUCCESS:
163                         logging.critical ('Deletion stage failed for: %s' % self.p2file)
164                         return -rsutil.common.EDELETE
165
166                 logging.info ('Successfully completed: %s' % self.p2file)
167                 return rsutil.common.SUCCESS
168
169         def safe_create_directory (self, dir):
170                 if dir == None:
171                         return rsutil.common.SUCCESS
172
173                 if os.path.isdir (dir):
174                         return rsutil.common.SUCCESS
175
176                 try:
177                         os.makedirs (dir)
178                         logging.info ('Created directory: %s' % dir)
179                 except OSError:
180                         logging.critical ('FAILED to create directory: %s' % dir)
181                         return -rsutil.common.ECREATE
182
183                 return rsutil.common.SUCCESS
184
185         def runExtract (self, todir=None):
186                 """Extract all heads of this set"""
187
188                 # Extract to the head's dir if we don't care where to extract
189                 if todir == None:
190                         todir = self.dir
191
192                 # Create the directory $todir if it doesn't exist
193                 ret = self.safe_create_directory (todir)
194
195                 if ret != rsutil.common.SUCCESS:
196                         return -rsutil.common.EEXTRACT
197
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)
203
204                         # Check error code
205                         if ret != rsutil.common.SUCCESS:
206                                 logging.critical ('Failed extracting: %s' % h)
207                                 return -rsutil.common.EEXTRACT
208
209                 return rsutil.common.SUCCESS
210
211         def find_extraction_heads (self):
212                 assert False # You MUST override this on a per-type basis
213
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.
217                 #
218                 # Also note that the todir given will always be valid for the current directory
219                 # when the function is called.
220
221                 assert False # You MUST override this on a per-type basis
222