X-Git-Url: https://www.irasnyder.com/gitweb/?p=rarslave2.git;a=blobdiff_plain;f=rarslave.py;h=49f72b119ed0cdf1b22f05670f5ce96b8ec768b2;hp=eb133a1881b81df204c6f8f96abe124ec26b787a;hb=02e9b17925f6608cde62f6ea3d374ea1c39350c8;hpb=2c789e235b0fe20478427e49148b9697f182ac95 diff --git a/rarslave.py b/rarslave.py index eb133a1..49f72b1 100644 --- a/rarslave.py +++ b/rarslave.py @@ -5,7 +5,7 @@ VERSION="2.0.0" PROGRAM="rarslave2" import re, os, sys, optparse -import par2parser +import Par2Parser import RarslaveConfig import RarslaveLogger @@ -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,27 @@ 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)] + try: + parsed_matches = Par2Parser.get_protected_files (dir, p2file) + except EnvironmentError: + parsed_matches = [] + logger.addMessage ('Bad par2 file: %s' % p2file, RarslaveLogger.MessageType.Fatal) + + return name_matches + parsed_matches def find_par2_files (files): """Find all par2 files in the list $files""" @@ -226,16 +235,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 +260,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) @@ -282,9 +281,9 @@ def find_extraction_heads (dir, files): for f in p2files: done = False try: - prot_files = par2parser.get_protected_files (dir, f) + prot_files = Par2Parser.get_protected_files (dir, f) done = True - except: #FIXME: add the actual exceptions + except EnvironmentError: logger.addMessage ('Error parsing PAR2 file: %s', f) continue @@ -300,42 +299,41 @@ def find_extraction_heads (dir, files): # 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.Fatal) + RarslaveLogger.MessageType.Verbose) # No-heads here, but it's better than failing completely extractor = RarslaveExtractor (TYPE_NOEXTRACT) 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 +361,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): @@ -426,6 +424,7 @@ def delete_list (dir, files, interactive=False): 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: