Print a traceback on unknown exceptions
[rarslave2.git] / PAR2Set / Join.py
index 3dd78ce..0970d87 100644 (file)
@@ -1,81 +1,82 @@
 #!/usr/bin/env python
 # vim: set ts=4 sts=4 sw=4 textwidth=92:
 
-import os
-import logging
-import PAR2Set.Base
-import rsutil.common
+"""
+Holds the Join class.
 
-#
-# This is a regular joined file type
-#
-# It will detect sets like the following:
-# X.par2
-# X.vol0+1.par2
-# X.vol1+2.par2
-# X.001
-# X.002
-# X.003
-#
-# Where the PAR2 files protect a file named X.avi (or similar). It will not
-# work where the PAR2 files are protecting the .001, etc files directly.
-#
+This module works with normal joined sets.
+
+It will detect sets like the following:
+X.par2
+X.vol0+1.par2
+...
 
-def detector (name_files, prot_files):
-       return rsutil.common.has_a_match ('^.*\.\d\d\d$', name_files) \
-                       and not rsutil.common.has_a_match ('^.*\.\d\d\d$', prot_files)
+X.001
+X.002
+...
 
+Where the PAR2 files protect a file named X.avi, or similar other types.
+It will not work where the PAR2 files are protecting the X.001, etc files
+directly.
+"""
 
-class Join (PAR2Set.Base.Base):
+__author__    = "Ira W. Snyder (devel@irasnyder.com)"
+__copyright__ = "Copyright (c) 2006,2007 Ira W. Snyder (devel@irasnyder.com)"
+__license__   = "GNU GPL v2 (or, at your option, any later version)"
 
-       def __repr__ (self):
-               return 'JOIN'
+#    Join.py
+#
+#    Copyright (C) 2006,2007  Ira W. Snyder (devel@irasnyder.com)
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-       def find_joinfiles (self):
-               return rsutil.common.find_matches ('^.*\.\d\d\d$', self.name_matched_files)
+import os, re, logging
+from PAR2Set import Base, utils
 
-       def runVerifyAndRepair (self):
-               PAR2_CMD = rsutil.common.config_get_value ('commands', 'par2repair')
+class Join(Base):
 
-               # 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]
+    def detect(self):
 
-               for f in self.find_joinfiles ():
-                       command += "\"%s\" " % os.path.split (f)[1]
+        regex = r'^.*\.\d\d\d$'
+        m1 = utils.hasAMatch(regex, self.similarlyNamedFiles)
+        m2 = utils.hasAMatch(regex, self.protectedFiles)
 
-               # run the command
-               ret = rsutil.common.run_command (command, self.dir)
+        # This is a good match if this criteria is met
+        if m1 and not m2:
+            return
 
-               # check the result
-               if ret != 0:
-                       logging.critical ('PAR2 Check / Repair failed: %s' % self.p2file)
-                       return -rsutil.common.ECHECK
+        raise TypeError
 
-               return rsutil.common.SUCCESS
+    ############################################################################
 
-       def find_extraction_heads (self):
-               return self.prot_matched_files
+    def repair(self):
 
-       def extraction_function (self, file, todir):
-               """Extract a single file of this type to the given directory"""
+        regex = r'^.*\.\d\d\d$'
+        files = utils.findMatches(regex, self.similarlyNamedFiles)
+        utils.runCommand(['par2repair'] + self.PAR2Files + files, self.directory)
 
-               NOEXTRACT_CMD = rsutil.common.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 rsutil.common.SUCCESS
+    def findDeletableFiles(self):
 
-               cmd = NOEXTRACT_CMD % (file, todir)
-               ret = rsutil.common.run_command (cmd)
+        files = Base.findDeletableFiles(self)
+        files = [f for f in files if f not in self.protectedFiles]
 
-               # Check error code
-               if ret != 0:
-                       return -rsutil.common.EEXTRACT
+        return files
 
-               return rsutil.common.SUCCESS
+    ############################################################################