X-Git-Url: https://www.irasnyder.com/gitweb/?p=rarslave2.git;a=blobdiff_plain;f=rarslave.py;h=a27c334cfe2e14d2b159c2defbaf5657c81e1de4;hp=ee8be7e7b3201af831f3bed17d25cc4202b8cd94;hb=8a063b3c5c7910db244bdab28e0409cde2ff551c;hpb=f2f961bc4b0dcedb3a6108b14506e4416a6628bf;ds=sidebyside diff --git a/rarslave.py b/rarslave.py index ee8be7e..a27c334 100644 --- a/rarslave.py +++ b/rarslave.py @@ -4,155 +4,52 @@ VERSION="2.0.0" PROGRAM="rarslave2" -import re, os, sys, optparse -import Par2Parser -import RarslaveConfig -import RarslaveLogger +import os, sys, optparse, logging +import rsutil +import RarslaveDetector -# Global Variables -(TYPE_OLDRAR, TYPE_NEWRAR, TYPE_ZIP, TYPE_NOEXTRACT) = range (4) -(SUCCESS, ECHECK, EEXTRACT, EDELETE) = range(4) -config = RarslaveConfig.RarslaveConfig() -logger = RarslaveLogger.RarslaveLogger () +# Global options from the rsutil.globals class +options = rsutil.globals.options +config = rsutil.globals.config -# Global options to be set / used later. -options = None +# A tiny class to hold logging output until we're finished +class DelayedLogger (object): + def __init__ (self, output=sys.stdout.write): + self.__messages = [] + self.__output = output -class RarslaveExtractor (object): + def write (self, msg): + self.__messages.append (msg) - def __init__ (self, type): - self.type = type - self.heads = [] + def flush (self): + pass - def addHead (self, dir, head): - assert os.path.isdir (dir) - assert os.path.isfile (os.path.join (dir, head)) + def size (self): + """Returns the number of messages queued for printing""" + return len (self.__messages) - full_head = os.path.join (dir, head) - logger.addMessage ('Adding extraction head: %s' % full_head, RarslaveLogger.MessageType.Debug) - self.heads.append (full_head) + def close (self): + """Print all messages, clear the queue""" + for m in self.__messages: + self.__output (m) - def extract (self, todir=None): - # Extract all heads of this set + self.__messages = [] - # Create the directory $todir if it doesn't exist - if todir != None and not os.path.isdir (todir): - logger.addMessage ('Creating directory: %s' % todir, RarslaveLogger.MessageType.Verbose) - try: - os.makedirs (todir) - except OSError: - logger.addMessage ('FAILED to create directory: %s' % todir, RarslaveLogger.MessageType.Fatal) - return -EEXTRACT +# A tiny class used to find unique PAR2 sets +class CompareSet (object): - # Extract all heads - extraction_func = \ - { TYPE_OLDRAR : self.__extract_rar, - TYPE_NEWRAR : self.__extract_rar, - TYPE_ZIP : self.__extract_zip, - TYPE_NOEXTRACT : self.__extract_noextract }[self.type] - - # Call the extraction function on each head - for h in self.heads: - if todir == None: - # Run in the head's directory - ret = extraction_func (h, os.path.dirname (h)) - else: - 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 = config.get_value ('commands', 'unrar') - - cmd = '%s \"%s\"' % (RAR_CMD, file) - ret = run_command (cmd, todir) - - # Check error code - if ret != 0: - return -EEXTRACT - - return SUCCESS - - def __extract_zip (self, file, todir): - ZIP_CMD = config.get_value ('commands', 'unzip') - - cmd = ZIP_CMD % (file, todir) - ret = run_command (cmd) - - # Check error code - 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! - NOEXTRACT_CMD = config.get_value ('commands', 'noextract') - - # Make sure that both files are not the same file. If they are, don't run at all. - if os.path.samefile (file, os.path.join (todir, file)): - return SUCCESS - - cmd = NOEXTRACT_CMD % (file, todir) - ret = run_command (cmd) - - # Check error code - if ret != 0: - return -EEXTRACT - - 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. - - pwd = os.getcwd () - - if indir != None: - assert os.path.isdir (indir) # MUST be a directory! - os.chdir (indir) - - ret = os.system (cmd) - os.chdir (pwd) - return ret - -def full_abspath (p): - return os.path.abspath (os.path.expanduser (p)) - -def get_basename (name): - """Strips most kinds of endings from a filename""" - - regex = config.get_value ('regular expressions', 'basename_regex') - r = re.compile (regex, re.IGNORECASE) - done = False - - while not done: - done = True - - if r.match (name): - g = r.match (name).groups() - name = g[0] - done = False + def __init__ (self, dir, p2file): + self.dir = dir + self.p2file = p2file - return name + self.basename = rsutil.common.get_basename (self.p2file) + self.name_matches = rsutil.common.find_name_matches (self.dir, self.basename) -def find_par2_files (files): - """Find all par2 files in the list $files""" + def __eq__ (self, rhs): + return (self.dir == rhs.dir) \ + and (self.basename == rhs.basename) \ + and rsutil.common.list_eq (self.name_matches, rhs.name_matches) - 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): """Finds all par2 files in a directory""" @@ -164,308 +61,7 @@ def find_all_par2_files (dir): dir = os.path.abspath (dir) files = os.listdir (dir) - return find_par2_files (files) - -def find_extraction_heads (dir, files): - """Takes a list of possible files and finds likely heads of - extraction.""" - - # NOTE: perhaps this should happen AFTER repair is - # NOTE: successful. That way all files would already exist - - # According to various sources online: - # 1) pre rar-3.0: .rar .r00 .r01 ... - # 2) post rar-3.0: .part01.rar .part02.rar - # 3) zip all ver: .zip - - extractor = None - p2files = find_par2_files (files) - - # Old RAR type, find all files ending in .rar - if is_oldrar (files): - extractor = RarslaveExtractor (TYPE_OLDRAR) - regex = re.compile ('^.*\.rar$', re.IGNORECASE) - for f in files: - if regex.match (f): - extractor.addHead (dir, f) - - if is_newrar (files): - extractor = RarslaveExtractor (TYPE_NEWRAR) - regex = re.compile ('^.*\.part0*1.rar$', re.IGNORECASE) - for f in files: - if regex.match (f): - extractor.addHead (dir, f) - - if is_zip (files): - extractor = RarslaveExtractor (TYPE_ZIP) - regex = re.compile ('^.*\.zip$', re.IGNORECASE) - for f in files: - if regex.match (f): - extractor.addHead (dir, f) - - if is_noextract (files): - # Use the Par2 Parser (from cfv) here to find out what files are protected. - # Since these are not being extracted, they will be mv'd to another directory - # later. - extractor = RarslaveExtractor (TYPE_NOEXTRACT) - - for f in p2files: - done = False - try: - prot_files = Par2Parser.get_protected_files (dir, f) - done = True - except (EnvironmentError, OverflowError, OSError): - logger.addMessage ('Error parsing PAR2 file: %s', f) - continue - - if done: - break - - if done: - for f in prot_files: - extractor.addHead (dir, f) - else: - logger.addMessage ('Error parsing all PAR2 files in this set ...') - - # Make sure we found the type - if extractor == None: - logger.addMessage ('Not able to find an extractor for this type of set: %s' % p2files[0], - RarslaveLogger.MessageType.Verbose) - - # No-heads here, but it's better than failing completely - extractor = RarslaveExtractor (TYPE_NOEXTRACT) - - return extractor - -def generic_matcher (files, regex, nocase=False): - """Run the regex over the files, and see if one matches or not. - NOTE: this does not return the matches, just if a match occurred.""" - - if nocase: - cregex = re.compile (regex, re.IGNORECASE) - else: - cregex = re.compile (regex) - - for f in files: - if cregex.match (f): - return True - - return False - -def is_oldrar (files): - return generic_matcher (files, '^.*\.r00$') - -def is_newrar (files): - return generic_matcher (files, '^.*\.part0*1\.rar$') - -def is_zip (files): - return generic_matcher (files, '^.*\.zip$') - -def is_noextract (files): - # Type that needs no extraction. - # TODO: Add others ??? - return generic_matcher (files, '^.*\.001$') - -def printlist (li): - for f in li: - print f - -class PAR2Set (object): - - dir = None - p2file = None # The starting par2 - basename = None # The p2file's basename - all_p2files = [] - name_matched_files = [] # Files that match by basename of the p2file - prot_matched_files = [] # Files that match by being protected members - - def __init__ (self, dir, p2file): - assert os.path.isdir (dir) - assert os.path.isfile (os.path.join (dir, p2file)) - - self.dir = dir - self.p2file = p2file - self.basename = get_basename (p2file) - - # Find files that match by name only - self.name_matched_files = self.__find_name_matches (self.dir, self.basename) - - # Find all par2 files for this set using name matches - self.all_p2files = find_par2_files (self.name_matched_files) - - # Try to get the protected files for this set - self.prot_matched_files = self.__parse_all_par2 () - - def __list_eq (self, l1, l2): - - if len(l1) != len(l2): - return False - - for e in l1: - if e not in l2: - return False - - return True - - def __eq__ (self, rhs): - return (self.dir == rhs.dir) and (self.basename == rhs.basename) and \ - self.__list_eq (self.name_matched_files, rhs.name_matched_files) and \ - self.__list_eq (self.prot_matched_files, rhs.prot_matched_files) - - def __parse_all_par2 (self): - """Searches though self.all_p2files and tries to parse at least one of them""" - done = False - files = [] - - for f in self.all_p2files: - - # Exit early if we've found a good file - if done: - break - - try: - files = Par2Parser.get_protected_files (self.dir, f) - done = True - except (EnvironmentError, OSError, OverflowError): - logger.addMessage ('Corrupt PAR2 file: %s' % f, RarslaveLogger.MessageType.Fatal) - - # Now that we're out of the loop, check if we really finished - if not done: - logger.addMessage ('All PAR2 files corrupt for: %s' % self.p2file, RarslaveLogger.MessageType.Fatal) - - # Return whatever we've got, empty or not - return files - - def __find_name_matches (self, dir, basename): - """Finds files which are likely to be part of the set corresponding - to $name in the directory $dir""" - - assert os.path.isdir (dir) - - ename = re.escape (basename) - regex = re.compile ('^%s.*$' % (ename, )) - - return [f for f in os.listdir (dir) if regex.match (f)] - - def __update_name_matches (self): - """Updates the self.name_matched_files variable with the most current information. - This should be called after the directory contents are likely to change.""" - - self.name_matched_files = self.__find_name_matches (self.dir, self.basename) - - def runCheckAndRepair (self): - PAR2_CMD = config.get_value ('commands', 'par2repair') - - # Get set up - all_files = self.name_matched_files + self.prot_matched_files - join = is_noextract (all_files) - - # 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] - - if join: - for f in all_files: - if f not in self.p2files: - command += "\"%s\" " % os.path.split (f)[1] - - # run the command - ret = run_command (command, self.dir) - - # check the result - if ret != 0: - logger.addMessage ('PAR2 Check / Repair failed: %s' % self.p2file, RarslaveLogger.MessageType.Fatal) - return -ECHECK - - return SUCCESS - - def __find_deleteable_files (self): - all_files = self.name_matched_files + self.prot_matched_files - DELETE_REGEX = config.get_value ('regular expressions', 'delete_regex') - dregex = re.compile (DELETE_REGEX, re.IGNORECASE) - - dfiles = [f for f in all_files if dregex.match (f)] - dset = set(dfiles) # to eliminate dupes - return list(dset) - - def __delete_list_of_files (self, dir, files, interactive=False): - # Delete a list of files - - assert os.path.isdir (dir) - - done = False - valid_y = ['Y', 'YES'] - valid_n = ['N', 'NO', ''] - - if interactive: - while not done: - print 'Do you want to delete the following?:' - printlist (files) - 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: - try: - os.remove (os.path.join (dir, f)) - logger.addMessage ('Deleteing: %s' % os.path.join (dir, f), RarslaveLogger.MessageType.Debug) - except: - logger.addMessage ('Failed to delete: %s' % os.path.join (dir, f), - RarslaveLogger.MessageType.Fatal) - return -EDELETE - - return SUCCESS - - def runDelete (self): - deleteable_files = self.__find_deleteable_files () - ret = self.__delete_list_of_files (self.dir, deleteable_files, options.interactive) - - return ret - - def run_all (self): - all_files = self.name_matched_files + self.prot_matched_files - - # Repair Stage - ret = self.runCheckAndRepair () - - if ret != SUCCESS: - logger.addMessage ('Repair stage failed for: %s' % self.p2file, RarslaveLogger.MessageType.Fatal) - return -ECHECK - - self.__update_name_matches () - all_files = self.name_matched_files + self.prot_matched_files - - # Extraction Stage - EXTRACT_DIR = options.extract_dir - extractor = find_extraction_heads (self.dir, all_files) - ret = extractor.extract (EXTRACT_DIR) - - if ret != SUCCESS: - logger.addMessage ('Extraction stage failed for: %s' % self.p2file, RarslaveLogger.MessageType.Fatal) - return -EEXTRACT - - self.__update_name_matches () - all_files = self.name_matched_files + self.prot_matched_files - - # Deletion Stage - ret = self.runDelete () - - if ret != SUCCESS: - logger.addMessage ('Deletion stage failed for: %s' % self.p2file, RarslaveLogger.MessageType.Fatal) - return -EDELETE - - logger.addMessage ('Successfully completed: %s' % self.p2file) - return SUCCESS - - + return rsutil.common.find_par2_files (files) def generate_all_parsets (dir): # Generate all parsets in the given directory. @@ -476,11 +72,11 @@ def generate_all_parsets (dir): p2files = find_all_par2_files (dir) for f in p2files: - p = PAR2Set (dir, f) + p = CompareSet (dir, f) if p not in parsets: parsets.append (p) - return parsets + return [(p.dir, p.p2file) for p in parsets] def check_required_progs(): """Check if the required programs are installed""" @@ -488,13 +84,13 @@ def check_required_progs(): shell_not_found = 32512 needed = [] - if run_command ('par2repair --help > /dev/null 2>&1') == shell_not_found: + if rsutil.common.run_command ('par2repair --help > /dev/null 2>&1') == shell_not_found: needed.append ('par2repair') - if run_command ('unrar --help > /dev/null 2>&1') == shell_not_found: + if rsutil.common.run_command ('unrar --help > /dev/null 2>&1') == shell_not_found: needed.append ('unrar') - if run_command ('unzip --help > /dev/null 2>&1') == shell_not_found: + if rsutil.common.run_command ('unzip --help > /dev/null 2>&1') == shell_not_found: needed.append ('unzip') if needed: @@ -506,7 +102,7 @@ def check_required_progs(): def run_options (options): # Fix directories - options.work_dir = full_abspath (options.work_dir) + options.work_dir = rsutil.common.full_abspath (options.work_dir) # Make sure that the directory is valid if not os.path.isdir (options.work_dir): @@ -516,7 +112,7 @@ def run_options (options): sys.exit (1) if options.extract_dir != None: - options.extract_dir = full_abspath (options.extract_dir) + options.extract_dir = rsutil.common.full_abspath (options.extract_dir) if options.version: print PROGRAM + ' - ' + VERSION @@ -541,60 +137,40 @@ def find_loglevel (options): loglevel = options.verbose - options.quiet - if loglevel < RarslaveLogger.MessageType.Fatal: - loglevel = RarslaveLogger.MessageType.Fatal - - if loglevel > RarslaveLogger.MessageType.Debug: - loglevel = RarslaveLogger.MessageType.Debug - - return loglevel - -def printMessageTable (loglevel): + if loglevel > 1: + loglevel = 1 - if logger.hasFatalMessages (): - print '\nFatal Messages\n' + '=' * 80 - logger.printLoglevel (RarslaveLogger.MessageType.Fatal) + if loglevel < -3: + loglevel = -3 - if loglevel == RarslaveLogger.MessageType.Fatal: - return + LEVELS = { 1 : logging.DEBUG, + 0 : logging.INFO, + -1: logging.WARNING, + -2: logging.ERROR, + -3: logging.CRITICAL + } - if logger.hasNormalMessages (): - print '\nNormal Messages\n' + '=' * 80 - logger.printLoglevel (RarslaveLogger.MessageType.Normal) - - if loglevel == RarslaveLogger.MessageType.Normal: - return - - if logger.hasVerboseMessages (): - print '\nVerbose Messages\n' + '=' * 80 - logger.printLoglevel (RarslaveLogger.MessageType.Verbose) - - if loglevel == RarslaveLogger.MessageType.Verbose: - return - - if logger.hasDebugMessages (): - print '\nDebug Messages\n' + '=' * 80 - logger.printLoglevel (RarslaveLogger.MessageType.Debug) - - return + return LEVELS [loglevel] def main (): + # Setup the logger + logger = DelayedLogger () + logging.basicConfig (stream=logger, level=logging.WARNING, \ + format='%(levelname)-8s %(message)s') + # Build the OptionParser parser = optparse.OptionParser() - parser.add_option('-n', '--not-recursive', - action='store_false', dest='recursive', - default=config.get_value('options', 'recursive'), + parser.add_option('-n', '--not-recursive', action='store_false', dest='recursive', + default=rsutil.common.config_get_value('options', 'recursive'), help="Don't run recursively") - parser.add_option('-d', '--work-dir', - dest='work_dir', type='string', - default=config.get_value('directories', 'working_directory'), + parser.add_option('-d', '--work-dir', dest='work_dir', type='string', + default=rsutil.common.config_get_value('directories', 'working_directory'), help="Start running at DIR", metavar='DIR') - parser.add_option('-e', '--extract-dir', - dest='extract_dir', type='string', - default=config.get_value('directories', 'extract_directory'), + parser.add_option('-e', '--extract-dir', dest='extract_dir', type='string', + default=rsutil.common.config_get_value('directories', 'extract_directory'), help="Extract to DIR", metavar='DIR') parser.add_option('-p', '--check-required-programs', @@ -611,7 +187,7 @@ def main (): default=False, help="Write out the current config") parser.add_option('-i', '--interactive', dest='interactive', action='store_true', - default=config.get_value('options', 'interactive'), + default=rsutil.common.config_get_value('options', 'interactive'), help="Confirm before removing files") parser.add_option('-q', '--quiet', dest='quiet', action='count', @@ -627,29 +203,34 @@ def main (): # Parse the given options global options - (options, args) = parser.parse_args() + (rsutil.globals.options, args) = parser.parse_args() + options = rsutil.globals.options # Run any special actions that are needed on these options run_options (options) - # Find the loglevel using the options given - loglevel = find_loglevel (options) + # Find the loglevel using the options given + logging.getLogger().setLevel (find_loglevel (options)) # Run recursively if options.recursive: for (dir, subdirs, files) in os.walk (options.work_dir): parsets = generate_all_parsets (dir) - for p in parsets: - p.run_all () + for (p2dir, p2file) in parsets: + detector = RarslaveDetector.RarslaveDetector (p2dir, p2file) + ret = detector.runMatchingTypes () # Non-recursive else: parsets = generate_all_parsets (options.work_dir) - for p in parsets: - p.run_all () + for (p2dir, p2file) in parsets: + detector = RarslaveDetector.RarslaveDetector (p2dir, p2file) + ret = detector.runMatchingTypes () # Print the results - printMessageTable (loglevel) + if logger.size () > 0: + print '\nLog\n' + '=' * 80 + logger.close () # Done! return 0