Initial commit of the rarslave project.
[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
49
50 def main ():
51         print find_all_par2_files ('/home/irasnyd/downloads/test_material/01/')
52
53 if __name__ == '__main__':
54         main ()