3 Helper code for file writing with optional compression.
5 @contact: Debian FTPMaster <ftpmaster@debian.org>
6 @copyright: 2011 Torsten Werner <twerner@debian.org>
7 @license: GNU General Public License version 2 or later
10 ################################################################################
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ################################################################################
28 from daklib.config import Config
30 from daklib.daksubprocess import check_call
35 class CompressionMethod(object):
36 def __init__(self, keyword, extension, command):
37 self.keyword = keyword
38 self.extension = extension
39 self.command = command
41 _compression_methods = (
42 CompressionMethod('bzip2', '.bz2', ['bzip2', '-9']),
43 CompressionMethod('gzip', '.gz', ['gzip', '-9cn', '--rsyncable']),
44 CompressionMethod('xz', '.xz', ['xz', '-c']),
45 # 'none' must be the last compression method as BaseFileWriter
46 # handling it will remove the input file for other compressions
47 CompressionMethod('none', '', None),
50 class BaseFileWriter(object):
52 Base class for compressed and uncompressed file writing.
54 def __init__(self, template, **keywords):
56 The template argument is a string template like
57 "dists/%(suite)s/%(component)s/Contents-%(architecture)s.gz" that
58 should be relative to the archive's root directory. The keywords
59 include strings for suite, component, architecture and booleans
60 uncompressed, gzip, bzip2.
62 self.compression = keywords.get('compression', ['none'])
63 self.path = template % keywords
67 Returns a file object for writing.
69 # create missing directories
71 os.makedirs(os.path.dirname(self.path))
74 self.file = open(self.path + '.new', 'w')
77 # internal helper function
78 def rename(self, filename):
79 tempfilename = filename + '.new'
80 os.chmod(tempfilename, 0o644)
81 os.rename(tempfilename, filename)
83 # internal helper function to compress output
84 def compress(self, cmd, suffix, path):
85 in_filename = "{0}.new".format(path)
86 out_filename = "{0}{1}.new".format(path, suffix)
88 with open(in_filename, 'r') as in_fh, open(out_filename, 'w') as out_fh:
89 check_call(cmd, stdin=in_fh, stdout=out_fh)
90 self.rename("{0}{1}".format(path, suffix))
94 Closes the file object and does the compression and rename work.
97 for method in _compression_methods:
98 if method.keyword in self.compression:
99 self.compress(method.command, method.extension, self.path)
101 # Try removing the file that would be generated.
102 # It's not an error if it does not exist.
104 os.unlink("{0}{1}".format(self.path, method.extension))
106 if e.errno != errno.ENOENT:
109 os.unlink(self.path + '.new')
111 class BinaryContentsFileWriter(BaseFileWriter):
112 def __init__(self, **keywords):
114 The value of the keywords suite, component, and architecture are
115 strings. The value of component may be omitted if not applicable.
116 Output files are gzip compressed only.
119 'compression': ['gzip'],
121 flags.update(keywords)
122 if flags['debtype'] == 'deb':
123 template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-%(architecture)s"
125 template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-udeb-%(architecture)s"
126 BaseFileWriter.__init__(self, template, **flags)
128 class SourceContentsFileWriter(BaseFileWriter):
129 def __init__(self, **keywords):
131 The value of the keywords suite and component are strings.
132 Output files are gzip compressed only.
135 'compression': ['gzip'],
137 flags.update(keywords)
138 template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-source"
139 BaseFileWriter.__init__(self, template, **flags)
141 class PackagesFileWriter(BaseFileWriter):
142 def __init__(self, **keywords):
144 The value of the keywords suite, component, debtype and architecture
145 are strings. Output files are gzip compressed only.
148 'compression': ['xz'],
150 flags.update(keywords)
151 if flags['debtype'] == 'deb':
152 template = "%(archive)s/dists/%(suite)s/%(component)s/binary-%(architecture)s/Packages"
154 template = "%(archive)s/dists/%(suite)s/%(component)s/debian-installer/binary-%(architecture)s/Packages"
155 BaseFileWriter.__init__(self, template, **flags)
157 class SourcesFileWriter(BaseFileWriter):
158 def __init__(self, **keywords):
160 The value of the keywords suite and component are strings. Output
161 files are gzip compressed only.
164 'compression': ['xz'],
166 flags.update(keywords)
167 template = "%(archive)s/dists/%(suite)s/%(component)s/source/Sources"
168 BaseFileWriter.__init__(self, template, **flags)
170 class TranslationFileWriter(BaseFileWriter):
171 def __init__(self, **keywords):
173 The value of the keywords suite, component and language are strings.
174 Output files are bzip2 compressed only.
177 'compression': ['bzip2'],
180 flags.update(keywords)
181 template = "%(archive)s/dists/%(suite)s/%(component)s/i18n/Translation-%(language)s"
182 super(TranslationFileWriter, self).__init__(template, **flags)