6532de57de81ce6d34a5a88f5f42018450d66be0
[rarslave2.git] / rarslave.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=112 :
3
4 import re, os, sys
5
6 def get_basename (name):
7         """Strips most kinds of endings from a filename"""
8
9         regex = '^(.+)\.(par2|vol\d+\+\d+|\d\d\d|part\d+|rar|zip|avi|mp4|mkv|ogm)$'
10         r = re.compile (regex, re.IGNORECASE)
11         done = False
12
13         while not done:
14                 done = True
15
16                 if r.match (name):
17                         g = r.match (name).groups()
18                         name = g[0]
19                         done = False
20
21         return name
22
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"""
26
27         if not os.path.isdir (os.path.abspath (dir)):
28                 raise ValueError # bad directory given
29
30         dir = os.path.abspath (dir)
31         ename = re.escape (name)
32         regex = re.compile ('^%s.*$' % (ename, ))
33
34         return [f for f in os.listdir (dir) if regex.match (f)]
35
36 def find_all_par2_files (dir):
37         """Finds all par2 files in a directory"""
38
39         if not os.path.isdir (os.path.abspath (dir)):
40                 raise ValueError # bad directory given
41
42         dir = os.path.abspath (dir)
43         regex = re.compile ('^.*\.par2$', re.IGNORECASE)
44
45         # Find all files
46         return [f for f in os.listdir (dir) if regex.match (f)]
47
48 def has_extension (f, ext):
49         """Checks if f has the extension ext"""
50
51         if ext[0] != '.':
52                 ext = '.' + ext
53
54         ext = re.escape (ext)
55         regex = re.compile ('^.*%s$' % (ext, ), re.IGNORECASE)
56         return regex.match (f)
57
58 def find_extraction_heads (files):
59         """Takes a list of possible files and finds likely heads of
60            extraction."""
61
62         # NOTE: perhaps this should happen AFTER repair is
63         # NOTE: successful. That way all files would already exist
64
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 
69
70         heads = []
71
72         # Old RAR type, find all files ending in .rar
73         if is_oldrar (files):
74                 regex = re.compile ('^.*\.rar$', re.IGNORECASE)
75                 for f in files:
76                         if regex.match (f):
77                                 heads.append (f)
78
79                 return heads
80
81         if is_newrar (files):
82                 regex = re.compile ('^.*\.part01.rar$', re.IGNORECASE)
83                 for f in files:
84                         if regex.match (f):
85                                 heads.append (f)
86
87                 return heads
88
89         if is_zip (files):
90                 regex = re.compile ('^.*\.zip$', re.IGNORECASE)
91                 for f in files:
92                         if regex.match (f):
93                                 heads.append (f)
94
95                 return heads
96
97         # Not a type we know yet
98         raise ValueError
99
100
101 def is_oldrar (files):
102         for f in files:
103                 if has_extension (f, '.r00'):
104                         return True
105
106 def is_newrar (files):
107         for f in files:
108                 if has_extension (f, '.part01.rar'):
109                         return True
110
111 def is_zip (files):
112         for f in files:
113                 if has_extension (f, '.zip'):
114                         return True
115
116
117 def main ():
118         print find_all_par2_files ('/home/irasnyd/downloads/test_material/01/')
119
120 if __name__ == '__main__':
121         main ()