+ # 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 =====')
+
+################################################################################