From: Ira W. Snyder Date: Wed, 27 Dec 2006 06:43:01 +0000 (-0800) Subject: [RARSLAVE] Improve "likely file" detection X-Git-Tag: v2.0.0~37 X-Git-Url: https://www.irasnyder.com/gitweb/?p=rarslave2.git;a=commitdiff_plain;h=963e27c8139b956490607dda32df3a480a377165 [RARSLAVE] Improve "likely file" detection Improve the likely file detection by using not only the name of the par2 files, but also what they are protecting. This should help a lot. Update all uses of find_likely_files() to the new API. Also, fix the filetype detectors so that they work, and move the duplicate code to a new function. Signed-off-by: Ira W. Snyder --- diff --git a/rarslave.py b/rarslave.py index eb133a1..1969ce2 100644 --- a/rarslave.py +++ b/rarslave.py @@ -134,7 +134,7 @@ class RarslaveRepairer (object): # Get set up basename = get_basename (self.file) - all_files = find_likely_files (basename, self.dir) + all_files = find_likely_files (self.dir, self.file) all_files.sort () par2_files = find_par2_files (all_files) @@ -194,18 +194,23 @@ def get_basename (name): return name -def find_likely_files (name, dir): +def find_likely_files (dir, p2file): """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 + assert os.path.isdir (dir) + assert os.path.isfile (os.path.join (dir, p2file)) + + basename = get_basename (p2file) dir = os.path.abspath (dir) - ename = re.escape (name) + ename = re.escape (basename) regex = re.compile ('^%s.*$' % (ename, )) - return [f for f in os.listdir (dir) if regex.match (f)] + name_matches = [f for f in os.listdir (dir) if regex.match (f)] + parsed_matches = par2parser.get_protected_files (dir, p2file) + + return name_matches + parsed_matches def find_par2_files (files): """Find all par2 files in the list $files""" @@ -226,16 +231,6 @@ def find_all_par2_files (dir): return find_par2_files (files) -def has_extension (f, ext): - """Checks if f has the extension ext""" - - if ext[0] != '.': - ext = '.' + ext - - ext = re.escape (ext) - regex = re.compile ('^.*%s$' % (ext, ), re.IGNORECASE) - return regex.match (f) - def find_extraction_heads (dir, files): """Takes a list of possible files and finds likely heads of extraction.""" @@ -261,7 +256,7 @@ def find_extraction_heads (dir, files): if is_newrar (files): extractor = RarslaveExtractor (TYPE_NEWRAR) - regex = re.compile ('^.*\.part01.rar$', re.IGNORECASE) + regex = re.compile ('^.*\.part0*1.rar$', re.IGNORECASE) for f in files: if regex.match (f): extractor.addHead (dir, f) @@ -307,35 +302,34 @@ def find_extraction_heads (dir, files): return extractor -def is_oldrar (files): - for f in files: - if has_extension (f, '.r00'): - return True +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.""" - return False + if nocase: + cregex = re.compile (regex, re.IGNORECASE) + else: + cregex = re.compile (regex) -def is_newrar (files): for f in files: - if has_extension (f, '.part01.rar'): + if cregex.match (f): return True return False -def is_zip (files): - for f in files: - if has_extension (f, '.zip'): - return True +def is_oldrar (files): + return generic_matcher (files, '^.*\.r00$') - return False +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 ??? - for f in files: - if has_extension (f, '.001'): - return True - - return False + return generic_matcher (files, '^.*\.001$') def find_deleteable_files (files): # Deleteable types regex should come from the config @@ -363,7 +357,7 @@ class PAR2Set (object): self.file = file basename = get_basename (file) - self.likely_files = find_likely_files (basename, dir) + self.likely_files = find_likely_files (dir, file) def __list_eq (self, l1, l2):