Major Projectwide Restructuring
[rarslave2.git] / PAR2Set_JOIN.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
4 import PAR2Set
5 from RarslaveCommon import *
6
7 #
8 # This is a regular joined file type
9 #
10 # It will detect sets like the following:
11 # X.par2
12 # X.vol0+1.par2
13 # X.vol1+2.par2
14 # X.001
15 # X.002
16 # X.003
17 #
18 # Where the PAR2 files protect a file named X.avi (or similar). It will not
19 # work where the PAR2 files are protecting the .001, etc files directly.
20 #
21
22 def detect_JOIN (name_files, prot_files):
23         return has_a_match ('^.*\.\d\d\d$', name_files)
24
25
26 class PAR2Set_JOIN (PAR2Set.PAR2Set):
27
28         def __repr__ (self):
29                 return 'JOIN'
30
31         def find_joinfiles (self):
32                 return find_matches ('^.*\.\d\d\d$', self.name_matched_files)
33
34         def runVerifyAndRepair (self):
35                 PAR2_CMD = config_get_value ('commands', 'par2repair')
36
37                 # assemble the command
38                 # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
39                 command = "%s \"%s\" " % (PAR2_CMD, self.p2file)
40
41                 for f in self.all_p2files:
42                         if f != self.p2file:
43                                 command += "\"%s\" " % os.path.split (f)[1]
44
45                 for f in self.find_joinfiles ():
46                         command += "\"%s\" " % os.path.split (f)[1]
47
48                 # run the command
49                 ret = run_command (command, self.dir)
50
51                 # check the result
52                 if ret != 0:
53                         fatalMessage ('PAR2 Check / Repair failed: %s' % self.p2file)
54                         return -ECHECK
55
56                 return SUCCESS
57
58         def find_extraction_heads (self):
59                 return self.prot_matched_files
60
61         def extraction_function (self, file, todir):
62                 """Extract a single file of this type to the given directory"""
63
64                 NOEXTRACT_CMD = config_get_value ('commands', 'noextract')
65
66                 # Make sure that both files are not the same file. If they are, don't run at all.
67                 if os.path.samefile (file, os.path.join (todir, file)):
68                         return SUCCESS
69
70                 cmd = NOEXTRACT_CMD % (file, todir)
71                 ret = run_command (cmd)
72
73                 # Check error code
74                 if ret != 0:
75                         return -EEXTRACT
76
77                 return SUCCESS
78