[RARSLAVE] Add debug logging messages
[rarslave2.git] / rarslave.py
index 2e9861a..285974d 100644 (file)
@@ -3,10 +3,14 @@
 
 import re, os, sys
 import par2parser
+import RarslaveConfig
+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.RarslaveLogger ()
 
 class RarslaveExtractor (object):
 
@@ -21,18 +25,20 @@ class RarslaveExtractor (object):
                # FIXME: probably CAN add this back, since we should be running this AFTER repair.
                #assert os.path.isfile (os.path.join (dir, head))
 
-               self.heads.append (os.path.join (dir, head))
+               full_head = os.path.join (dir, head)
+               logger.addMessage ('Adding extraction head: %s' % full_head, RarslaveLogger.MessageType.Debug)
+               self.heads.append (full_head)
 
        def extract (self, todir=None):
                # Extract all heads of this set
 
                # 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, RarslaveLogger.MessageType.Verbose)
                        try:
                                os.makedirs (todir)
                        except OSError:
-                               # TODO: LOGGER
+                               logger.addMessage ('FAILED to create directory: %s' % todir, RarslaveLogger.MessageType.Fatal)
                                return -EEXTRACT
 
                # Extract all heads
@@ -46,15 +52,24 @@ 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)
+
+                       logger.addMessage ('Extraction Function returned: %d' % ret, RarslaveLogger.MessageType.Debug)
+
+                       # Check error code
+                       if ret != SUCCESS:
+                               logger.addMessage ('Failed extracting: %s' % h, RarslaveLogger.MessageType.Fatal)
+                               return -EEXTRACT
+
+               return SUCCESS
 
        def __extract_rar (self, file, todir):
                assert os.path.isfile (file)
                assert os.path.isdir (todir)
 
-               RAR_CMD = 'unrar x -o+ -- '
+               RAR_CMD = config.get_value ('commands', 'unrar')
 
                cmd = '%s \"%s\"' % (RAR_CMD, file)
                ret = run_command (cmd, todir)
@@ -63,8 +78,10 @@ class RarslaveExtractor (object):
                if ret != 0:
                        return -EEXTRACT
 
+               return SUCCESS
+
        def __extract_zip (self, file, todir):
-               ZIP_CMD = 'unzip \"%s\" -d \"%s\"'
+               ZIP_CMD = config.get_value ('commands', 'unzip')
 
                cmd = ZIP_CMD % (file, todir)
                ret = run_command (cmd)
@@ -73,16 +90,22 @@ 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!
-               cmd = 'mv \"%s\" \"%s\"' % (file, todir)
+               NOEXTRACT_CMD = config.get_value ('commands', 'noextract')
+
+               cmd = NOEXTRACT_CMD % (file, todir)
                ret = run_command (cmd)
 
                # Check error code
                if ret != 0:
                        return -EEXTRACT
 
+               return SUCCESS
+
 
 
 class RarslaveRepairer (object):
@@ -100,7 +123,7 @@ class RarslaveRepairer (object):
        def checkAndRepair (self):
                # Form the command:
                # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
-               PAR2_CMD = 'par2repair -- '
+               PAR2_CMD = config.get_value ('commands', 'par2repair')
 
                # Get set up
                basename = get_basename (self.file)
@@ -113,22 +136,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, RarslaveLogger.MessageType.Fatal)
                        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.
@@ -141,7 +165,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)
 
@@ -149,17 +173,10 @@ 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"""
 
-       regex = '^(.+)\.(par2|vol\d+\+\d+|\d\d\d|part\d+|rar|zip|avi|mp4|mkv|ogm)$'
+       regex = config.get_value ('regular expressions', 'basename_regex')
        r = re.compile (regex, re.IGNORECASE)
        done = False
 
@@ -189,7 +206,8 @@ def find_likely_files (name, dir):
 def find_par2_files (files):
        """Find all par2 files in the list $files"""
 
-       regex = re.compile ('^.*\.par2$', re.IGNORECASE)
+       PAR2_REGEX = config.get_value ('regular expressions', 'par2_regex')
+       regex = re.compile (PAR2_REGEX, re.IGNORECASE)
        return [f for f in files if regex.match (f)]
 
 def find_all_par2_files (dir):
@@ -263,7 +281,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:
@@ -273,10 +291,15 @@ 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 ...')
 
        # 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],
+                               RarslaveLogger.MessageType.Fatal)
+
+               # No-heads here, but it's better than failing completely
+               extractor = RarslaveExtractor (TYPE_NOEXTRACT)
 
        return extractor
 
@@ -285,16 +308,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 ???
@@ -302,10 +331,13 @@ 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 = []
-       dregex = re.compile ('^.*\.(par2|\d|\d\d\d|rar|r\d\d|zip)$', re.IGNORECASE)
+       DELETE_REGEX = config.get_value ('regular expressions', 'delete_regex')
+       dregex = re.compile (DELETE_REGEX, re.IGNORECASE)
 
        return [f for f in files if dregex.match (f)]
 
@@ -351,43 +383,57 @@ 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, RarslaveLogger.MessageType.Fatal)
                        return -ECHECK
 
                # Extraction Stage
+               EXTRACT_DIR = config.get_value ('directories', 'extract_directory')
                extractor = find_extraction_heads (self.dir, self.likely_files)
-               ret = extractor.extract ('extract_dir') # FIXME: Get it from the config
+               ret = extractor.extract (EXTRACT_DIR)
 
-               if ret: # FAILURE
+               if ret != SUCCESS:
+                       logger.addMessage ('Extraction stage failed for: %s' % par2head, RarslaveLogger.MessageType.Fatal)
                        return -EEXTRACT
 
                # Deletion Stage
+               DELETE_INTERACTIVE = config.get_value ('options', 'interactive')
                deleteable_files = find_deleteable_files (self.likely_files)
-               ret = delete_list (deleteable_files)
+               ret = delete_list (deleteable_files, DELETE_INTERACTIVE)
 
-               if ret: # FAILURE
+               if ret != SUCCESS:
+                       logger.addMessage ('Deletion stage failed for: %s' % par2head, RarslaveLogger.MessageType.Fatal)
                        return -EDELETE
 
-               return 0
+               logger.addMessage ('Successfully completed: %s' % par2head)
+               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):
@@ -409,10 +455,35 @@ 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'
+
+       # Used in '--quiet' mode
+       if logger.hasFatalMessages ():
+               print '\nFatal Messages\n' + '=' * 80
+               logger.printLoglevel (RarslaveLogger.MessageType.Fatal)
+
+       # Used in no options mode
+       if logger.hasNormalMessages ():
+               print '\nNormal Messages\n' + '=' * 80
+               logger.printLoglevel (RarslaveLogger.MessageType.Normal)
+
+       # Used in --verbose mode
+       if logger.hasVerboseMessages ():
+               print '\nVerbose Messages\n' + '=' * 80
+               logger.printLoglevel (RarslaveLogger.MessageType.Verbose)
+
+       # Used in --debug mode
+       if logger.hasDebugMessages ():
+               print '\nDebug Messages\n' + '=' * 80
+               logger.printLoglevel (RarslaveLogger.MessageType.Debug)
+
+       print '\n\nALL MESSAGES:'
+       logger.printAllMessages ()
+
 if __name__ == '__main__':
        main ()
+