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 subprocess import check_call
34 class BaseFileWriter(object):
36 Base class for compressed and uncompressed file writing.
38 def __init__(self, template, **keywords):
40 The template argument is a string template like
41 "dists/%(suite)s/%(component)s/Contents-%(architecture)s.gz" that
42 should be relative to the archive's root directory. The keywords
43 include strings for suite, component, architecture and booleans
44 uncompressed, gzip, bzip2.
46 compression = keywords.get('compression', ['none'])
47 self.uncompressed = 'none' in compression
48 self.gzip = 'gzip' in compression
49 self.bzip2 = 'bzip2' in compression
50 self.path = template % keywords
54 Returns a file object for writing.
56 # create missing directories
58 os.makedirs(os.path.dirname(self.path))
61 self.file = open(self.path + '.new', 'w')
64 # internal helper function
65 def rename(self, filename):
66 tempfilename = filename + '.new'
67 os.chmod(tempfilename, 0o664)
68 os.rename(tempfilename, filename)
72 Closes the file object and does the compression and rename work.
76 check_call('gzip -9cn --rsyncable <%s.new >%s.gz.new' % (self.path, self.path),
78 self.rename('%s.gz' % self.path)
80 check_call('bzip2 -9 <%s.new >%s.bz2.new' % (self.path, self.path), shell = True)
81 self.rename('%s.bz2' % self.path)
83 self.rename(self.path)
85 os.unlink(self.path + '.new')
87 class BinaryContentsFileWriter(BaseFileWriter):
88 def __init__(self, **keywords):
90 The value of the keywords suite, component, and architecture are
91 strings. The value of component may be omitted if not applicable.
92 Output files are gzip compressed only.
95 'compression': ['gzip'],
97 flags.update(keywords)
98 if flags['debtype'] == 'deb':
99 template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-%(architecture)s"
101 template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-udeb-%(architecture)s"
102 BaseFileWriter.__init__(self, template, **flags)
104 class SourceContentsFileWriter(BaseFileWriter):
105 def __init__(self, **keywords):
107 The value of the keywords suite and component are strings.
108 Output files are gzip compressed only.
111 'compression': ['gzip'],
113 flags.update(keywords)
114 template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-source"
115 BaseFileWriter.__init__(self, template, **flags)
117 class PackagesFileWriter(BaseFileWriter):
118 def __init__(self, **keywords):
120 The value of the keywords suite, component, debtype and architecture
121 are strings. Output files are gzip compressed only.
124 'compression': ['gzip', 'bzip2'],
126 flags.update(keywords)
127 if flags['debtype'] == 'deb':
128 template = "%(archive)s/dists/%(suite)s/%(component)s/binary-%(architecture)s/Packages"
130 template = "%(archive)s/dists/%(suite)s/%(component)s/debian-installer/binary-%(architecture)s/Packages"
131 BaseFileWriter.__init__(self, template, **flags)
133 class SourcesFileWriter(BaseFileWriter):
134 def __init__(self, **keywords):
136 The value of the keywords suite and component are strings. Output
137 files are gzip compressed only.
140 'compression': ['gzip', 'bzip2'],
142 flags.update(keywords)
143 template = "%(archive)s/dists/%(suite)s/%(component)s/source/Sources"
144 BaseFileWriter.__init__(self, template, **flags)
146 class TranslationFileWriter(BaseFileWriter):
147 def __init__(self, **keywords):
149 The value of the keywords suite, component and language are strings.
150 Output files are bzip2 compressed only.
153 'compression': ['bzip2'],
156 flags.update(keywords)
157 template = "%(archive)s/dists/%(suite)s/%(component)s/i18n/Translation-%(language)s"
158 super(TranslationFileWriter, self).__init__(template, **flags)