From: Ira W. Snyder Date: Sun, 24 Dec 2006 03:08:11 +0000 (-0800) Subject: [RARSLAVE] Add extraction class X-Git-Tag: v2.0.0~64 X-Git-Url: https://www.irasnyder.com/gitweb/?p=rarslave2.git;a=commitdiff_plain;h=2806f9d02c38aaaf848acc6c4ed080f3425faa87;ds=sidebyside [RARSLAVE] Add extraction class Adds the RarslaveExtractor class which handles extraction of all types of set supported by rarslave. Signed-off-by: Ira W. Snyder --- diff --git a/rarslave-test.py b/rarslave-test.py index 64d04f6..d8336b1 100644 --- a/rarslave-test.py +++ b/rarslave-test.py @@ -51,7 +51,7 @@ class rarslavetest (unittest.TestCase): def testFindLikelyFilesBadDir (self): DIR = '/fake/dir' - + self.assertRaises (ValueError, find_likely_files, "fake", DIR) def testFindAllPar2Files (self): @@ -76,7 +76,7 @@ class rarslavetest (unittest.TestCase): self.assertFalse (has_extension (FILE, 'part01')) def testIsNewRar (self): - DIR = '/home/irasnyd/downloads/test_material/01/' + DIR = os.getcwd() + '/test_material/01/' self.assertTrue (is_newrar (os.listdir (DIR))) diff --git a/rarslave.py b/rarslave.py index 47fbb03..093d5fa 100644 --- a/rarslave.py +++ b/rarslave.py @@ -3,6 +3,43 @@ import re, os, sys +# Global Variables +(TYPE_OLDRAR, TYPE_NEWRAR, TYPE_ZIP, TYPE_NOEXTRACT) = range (4) + +class RarslaveExtractor (object): + + def __init__ (self, type): + self.type = type + self.heads = [] + + def addHead (self, head): + assert not os.path.isfile (head) + + self.heads.append (head) + + def extract (self, todir): + # 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): + print 'Extracting (%s, %s)' % (file, todir) + + def __extract_zip (self, file, todir): + print 'Extracting (%s, %s)' % (file, todir) + + def __extract_noextract (self, file, todir): + print 'Extracting (%s, %s)' % (file, todir) + + def get_basename (name): """Strips most kinds of endings from a filename""" @@ -31,7 +68,7 @@ def find_likely_files (name, dir): ename = re.escape (name) regex = re.compile ('^%s.*$' % (ename, )) - return [f for f in os.listdir (dir) if regex.match (f)] + return [os.path.abspath(f) for f in os.listdir (dir) if regex.match (f)] def find_all_par2_files (dir): """Finds all par2 files in a directory""" @@ -67,36 +104,37 @@ def find_extraction_heads (files): # 2) post rar-3.0: .part01.rar .part02.rar # 3) zip all ver: .zip - heads = [] + extractor = None # 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): - heads.append (f) - - return heads + extractor.addHead (f) if is_newrar (files): + extractor = RarslaveExtractor (TYPE_NEWRAR) regex = re.compile ('^.*\.part01.rar$', re.IGNORECASE) for f in files: if regex.match (f): - heads.append (f) - - return heads + extractor.addHead (f) if is_zip (files): + extractor = RarslaveExtractor (TYPE_ZIP) regex = re.compile ('^.*\.zip$', re.IGNORECASE) for f in files: if regex.match (f): - heads.append (f) + extractor.addHead (f) - return heads + if is_noextract (files): + extractor = RarslaveExtractor (TYPE_NOEXTRACT) - # Not a type we know yet - raise ValueError + # Make sure we found the type + assert extractor != None + return extractor def is_oldrar (files): for f in files: @@ -127,27 +165,26 @@ def find_deleteable_files (files): return [f for f in files if dregex.match (f)] -def extract (heads, todir): - # Try to extract each head - # NOTE: REQUIRES full paths to heads - - PWD = os.getcwd() - - # FIXME: Should come from the config - RCMD = 'unrar x -o+ -- ' - ZCMD = 'unzip -- ' - - for h in heads: - # find type - # extract it - - # NOTE: probably not able to clean up effectively... - pass - - +def printlist (li): + for f in li: + print f def main (): - print find_all_par2_files ('/home/irasnyd/downloads/test_material/01/') + DIR = os.path.abspath ('test_material/01/') + p2files = find_all_par2_files (DIR) + files = find_likely_files (get_basename (p2files[0]), DIR) + find_extraction_heads (files).extract('nodir') + print 'DELETEABLE_FILES:' + printlist ( find_deleteable_files (files) ) + + print + + DIR = os.path.abspath ('test_material/13/') + p2files = find_all_par2_files (DIR) + files = find_likely_files (get_basename (p2files[0]), DIR) + find_extraction_heads (files).extract ('nodir') + print 'DELETEABLE_FILES:' + printlist ( find_deleteable_files (files) ) if __name__ == '__main__': main ()