Use exceptions for error handling
[rarslave2.git] / PAR2Set / Base.py
index 533ff4f..a5d3dcb 100644 (file)
@@ -109,25 +109,7 @@ class Base (object):
                """Verify and Repair a PAR2Set. This is done using the par2repair command by
                   default"""
 
-               PAR2_CMD = rsutil.common.config_get_value ('commands', 'par2repair')
-
-               # assemble the command
-               # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
-               command = "%s \"%s\" " % (PAR2_CMD, self.p2file)
-
-               for f in self.all_p2files:
-                       if f != self.p2file:
-                               command += "\"%s\" " % os.path.split (f)[1]
-
-               # run the command
-               ret = rsutil.common.run_command (command, self.dir)
-
-               # check the result
-               if ret != 0:
-                       logging.critical ('PAR2 Check / Repair failed: %s' % self.p2file)
-                       return -rsutil.common.ECHECK
-
-               return rsutil.common.SUCCESS
+               rsutil.common.run_command(['par2repair'] + self.all_p2files, self.dir)
 
        def find_deleteable_files (self):
                """Find all files which are deletable by using the regular expression from the
@@ -147,32 +129,38 @@ class Base (object):
 
                assert os.path.isdir (dir)
 
-               done = False
-               valid_y = ['Y', 'YES']
-               valid_n = ['N', 'NO', '']
-
+               # If we are interactive, decide yes or no
                if interactive:
-                       while not done:
+                       while True:
                                print 'Do you want to delete the following?:'
                                for f in files:
                                        print f
-                               s = raw_input ('Delete [y/N]: ').upper()
 
-                               if s in valid_y + valid_n:
-                                       done = True
+                               s = raw_input('Delete [y/N]: ').upper()
+
+                               # If we got a valid no answer, leave now
+                               if s in ['N', 'NO', '']:
+                                       return
+
+                               # If we got a valid yes answer, delete them
+                               if s in ['Y', 'YES']:
+                                       break
 
-                       if s in valid_n:
-                               return rsutil.common.SUCCESS
+                               # Not a good answer, ask again
+                               print 'Invalid response'
 
+               # We got a yes answer (or are non-interactive), delete the files
                for f in files:
-                       try:
-                               os.remove (os.path.join (dir, f))
-                               logging.debug ('Deleteing: %s' % os.path.join (dir, f))
-                       except:
-                               logging.error ('Failed to delete: %s' % os.path.join (dir, f))
-                               return -rsutil.common.EDELETE
 
-               return rsutil.common.SUCCESS
+                       # Get the full filename
+                       fullname = os.path.join(dir, f)
+
+                       # Delete the file
+                       try:
+                               os.remove(fullname)
+                               logging.debug('Deleting: %s' % fullname)
+                       except OSError:
+                               logging.error('Failed to delete: %s' % fullname)
 
        def runDelete (self):
                """Run the delete operation and return the result"""
@@ -187,78 +175,39 @@ class Base (object):
                """Run all of the major sections in the class: repair, extraction, and deletion."""
 
                # Repair Stage
-               ret = self.runVerifyAndRepair ()
-
-               if ret != rsutil.common.SUCCESS:
-                       logging.critical ('Repair stage failed for: %s' % self.p2file)
-                       return -rsutil.common.ECHECK
+               try:
+                       self.runVerifyAndRepair ()
+               except (RuntimeError, OSError):
+                       logging.critical('Repair stage failed for: %s' % self.p2file)
+                       raise
 
-               self.update_matches ()
+               self.update_matches()
 
                # Extraction Stage
-               ret = self.runExtract ()
-
-               if ret != rsutil.common.SUCCESS:
-                       logging.critical ('Extraction stage failed for: %s' % self.p2file)
-                       return -rsutil.common.EEXTRACT
+               try:
+                       self.runExtract ()
+               except (RuntimeError, OSError):
+                       logging.critical('Extraction stage failed for: %s' % self.p2file)
+                       raise
 
                self.update_matches ()
 
                # Deletion Stage
-               ret = self.runDelete ()
-
-               if ret != rsutil.common.SUCCESS:
-                       logging.critical ('Deletion stage failed for: %s' % self.p2file)
-                       return -rsutil.common.EDELETE
-
-               logging.info ('Successfully completed: %s' % self.p2file)
-               return rsutil.common.SUCCESS
-
-       def safe_create_directory (self, dir):
-               """Safely create a directory, logging the result.
-
-                  dir -- the directory to create (None is ignored)"""
-
-               if dir == None:
-                       return rsutil.common.SUCCESS
-
-               if os.path.isdir (dir):
-                       return rsutil.common.SUCCESS
-
                try:
-                       os.makedirs (dir)
-                       logging.info ('Created directory: %s' % dir)
-               except OSError:
-                       logging.critical ('FAILED to create directory: %s' % dir)
-                       return -rsutil.common.ECREATE
+                       self.runDelete ()
+               except (RuntimeError, OSError):
+                       logging.critical('Deletion stage failed for: %s' % self.p2file)
+                       raise
 
-               return rsutil.common.SUCCESS
+               logging.info ('Successfully completed: %s' % self.p2file)
 
-       def runExtract (self, todir=None):
+       def runExtract (self):
                """Extract all heads of this set and return the result"""
 
-               # Extract to the head's dir if we don't care where to extract
-               if todir == None:
-                       todir = self.dir
-
-               # Create the directory $todir if it doesn't exist
-               ret = self.safe_create_directory (todir)
-
-               if ret != rsutil.common.SUCCESS:
-                       return -rsutil.common.EEXTRACT
-
                # Call the extraction function on each head
                for h in self.find_extraction_heads ():
                        full_head = rsutil.common.full_abspath (os.path.join (self.dir, h))
-                       ret = self.extraction_function (full_head, todir)
-                       logging.debug ('Extraction Function returned: %d' % ret)
-
-                       # Check error code
-                       if ret != rsutil.common.SUCCESS:
-                               logging.critical ('Failed extracting: %s' % h)
-                               return -rsutil.common.EEXTRACT
-
-               return rsutil.common.SUCCESS
+                       self.extraction_function (full_head, self.dir)
 
        def find_extraction_heads (self):
                """Find all extraction heads associated with this set. This must be