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"""
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"""
# 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:
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 ()