-# vim: set ts=4 sts=4 sw=4 textwidth=112 :
-
-import re, os, sys
-import par2parser
-
-# Global Variables
-(TYPE_OLDRAR, TYPE_NEWRAR, TYPE_ZIP, TYPE_NOEXTRACT) = range (4)
-(ECHECK, EEXTRACT, EDELETE) = range(1,4)
-
-class RarslaveExtractor (object):
-
- def __init__ (self, type):
- self.type = type
- self.heads = []
-
- def addHead (self, dir, head):
- assert os.path.isdir (dir)
- # REQUIRES that the dir is valid, but not that the file is valid, so that
- # we can move a file that doesn't exist yet.
- # 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))
-
- def extract (self, todir):
- # Extract all heads of this set
-
- # Create the directory $todir if it doesn't exist
- if not os.path.isdir (todir):
- # TODO: LOGGER
- try:
- os.makedirs (todir)
- except OSError:
- # TODO: LOGGER
- return -EEXTRACT
-
- # 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:
- extraction_func (h, todir)
-
- def __extract_rar (self, file, todir):
- assert os.path.isfile (file)
- assert os.path.isdir (todir)
-
- RAR_CMD = 'unrar x -o+ -- '
-
- cmd = '%s \"%s\"' % (RAR_CMD, file)
- ret = run_command (cmd, todir)
-
- # Check error code
- if ret != 0:
- return -EEXTRACT
-
- def __extract_zip (self, file, todir):
- ZIP_CMD = 'unzip \"%s\" -d \"%s\"'
-
- cmd = ZIP_CMD % (file, todir)
- ret = run_command (cmd)
-
- # Check error code
- if ret != 0:
- return -EEXTRACT
-
- 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)
- ret = run_command (cmd)
-
- # Check error code
- if ret != 0:
- return -EEXTRACT
-
-
-
-class RarslaveRepairer (object):
- # Verify (and repair) the set
- # Make sure it worked, otherwise clean up and return failure
-
- def __init__ (self, dir, file, join=False):
- self.dir = dir # the directory containing the par2 file
- self.file = file # the par2 file
- self.join = join # True if the par2 set is 001 002 ...
-
- assert os.path.isdir (dir)
- assert os.path.isfile (os.path.join (dir, file))
-
- def checkAndRepair (self):
- # Form the command:
- # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
- PAR2_CMD = 'par2repair -- '
-
- # Get set up
- basename = get_basename (self.file)
- all_files = find_likely_files (basename, self.dir)
- all_files.sort ()
- par2_files = find_par2_files (all_files)
-
- # assemble the command
- command = "%s \"%s\" " % (PAR2_CMD, self.file)
-
- for f in par2_files:
- if f != self.file:
- command += "\"%s\" " % get_filename(f)
-
- if self.join:
- for f in all_files:
- if f not in par2_files:
- command += "\"%s\" " % get_filename(f)
-
- # run the command
- ret = run_command (command, self.dir)
-
- # check the result
- if ret != 0:
- # TODO: logger
- print 'error during checkAndRepair()'
- return -ECHECK
-
-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)
-
- # FIXME: re-enable this after testing
- print 'RUNNING (%s): %s' % (indir, cmd)
- return 0
-
- # return os.system (cmd)
-
-
-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]
+# vim: set ts=4 sts=4 sw=4 textwidth=92:
+
+"""
+The main program of the rarslave project.
+
+This handles all of the commandline, configuration file, and option
+work. It gets the environment set up for a run using the RarslaveDetector
+class.
+"""
+
+__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)"
+
+# rarslave.py -- a usenet autorepair and autoextract utility
+#
+# 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
+
+VERSION="2.0.0"
+PROGRAM="rarslave"
+
+import os, sys, optparse, logging
+import rsutil
+import RarslaveDetector
+
+# Global options from the rsutil.globals class
+options = rsutil.globals.options
+config = rsutil.globals.config
+
+# A tiny class to hold logging output until we're finished
+class DelayedLogger (object):
+
+ """A small class to hold logging output until the program is finished running.
+ It emulates sys.stdout in the needed ways for the logging module."""
+
+ def __init__ (self, output=sys.stdout.write):
+ self.__messages = []
+ self.__output = output
+
+ def write (self, msg):
+ self.__messages.append (msg)
+
+ def flush (self):
+ pass