X-Git-Url: https://www.irasnyder.com/gitweb/?p=rarslave2.git;a=blobdiff_plain;f=rarslave.py;h=47fbb03e35ebc4ea2c24369f089e28a867ca49a8;hp=7e3881628a44c7ee339f2cb8b488a0622d49f3a1;hb=5ec7a887a528ecf80185ef26a2d5b754cb05ef18;hpb=aebb7adb2c800b1efc7c7d1877a16609cfe76b65 diff --git a/rarslave.py b/rarslave.py index 7e38816..47fbb03 100644 --- a/rarslave.py +++ b/rarslave.py @@ -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)] +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 ():