Add support for Join sets where the parity protects the split files
[rarslave2.git] / PAR2Set / JoinProtected.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
4 """
5 Holds the JoinProtected class.
6
7 This module works with joined sets where the joined files are
8 protected directly by the PAR2 files.
9
10 It will detect sets like the following:
11 X.par2
12 X.vol0+1.par2
13 ...
14
15 X.001
16 X.002
17 ...
18
19 Where the PAR2 files protect a file named X.001, etc. It works in the case
20 that the PAR2 files are protecting the X.001, etc. files directly.
21
22 It currently whitelists a number of media formats that can be protected.
23 """
24
25 __author__    = "Ira W. Snyder (devel@irasnyder.com)"
26 __copyright__ = "Copyright (c) 2014 Ira W. Snyder (devel@irasnyder.com)"
27 __license__   = "GNU GPL v2 (or, at your option, any later version)"
28
29 #    JoinProtected.py
30 #
31 #    Copyright (C) 2014  Ira W. Snyder (devel@irasnyder.com)
32 #
33 #    This program is free software; you can redistribute it and/or modify
34 #    it under the terms of the GNU General Public License as published by
35 #    the Free Software Foundation; either version 2 of the License, or
36 #    (at your option) any later version.
37 #
38 #    This program is distributed in the hope that it will be useful,
39 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
40 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41 #    GNU General Public License for more details.
42 #
43 #    You should have received a copy of the GNU General Public License
44 #    along with this program; if not, write to the Free Software
45 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
46
47 import os, re, logging
48 from PAR2Set import Base, utils
49
50 class JoinProtected(Base):
51
52     ############################################################################
53
54     # find all prefixes in the protected set by stripping the extension off
55     # of each file
56     def getPrefixes(self):
57
58         prefixes = set()
59         for f in self.protectedFiles:
60             prefix, extension = os.path.splitext(f)
61             prefixes.add(prefix)
62
63         return list(prefixes)
64
65
66     ############################################################################
67
68     def detect(self):
69
70         # check to see that there are joined files in the set
71         regex = r'^.*\.\d\d\d$'
72         if not utils.hasAMatch(regex, self.protectedFiles):
73             raise TypeError
74
75         # This is a good match if this criteria is met
76         prefixes = self.getPrefixes()
77         if len(prefixes) == 1:
78             return
79
80         raise TypeError
81
82     ############################################################################
83
84     def repair(self):
85
86         regex = r'^.*\.\d\d\d$'
87         files = utils.findMatches(regex, self.protectedFiles)
88         utils.runCommand(['par2repair'] + self.PAR2Files + files, self.directory)
89
90     ############################################################################
91
92     def extract(self):
93
94         # we guaranteed there is exactly one prefix during detection
95         filename = self.getPrefixes()[0]
96
97         regex = r'^.*\.\d\d\d$'
98         files = utils.findMatches(regex, self.protectedFiles)
99
100         # concatenate all '.001' files into a single output file
101         with open(filename, 'wb') as f:
102             utils.runCommand(['cat'] + files, self.directory, stdout=f)
103
104     ############################################################################
105
106     def findDeletableFiles(self):
107
108         prefixes = self.getPrefixes()
109
110         files = Base.findDeletableFiles(self)
111         files = [f for f in files if f not in prefixes]
112
113         return files
114
115     ############################################################################
116