- # 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 ('^.*\.part01.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: #FIXME: add the actual exceptions
- print 'ERROR PARSING P2FILE ...', f
- continue
-
- if done:
- break
-
- if done:
- for f in prot_files:
- extractor.addHead (dir, f)
- else:
- print 'BADNESS'
-
- # Make sure we found the type
- assert extractor != None
-
- return extractor
-
-def is_oldrar (files):
- for f in files:
- if has_extension (f, '.r00'):
- return True
-
-def is_newrar (files):
- for f in files:
- if has_extension (f, '.part01.rar'):
- return True
-
-def is_zip (files):
- for f in files:
- if has_extension (f, '.zip'):
- return True
-
-def is_noextract (files):
- # Type that needs no extraction.
- # TODO: Add others ???
- for f in files:
- if has_extension (f, '.001'):
- return True
-
-def find_deleteable_files (files):
- # Deleteable types regex should come from the config
- dfiles = []
- 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)]
-
-def printlist (li):
- for f in li:
- print f
-
-class PAR2Set (object):
-
- dir = None
- file = None
- likely_files = []
-
- def __init__ (self, dir, file):
- assert os.path.isdir (dir)
- assert os.path.isfile (os.path.join (dir, file))
-
- self.dir = dir
- self.file = file
-
- basename = get_basename (file)
- self.likely_files = find_likely_files (basename, dir)
-
- 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.__list_eq (self.likely_files, rhs.likely_files)
-
- def run_all (self):
- par2files = find_par2_files (self.likely_files)
- par2head = par2files[0]
-
- join = is_noextract (self.likely_files)
-
- # Repair Stage
- repairer = RarslaveRepairer (self.dir, par2head, join)
- ret = repairer.checkAndRepair () # FIXME: Check return value
-
- if ret: # FAILURE
- 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)
-
- if ret: # FAILURE
- return -EEXTRACT
-
- # Deletion Stage
- DELETE_INTERACTIVE = config.get_value ('options', 'interactive')
- deleteable_files = find_deleteable_files (self.likely_files)
- ret = delete_list (deleteable_files, DELETE_INTERACTIVE)
-
- 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
-
- if interactive:
- # TODO: prompt here
- # prompt -> OK_TO_DELETE -> do nothing, fall through
- # prompt -> NOT_OK -> return immediately
- pass
-
- for f in files:
- # FIXME: re-enable this in production
- # os.remove (f)
- print 'rm', f
-
- return 0
+ # Make sure that the directory is valid
+ if not os.path.isdir (options.directory):
+ sys.stderr.write ('\"%s\" is not a valid directory. Use the \"-d\"\n' % options.directory)
+ sys.stderr.write ('option to override the working directory temporarily, or edit the\n')
+ sys.stderr.write ('configuration file to override the working directory permanently.\n')
+ sys.exit (1)
+
+ if options.version:
+ print PROGRAM + ' - ' + VERSION
+ print
+ print 'Copyright (c) 2005-2008 Ira W. Snyder (devel@irasnyder.com)'
+ print
+ print 'This program comes with ABSOLUTELY NO WARRANTY.'
+ print 'This is free software, and you are welcome to redistribute it'
+ print 'under certain conditions. See the file COPYING for details.'
+ sys.exit (0)
+
+ return (options, args)
+
+################################################################################
+
+# Find each unique CompareSet in the given directory and set of files
+def findUniqueSets(directory, files):
+
+ regex = r'^.*\.par2'
+ s = []
+
+ for f in PAR2Set.utils.findMatches(regex, files):
+
+ try:
+ c = PAR2Set.CompareSet(directory, f)
+ except:
+ # We just ignore any errors that happen, such as
+ # parsing the PAR file
+ pass
+ else:
+ # Ok, we got a valid set, add it to s
+ if c not in s:
+ s.append(c)
+
+ return s
+
+################################################################################
+
+# Run each PAR2Set type on a CompareSet
+def runEachType(cs, options):
+
+ types = (
+ PAR2Set.JoinProtected,
+ PAR2Set.Join,
+ PAR2Set.ZIP,
+ PAR2Set.OldRAR,
+ PAR2Set.NewRAR,
+ PAR2Set.ExtractFirstOldRAR,
+ PAR2Set.ExtractFirstNewRAR,
+ PAR2Set.NoExtract,
+ )
+
+ detected = False
+
+ # Try to detect each type in turn
+ for t in types:
+ try:
+ instance = t(cs, options)
+ except TypeError:
+ logging.debug('%s not detected for %s' % (t.__name__, cs.parityFile))
+ continue
+ else:
+ detected = True
+ logging.debug('%s detected for %s' % (t.__name__, cs.parityFile))
+
+ # We detected something, try to run it
+ try:
+ instance.run()
+ except (OSError, CalledProcessError):
+ logging.critical('Failure: %s' % instance)
+ else:
+ # Leave early, we're done
+ logging.info('Success: %s' % instance)
+ return
+
+ # Check that at least one detection worked
+ if not detected:
+ logging.critical('Detection failed: %s' % cs.parityFile)
+ logging.debug('The following information will help to create a detector')
+ logging.debug('===== BEGIN CompareSet RAW INFO =====')
+ logging.debug(str(cs))
+ logging.debug('===== END CompareSet RAW INFO =====')
+
+ # If we got here, either the detection didn't work or the run itself didn't
+ # work, so print out the message telling the user that we were unsuccessful
+ logging.critical('Unsuccessful: %s' % cs.parityFile)
+
+################################################################################
+
+def runDirectory(directory, files, options):
+
+ logging.debug('Running in directory: %s' % directory)
+ sets = findUniqueSets(directory, files)
+
+ for cs in sets:
+ try:
+ runEachType(cs, options)
+ except Exception, e:
+ import traceback
+ logging.error('Unknown Exception: %s' % cs.parityFile)
+ logging.error('===== BEGIN Bactrace =====')
+ [logging.error(l) for l in traceback.format_exc(e).split('\n')]
+ logging.error('===== END Bactrace =====')
+
+################################################################################