[RARSLAVE] Improve error handling
authorIra W. Snyder <devel@irasnyder.com>
Mon, 25 Dec 2006 09:13:51 +0000 (01:13 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Mon, 25 Dec 2006 09:13:51 +0000 (01:13 -0800)
Improve the error handling, mostly of the main operations that occur during
a program run (check, repair, join, extract, delete).

Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
rarslave.py

index 884bfe5..53f0b41 100644 (file)
@@ -6,6 +6,7 @@ import par2parser
 
 # Global Variables
 (TYPE_OLDRAR, TYPE_NEWRAR, TYPE_ZIP, TYPE_NOEXTRACT) = range (4)
 
 # Global Variables
 (TYPE_OLDRAR, TYPE_NEWRAR, TYPE_ZIP, TYPE_NOEXTRACT) = range (4)
+(ECHECK, EEXTRACT, EDELETE) = range(1,4)
 
 class RarslaveExtractor (object):
 
 
 class RarslaveExtractor (object):
 
@@ -32,8 +33,7 @@ class RarslaveExtractor (object):
                                os.makedirs (todir)
                        except OSError:
                                # TODO: LOGGER
                                os.makedirs (todir)
                        except OSError:
                                # TODO: LOGGER
-                               # Failed mkdir -p, clean up time ...
-                               pass # FIXME: temporary for syntax
+                               return -EEXTRACT
 
                # Extract all heads
                extraction_func = \
 
                # Extract all heads
                extraction_func = \
@@ -52,24 +52,33 @@ class RarslaveExtractor (object):
 
                RAR_CMD = 'unrar x -o+ -- '
 
 
                RAR_CMD = 'unrar x -o+ -- '
 
-               #file = full_abspath (file)
-               #todir = full_abspath (todir)
-
                cmd = '%s \"%s\"' % (RAR_CMD, file)
                ret = run_command (cmd, todir)
 
                cmd = '%s \"%s\"' % (RAR_CMD, file)
                ret = run_command (cmd, todir)
 
+               # Check error code
+               if ret != 0:
+                       return -EEXTRACT
+
        def __extract_zip (self, file, todir):
                ZIP_CMD = 'unzip \"%s\" -d \"%s\"'
 
                cmd = ZIP_CMD % (file, todir)
                ret = run_command (cmd)
 
        def __extract_zip (self, file, todir):
                ZIP_CMD = 'unzip \"%s\" -d \"%s\"'
 
                cmd = ZIP_CMD % (file, todir)
                ret = run_command (cmd)
 
+               # Check error code
+               if ret != 0:
+                       return -EEXTRACT
+
        def __extract_noextract (self, file, todir):
                # Just move this file to the $todir, since no extraction is needed
                # FIXME: NOTE: mv will fail by itself if you're moving to the same dir!
                cmd = 'mv \"%s\" \"%s\"' % (file, todir)
                ret = run_command (cmd)
 
        def __extract_noextract (self, file, todir):
                # Just move this file to the $todir, since no extraction is needed
                # FIXME: NOTE: mv will fail by itself if you're moving to the same dir!
                cmd = 'mv \"%s\" \"%s\"' % (file, todir)
                ret = run_command (cmd)
 
+               # Check error code
+               if ret != 0:
+                       return -EEXTRACT
+
 
 
 class RarslaveRepairer (object):
 
 
 class RarslaveRepairer (object):
@@ -110,6 +119,12 @@ class RarslaveRepairer (object):
                # run the command
                ret = run_command (command, self.dir)
 
                # run the command
                ret = run_command (command, self.dir)
 
+               # check the result
+               if ret != 0:
+                       # TODO: logger
+                       print 'error during checkAndRepair()'
+                       return -ECHECK
+
 def run_command (cmd, indir=None):
        # Runs the specified command-line in the directory given (or, in the current directory
        # if none is given). It returns the status code given by the application.
 def run_command (cmd, indir=None):
        # Runs the specified command-line in the directory given (or, in the current directory
        # if none is given). It returns the status code given by the application.
@@ -122,6 +137,8 @@ def run_command (cmd, indir=None):
 
        # FIXME: re-enable this after testing
        print 'RUNNING (%s): %s' % (indir, cmd)
 
        # FIXME: re-enable this after testing
        print 'RUNNING (%s): %s' % (indir, cmd)
+       return 0
+
        # return os.system (cmd)
 
 
        # return os.system (cmd)
 
 
@@ -332,15 +349,25 @@ class PAR2Set (object):
                repairer = RarslaveRepairer (self.dir, par2head, join)
                ret = repairer.checkAndRepair () # FIXME: Check return value
 
                repairer = RarslaveRepairer (self.dir, par2head, join)
                ret = repairer.checkAndRepair () # FIXME: Check return value
 
+               if ret: # FAILURE
+                       return -ECHECK
+
                # Extraction Stage
                extractor = find_extraction_heads (self.dir, self.likely_files)
                ret = extractor.extract ('extract_dir') # FIXME: Get it from the config
 
                # Extraction Stage
                extractor = find_extraction_heads (self.dir, self.likely_files)
                ret = extractor.extract ('extract_dir') # FIXME: Get it from the config
 
+               if ret: # FAILURE
+                       return -EEXTRACT
+
                # Deletion Stage
                # Deletion Stage
-               # printlist ( find_deleteable_files (self.likely_files) )
                deleteable_files = find_deleteable_files (self.likely_files)
                ret = delete_list (deleteable_files)
 
                deleteable_files = find_deleteable_files (self.likely_files)
                ret = delete_list (deleteable_files)
 
+               if ret: # FAILURE
+                       return -EDELETE
+
+               return 0
+
 def delete_list (files, interactive=False):
        # Delete a list of files
        # TODO: Add the ability to confirm deletion, like in the original rarslave
 def delete_list (files, interactive=False):
        # Delete a list of files
        # TODO: Add the ability to confirm deletion, like in the original rarslave
@@ -356,6 +383,8 @@ def delete_list (files, interactive=False):
                # os.remove (f)
                print 'rm', f
 
                # os.remove (f)
                print 'rm', f
 
+       return 0
+
 
 def generate_all_parsets (dir):
        # Generate all parsets in the given directory.
 
 def generate_all_parsets (dir):
        # Generate all parsets in the given directory.