Add Copyright / License information + Documentation
[rarslave2.git] / PAR2Set / ZIP.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
4 """
5 Holds the ZIP class.
6
7 This module works with regular zip file sets.
8
9 It will detect sets like the following:
10 X.par2
11 X.vol0+1.par2
12 ...
13
14 Y.zip
15 ...
16
17 Where the PAR2 files protect the Y.zip file(s). Note that Y can be equal to
18 X, but is not required to be.
19 """
20
21 __author__    = "Ira W. Snyder (devel@irasnyder.com)"
22 __copyright__ = "Copyright (c) 2006,2007 Ira W. Snyder (devel@irasnyder.com)"
23 __license__   = "GNU GPL v2 (or, at your option, any later version)"
24
25 #    ZIP.py -- detect and work with zip sets
26 #
27 #    Copyright (C) 2006,2007  Ira W. Snyder (devel@irasnyder.com)
28 #
29 #    This program is free software; you can redistribute it and/or modify
30 #    it under the terms of the GNU General Public License as published by
31 #    the Free Software Foundation; either version 2 of the License, or
32 #    (at your option) any later version.
33 #
34 #    This program is distributed in the hope that it will be useful,
35 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
36 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37 #    GNU General Public License for more details.
38 #
39 #    You should have received a copy of the GNU General Public License
40 #    along with this program; if not, write to the Free Software
41 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
42
43 import PAR2Set.Base
44 import rsutil.common
45
46
47 def detector (name_files, prot_files):
48         """Detect a zip set"""
49
50         all_files = rsutil.common.no_duplicates (name_files + prot_files)
51         return rsutil.common.has_a_match ('^.*\.zip$', all_files)
52
53
54 class ZIP (PAR2Set.Base.Base):
55
56         """Class for working with normal zip sets"""
57
58         def __repr__ (self):
59                 return 'ZIP'
60
61         def find_extraction_heads (self):
62                 """Find the heads of extraction for a zip set"""
63
64                 return rsutil.common.find_matches ('^.*\.zip', self.all_files)
65
66         def extraction_function (self, file, todir):
67                 """Extract a single zip file to the given directory.
68
69                    file -- the file to extract
70                    todir -- the directory to extract into"""
71
72                 ZIP_CMD = rsutil.common.config_get_value ('commands', 'unzip')
73
74                 cmd = ZIP_CMD % (file, todir)
75                 ret = rsutil.common.run_command (cmd)
76
77                 # Check error code
78                 if ret != 0:
79                         return -rsutil.common.EEXTRACT
80
81                 return rsutil.common.SUCCESS
82