2 # vim: set ts=4 sts=4 sw=4 textwidth=112 :
6 def get_basename (name):
7 """Strips most kinds of endings from a filename"""
9 regex = '^(.+)\.(par2|vol\d+\+\d+|\d\d\d|part\d+|rar|zip|avi|mp4|mkv|ogm)$'
10 r = re.compile (regex, re.IGNORECASE)
17 g = r.match (name).groups()
23 def find_likely_files (name, dir):
24 """Finds files which are likely to be part of the set corresponding
25 to $name in the directory $dir"""
27 if not os.path.isdir (os.path.abspath (dir)):
28 raise ValueError # bad directory given
30 dir = os.path.abspath (dir)
31 ename = re.escape (name)
32 regex = re.compile ('^%s.*$' % (ename, ))
34 return [f for f in os.listdir (dir) if regex.match (f)]
36 def find_all_par2_files (dir):
37 """Finds all par2 files in a directory"""
39 if not os.path.isdir (os.path.abspath (dir)):
40 raise ValueError # bad directory given
42 dir = os.path.abspath (dir)
43 regex = re.compile ('^.*\.par2$', re.IGNORECASE)
46 return [f for f in os.listdir (dir) if regex.match (f)]
48 def has_extension (f, ext):
49 """Checks if f has the extension ext"""
55 regex = re.compile ('^.*%s$' % (ext, ), re.IGNORECASE)
56 return regex.match (f)
58 def find_extraction_heads (files):
59 """Takes a list of possible files and finds likely heads of
62 # NOTE: perhaps this should happen AFTER repair is
63 # NOTE: successful. That way all files would already exist
65 # According to various sources online:
66 # 1) pre rar-3.0: .rar .r00 .r01 ...
67 # 2) post rar-3.0: .part01.rar .part02.rar
68 # 3) zip all ver: .zip
72 # Old RAR type, find all files ending in .rar
74 regex = re.compile ('^.*\.rar$', re.IGNORECASE)
82 regex = re.compile ('^.*\.part01.rar$', re.IGNORECASE)
90 regex = re.compile ('^.*\.zip$', re.IGNORECASE)
97 # Not a type we know yet
101 def is_oldrar (files):
103 if has_extension (f, '.r00'):
106 def is_newrar (files):
108 if has_extension (f, '.part01.rar'):
113 if has_extension (f, '.zip'):
118 print find_all_par2_files ('/home/irasnyd/downloads/test_material/01/')
120 if __name__ == '__main__':