1dd680a668998482bded1b824eff12d2bafdda46
[rarslave2.git] / PAR2Set / Join.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
4 """
5 Holds the Join class.
6
7 This module works with normal joined sets.
8
9 It will detect sets like the following:
10 X.par2
11 X.vol0+1.par2
12 ...
13
14 X.001
15 X.002
16 ...
17
18 Where the PAR2 files protect a file named X.avi, or similar other types.
19 It will not work where the PAR2 files are protecting the X.001, etc files
20 directly.
21 """
22
23 __author__    = "Ira W. Snyder (devel@irasnyder.com)"
24 __copyright__ = "Copyright (c) 2006,2007 Ira W. Snyder (devel@irasnyder.com)"
25 __license__   = "GNU GPL v2 (or, at your option, any later version)"
26
27 #    Join.py
28 #
29 #    Copyright (C) 2006,2007  Ira W. Snyder (devel@irasnyder.com)
30 #
31 #    This program is free software; you can redistribute it and/or modify
32 #    it under the terms of the GNU General Public License as published by
33 #    the Free Software Foundation; either version 2 of the License, or
34 #    (at your option) any later version.
35 #
36 #    This program is distributed in the hope that it will be useful,
37 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
38 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39 #    GNU General Public License for more details.
40 #
41 #    You should have received a copy of the GNU General Public License
42 #    along with this program; if not, write to the Free Software
43 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
44
45 import re
46 import os
47 import logging
48 import PAR2Set.Base
49 import rsutil.common
50
51
52 def detector (name_files, prot_files):
53         """Detects a Join set"""
54
55         return rsutil.common.has_a_match ('^.*\.\d\d\d$', name_files) \
56                         and not rsutil.common.has_a_match ('^.*\.\d\d\d$', prot_files)
57
58
59 class Join (PAR2Set.Base.Base):
60
61         """Class for normal joined-file sets"""
62
63         def __repr__ (self):
64                 return 'JOIN'
65
66         def find_joinfiles (self):
67                 """Finds files which contain data to be joined together"""
68
69                 return rsutil.common.find_matches ('^.*\.\d\d\d$', self.name_matched_files)
70
71         def runVerifyAndRepair (self):
72                 """Verify and Repair a PAR2Set. This version extends the PAR2Set.Base.Base
73                    version by adding the datafiles to be joined at the end of the command
74                    line.
75
76                    This is done using the par2repair command by default"""
77
78                 rsutil.common.run_command(['par2repair'] + self.all_p2files + self.find_joinfiles(), self.dir)
79
80         def find_deleteable_files (self):
81                 """Find all files which are deletable by using the regular expression from the
82                    configuration file"""
83
84                 DELETE_REGEX = rsutil.common.config_get_value ('regular expressions', 'delete_regex')
85                 dregex = re.compile (DELETE_REGEX, re.IGNORECASE)
86
87                 return [f for f in self.all_files if dregex.match (f) and \
88                                 f not in self.prot_matched_files]
89
90         def find_extraction_heads (self):
91                 """Find the extraction heads. Since this should not be an extractable set,
92                    we return the files which are protected directly by the PAR2 files."""
93
94                 return self.prot_matched_files
95
96         def extraction_function (self, file, todir):
97                 """Extract a single file of the Join type.
98
99                    file -- the file to extract
100                    todir -- the directory to extract to
101
102                    This command ignores the extraction if file and todir+file are the same
103                    file. This keeps things like mv working smoothly."""
104
105                 # The Join type doesn't need any extraction
106                 pass