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