Initial work on extraction.
[rarslave2.git] / rarslave.py
index 7e38816..47fbb03 100644 (file)
@@ -45,6 +45,105 @@ def find_all_par2_files (dir):
        # Find all files
        return [f for f in os.listdir (dir) if regex.match (f)]
 
        # Find all files
        return [f for f in os.listdir (dir) if regex.match (f)]
 
+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 (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 
+
+       heads = []
+
+       # Old RAR type, find all files ending in .rar
+       if is_oldrar (files):
+               regex = re.compile ('^.*\.rar$', re.IGNORECASE)
+               for f in files:
+                       if regex.match (f):
+                               heads.append (f)
+
+               return heads
+
+       if is_newrar (files):
+               regex = re.compile ('^.*\.part01.rar$', re.IGNORECASE)
+               for f in files:
+                       if regex.match (f):
+                               heads.append (f)
+
+               return heads
+
+       if is_zip (files):
+               regex = re.compile ('^.*\.zip$', re.IGNORECASE)
+               for f in files:
+                       if regex.match (f):
+                               heads.append (f)
+
+               return heads
+
+       # Not a type we know yet
+       raise ValueError
+
+
+def is_oldrar (files):
+       for f in files:
+               if has_extension (f, '.r00'):
+                       return True
+
+def is_newrar (files):
+       for f in files:
+               if has_extension (f, '.part01.rar'):
+                       return True
+
+def is_zip (files):
+       for f in files:
+               if has_extension (f, '.zip'):
+                       return True
+
+def is_noextract (files):
+       # Type that needs no extraction.
+       # TODO: Add others ???
+       for f in files:
+               if has_extension (f, '.001'):
+                       return True
+
+def find_deleteable_files (files):
+       # Deleteable types regex should come from the config
+       dfiles = []
+       dregex = re.compile ('^.*\.(par2|\d|\d\d\d|rar|r\d\d|zip)$', re.IGNORECASE)
+
+       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 main ():
 
 
 def main ():