- # Check error code
- if ret != SUCCESS:
- logger.addMessage ('Failed extracting: %s' % h)
- 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')
-
- cmd = NOEXTRACT_CMD % (file, todir)
- ret = run_command (cmd)
-
- # Check error code
- if ret != 0:
- return -EEXTRACT
-
- return SUCCESS
-
-
-
-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 = config.get_value ('commands', '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\" " % os.path.split (f)[1]
-
- if self.join:
- for f in all_files:
- if f not in par2_files:
- 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.file)
- return -ECHECK
-
- 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)
-
- # FIXME: re-enable this after testing
- print 'RUNNING (%s): %s' % (indir, cmd)
- return SUCCESS
-
- # return os.system (cmd)
-
-
-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
-
- return name
-
-def find_likely_files (name, dir):
- """Finds files which are likely to be part of the set corresponding
- to $name in the directory $dir"""
-
- if not os.path.isdir (os.path.abspath (dir)):
- raise ValueError # bad directory given
-
- dir = os.path.abspath (dir)
- ename = re.escape (name)
- regex = re.compile ('^%s.*$' % (ename, ))