[RARSLAVE] Add logging messages
authorIra W. Snyder <devel@irasnyder.com>
Tue, 26 Dec 2006 05:05:22 +0000 (21:05 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Wed, 27 Dec 2006 00:06:59 +0000 (16:06 -0800)
Adds a lot of logging messages. Also audited (and added) many error return
values, as well as missing success paths.

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

index 3f251f3..224d105 100644 (file)
@@ -4,11 +4,13 @@
 import re, os, sys
 import par2parser
 import RarslaveConfig
+from RarslaveLogger import RarslaveLogger
 
 # Global Variables
 (TYPE_OLDRAR, TYPE_NEWRAR, TYPE_ZIP, TYPE_NOEXTRACT) = range (4)
-(ECHECK, EEXTRACT, EDELETE) = range(1,4)
+(SUCCESS, ECHECK, EEXTRACT, EDELETE) = range(4)
 config = RarslaveConfig.RarslaveConfig()
+logger = RarslaveLogger ()
 
 class RarslaveExtractor (object):
 
@@ -30,11 +32,11 @@ class RarslaveExtractor (object):
 
                # Create the directory $todir if it doesn't exist
                if todir != None and not os.path.isdir (todir):
-                       # TODO: LOGGER
+                       logger.addMessage ('Creating directory: %s' % todir, False)
                        try:
                                os.makedirs (todir)
                        except OSError:
-                               # TODO: LOGGER
+                               logger.addMessage ('FAILED to create directory: %s' % todir)
                                return -EEXTRACT
 
                # Extract all heads
@@ -48,9 +50,16 @@ class RarslaveExtractor (object):
                for h in self.heads:
                        if todir == None:
                                # Run in the head's directory
-                               extraction_func (h, os.path.dirname (h))
+                               ret = extraction_func (h, os.path.dirname (h))
                        else:
-                               extraction_func (h, todir)
+                               ret = extraction_func (h, todir)
+
+                       # Check error code
+                       if ret != SUCCESS:
+                               logger.addMessage ('Failed extracting: %s' % h)
+                               return -EEXTRACT
+
+               return SUCCESS
 
        def __extract_rar (self, file, todir):
                assert os.path.isfile (file)
@@ -65,6 +74,8 @@ class RarslaveExtractor (object):
                if ret != 0:
                        return -EEXTRACT
 
+               return SUCCESS
+
        def __extract_zip (self, file, todir):
                ZIP_CMD = config.get_value ('commands', 'unzip')
 
@@ -75,6 +86,8 @@ class RarslaveExtractor (object):
                if ret != 0:
                        return -EEXTRACT
 
+               return SUCCESS
+
        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!
@@ -87,6 +100,8 @@ class RarslaveExtractor (object):
                if ret != 0:
                        return -EEXTRACT
 
+               return SUCCESS
+
 
 
 class RarslaveRepairer (object):
@@ -117,22 +132,23 @@ class RarslaveRepairer (object):
 
                for f in par2_files:
                        if f != self.file:
-                               command += "\"%s\" " % get_filename(f)
+                               command += "\"%s\" " % os.path.split (f)[1]
 
                if self.join:
                        for f in all_files:
                                if f not in par2_files:
-                                       command += "\"%s\" " % get_filename(f)
+                                       command += "\"%s\" " % os.path.split (f)[1]
 
                # run the command
                ret = run_command (command, self.dir)
 
                # check the result
                if ret != 0:
-                       # TODO: logger
-                       print 'error during checkAndRepair()'
+                       logger.addMessage ('PAR2 Check / Repair failed: %s' % self.file)
                        return -ECHECK
 
+               return SUCCESS
+
 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.
@@ -145,7 +161,7 @@ def run_command (cmd, indir=None):
 
        # FIXME: re-enable this after testing
        print 'RUNNING (%s): %s' % (indir, cmd)
-       return 0
+       return SUCCESS
 
        # return os.system (cmd)
 
@@ -153,13 +169,6 @@ def run_command (cmd, indir=None):
 def full_abspath (p):
        return os.path.abspath (os.path.expanduser (p))
 
-def get_filename (f):
-       # TODO: I don't think that we should enforce this...
-       # TODO: ... because I think we should be able to get the filename, regardless
-       # TODO: of whether this is a legit filename RIGHT NOW or not.
-       # assert os.path.isfile (f)
-       return os.path.split (f)[1]
-
 def get_basename (name):
        """Strips most kinds of endings from a filename"""
 
@@ -268,7 +277,7 @@ def find_extraction_heads (dir, files):
                                prot_files = par2parser.get_protected_files (dir, f)
                                done = True
                        except: #FIXME: add the actual exceptions
-                               print 'ERROR PARSING P2FILE ...', f
+                               logger.addMessage ('Error parsing PAR2 file: %s', f)
                                continue
 
                        if done:
@@ -278,10 +287,14 @@ def find_extraction_heads (dir, files):
                        for f in prot_files:
                                extractor.addHead (dir, f)
                else:
-                       print 'BADNESS'
+                       logger.addMessage ('Error parsing all PAR2 files in this set ...', True)
 
        # Make sure we found the type
-       assert extractor != None
+       if extractor == None:
+               logger.addMessage ('Not able to find an extractor for this type of set: %s' % p2files[0])
+
+               # No-heads here, but it's better than failing completely
+               extractor = RarslaveExtractor (TYPE_NOEXTRACT)
 
        return extractor
 
@@ -290,16 +303,22 @@ def is_oldrar (files):
                if has_extension (f, '.r00'):
                        return True
 
+       return False
+
 def is_newrar (files):
        for f in files:
                if has_extension (f, '.part01.rar'):
                        return True
 
+       return False
+
 def is_zip (files):
        for f in files:
                if has_extension (f, '.zip'):
                        return True
 
+       return False
+
 def is_noextract (files):
        # Type that needs no extraction.
        # TODO: Add others ???
@@ -307,6 +326,8 @@ def is_noextract (files):
                if has_extension (f, '.001'):
                        return True
 
+       return False
+
 def find_deleteable_files (files):
        # Deleteable types regex should come from the config
        dfiles = []
@@ -357,9 +378,10 @@ class PAR2Set (object):
 
                # Repair Stage
                repairer = RarslaveRepairer (self.dir, par2head, join)
-               ret = repairer.checkAndRepair () # FIXME: Check return value
+               ret = repairer.checkAndRepair ()
 
-               if ret: # FAILURE
+               if ret != SUCCESS:
+                       logger.addMessage ('Repair stage failed for: %s' % par2head)
                        return -ECHECK
 
                # Extraction Stage
@@ -367,7 +389,8 @@ class PAR2Set (object):
                extractor = find_extraction_heads (self.dir, self.likely_files)
                ret = extractor.extract (EXTRACT_DIR)
 
-               if ret: # FAILURE
+               if ret != SUCCESS:
+                       logger.addMessage ('Extraction stage failed for: %s' % par2head)
                        return -EEXTRACT
 
                # Deletion Stage
@@ -375,27 +398,37 @@ class PAR2Set (object):
                deleteable_files = find_deleteable_files (self.likely_files)
                ret = delete_list (deleteable_files, DELETE_INTERACTIVE)
 
-               if ret: # FAILURE
+               if ret != SUCCESS:
+                       logger.addMessage ('Deletion stage failed for: %s' % par2head)
                        return -EDELETE
 
-               return 0
+               logger.addMessage ('Successfully completed: %s' % par2head, True)
+               return SUCCESS
 
 def delete_list (files, interactive=False):
        # Delete a list of files
-       # TODO: Add the ability to confirm deletion, like in the original rarslave
+
+       done = False
+       valid_y = ['Y', 'YES']
+       valid_n = ['N', 'NO']
 
        if interactive:
-               # TODO: prompt here
-               # prompt -> OK_TO_DELETE -> do nothing, fall through
-               # prompt -> NOT_OK -> return immediately
-               pass
+               while not done:
+                       print 'Do you want to delete the following?:'
+                       s = raw_input ('Delete [y/N]: ').upper()
+
+                       if s in valid_y + valid_n:
+                               done = True
+
+               if s in valid_n:
+                       return SUCCESS
 
        for f in files:
                # FIXME: re-enable this in production
                # os.remove (f)
-               print 'rm', f
+               print 'rm \"%s\"' % f
 
-       return 0
+       return SUCCESS
 
 
 def generate_all_parsets (dir):
@@ -417,10 +450,12 @@ def main ():
        TOPDIR = os.path.abspath ('test_material')
 
        for (dir, subdirs, files) in os.walk (TOPDIR):
-               print 'DEBUG: IN DIRECTORY:', dir
                parsets = generate_all_parsets (dir)
                for p in parsets:
                        p.run_all ()
 
+       print '\nRARSLAVE STATUS\n'
+       logger.printAllMessages ()
+
 if __name__ == '__main__':
        main ()