- return find_par2_files (files)
-
-def find_extraction_heads (dir, files):
- """Takes a list of possible files and finds likely heads of
- extraction."""
-
- # 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 ('^.*\.part0*1.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 (EnvironmentError, OverflowError, OSError):
- logger.addMessage ('Error parsing PAR2 file: %s', f)
- continue
-
- if done:
- break
-
- if done:
- for f in prot_files:
- extractor.addHead (dir, f)
- else:
- logger.addMessage ('Error parsing all PAR2 files in this set ...')
-
- # Make sure we found the type
- if extractor == None:
- logger.addMessage ('Not able to find an extractor for this type of set: %s' % p2files[0],
- RarslaveLogger.MessageType.Verbose)
-
- # No-heads here, but it's better than failing completely
- extractor = RarslaveExtractor (TYPE_NOEXTRACT)
-
- return extractor
-
-def generic_matcher (files, regex, nocase=False):
- """Run the regex over the files, and see if one matches or not.
- NOTE: this does not return the matches, just if a match occurred."""
-
- if nocase:
- cregex = re.compile (regex, re.IGNORECASE)
- else:
- cregex = re.compile (regex)
-
- for f in files:
- if cregex.match (f):
- return True
-
- return False
-
-def is_oldrar (files):
- return generic_matcher (files, '^.*\.r00$')
-
-def is_newrar (files):
- return generic_matcher (files, '^.*\.part0*1\.rar$')
-
-def is_zip (files):
- return generic_matcher (files, '^.*\.zip$')
-
-def is_noextract (files):
- # Type that needs no extraction.
- # TODO: Add others ???
- return generic_matcher (files, '^.*\.001$')
-
-def printlist (li):
- for f in li:
- print f
-
-class PAR2Set (object):
-
- dir = None
- p2file = None # The starting par2
- basename = None # The p2file's basename
- all_p2files = []
- name_matched_files = [] # Files that match by basename of the p2file
- prot_matched_files = [] # Files that match by being protected members
-
- def __init__ (self, dir, p2file):
- assert os.path.isdir (dir)
- assert os.path.isfile (os.path.join (dir, p2file))
-
- self.dir = dir
- self.p2file = p2file
- self.basename = get_basename (p2file)
-
- # Find files that match by name only
- self.name_matched_files = self.__find_name_matches (self.dir, self.basename)
-
- # Find all par2 files for this set using name matches
- self.all_p2files = find_par2_files (self.name_matched_files)
-
- # Try to get the protected files for this set
- self.prot_matched_files = self.__parse_all_par2 ()
-
- 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.dir == rhs.dir) and (self.basename == rhs.basename) and \
- self.__list_eq (self.name_matched_files, rhs.name_matched_files) and \
- self.__list_eq (self.prot_matched_files, rhs.prot_matched_files)
-
- def __parse_all_par2 (self):
- """Searches though self.all_p2files and tries to parse at least one of them"""
- done = False
- files = []
-
- for f in self.all_p2files:
-
- # Exit early if we've found a good file
- if done:
- break
-
- try:
- files = Par2Parser.get_protected_files (self.dir, f)
- done = True
- except (EnvironmentError, OSError, OverflowError):
- logger.addMessage ('Corrupt PAR2 file: %s' % f, RarslaveLogger.MessageType.Fatal)
-
- # Now that we're out of the loop, check if we really finished
- if not done:
- logger.addMessage ('All PAR2 files corrupt for: %s' % self.p2file, RarslaveLogger.MessageType.Fatal)
-
- # Return whatever we've got, empty or not
- return files
-
- def __find_name_matches (self, dir, basename):
- """Finds files which are likely to be part of the set corresponding
- to $name in the directory $dir"""
-
- assert os.path.isdir (dir)
-
- ename = re.escape (basename)
- regex = re.compile ('^%s.*$' % (ename, ))
-
- return [f for f in os.listdir (dir) if regex.match (f)]
-
- def __update_name_matches (self):
- """Updates the self.name_matched_files variable with the most current information.
- This should be called after the directory contents are likely to change."""
-
- self.name_matched_files = self.__find_name_matches (self.dir, self.basename)
-
- def runCheckAndRepair (self):
- PAR2_CMD = config.get_value ('commands', 'par2repair')
-
- # Get set up
- all_files = self.name_matched_files + self.prot_matched_files
- join = is_noextract (all_files)
-
- # 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]
-
- if join:
- for f in all_files:
- if f not in self.p2files:
- 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.p2file, RarslaveLogger.MessageType.Fatal)
- return -ECHECK
-
- return SUCCESS
-
- def __find_deleteable_files (self):
- all_files = self.name_matched_files + self.prot_matched_files
- DELETE_REGEX = config.get_value ('regular expressions', 'delete_regex')
- dregex = re.compile (DELETE_REGEX, re.IGNORECASE)
-
- dfiles = [f for f in all_files if dregex.match (f)]
- dset = set(dfiles) # to eliminate dupes
- return list(dset)
-
- def __delete_list_of_files (self, dir, files, interactive=False):
- # Delete a list of files
-
- assert os.path.isdir (dir)
-
- done = False
- valid_y = ['Y', 'YES']
- valid_n = ['N', 'NO', '']
-
- if interactive:
- while not done:
- print 'Do you want to delete the following?:'
- printlist (files)
- s = raw_input ('Delete [y/N]: ').upper()
-
- if s in valid_y + valid_n:
- done = True
-
- if s in valid_n:
- return SUCCESS
-
- for f in files:
- try:
- os.remove (os.path.join (dir, f))
- logger.addMessage ('Deleteing: %s' % os.path.join (dir, f), RarslaveLogger.MessageType.Debug)
- except:
- logger.addMessage ('Failed to delete: %s' % os.path.join (dir, f),
- RarslaveLogger.MessageType.Fatal)
- return -EDELETE
-
- return SUCCESS
-
- def runDelete (self):
- deleteable_files = self.__find_deleteable_files ()
- ret = self.__delete_list_of_files (self.dir, deleteable_files, options.interactive)
-
- return ret
-
- def run_all (self):
- all_files = self.name_matched_files + self.prot_matched_files
-
- # Repair Stage
- ret = self.runCheckAndRepair ()
-
- if ret != SUCCESS:
- logger.addMessage ('Repair stage failed for: %s' % self.p2file, RarslaveLogger.MessageType.Fatal)
- return -ECHECK
-
- self.__update_name_matches ()
- all_files = self.name_matched_files + self.prot_matched_files
-
- # Extraction Stage
- EXTRACT_DIR = options.extract_dir
- extractor = find_extraction_heads (self.dir, all_files)
- ret = extractor.extract (EXTRACT_DIR)
-
- if ret != SUCCESS:
- logger.addMessage ('Extraction stage failed for: %s' % self.p2file, RarslaveLogger.MessageType.Fatal)
- return -EEXTRACT
-
- self.__update_name_matches ()
- all_files = self.name_matched_files + self.prot_matched_files
-
- # Deletion Stage
- ret = self.runDelete ()
-
- if ret != SUCCESS:
- logger.addMessage ('Deletion stage failed for: %s' % self.p2file, RarslaveLogger.MessageType.Fatal)
- return -EDELETE
-
- logger.addMessage ('Successfully completed: %s' % self.p2file)
- return SUCCESS
-
-