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