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 self.uncompressed = keywords.get('uncompressed', True)
47 self.gzip = keywords.get('gzip', False)
48 self.bzip2 = keywords.get('bzip2', False)
49 root_dir = Config()['Dir::Root']
50 relative_dir = template % keywords
51 self.path = os.path.join(root_dir, relative_dir)
55 Returns a file object for writing.
57 self.file = open(self.path + '.new', 'w')
60 # internal helper function
61 def rename(self, filename):
62 tempfilename = filename + '.new'
63 os.chmod(tempfilename, 0664)
64 os.rename(tempfilename, filename)
68 Closes the file object and does the compression and rename work.
72 check_call('gzip --rsyncable <%s.new >%s.gz.new' % (self.path, self.path),
74 self.rename('%s.gz' % self.path)
76 check_call('bzip2 <%s.new >%s.bz2.new' % (self.path, self.path), shell = True)
77 self.rename('%s.bz2' % self.path)
79 self.rename(self.path)
81 os.unlink(self.path + '.new')
83 class BinaryContentsFileWriter(BaseFileWriter):
84 def __init__(self, **keywords):
86 The value of the keywords suite, component, and architecture are
87 strings. The value of component may be omitted if not applicable.
88 Output files are gzip compressed only.
91 'uncompressed': False,
95 flags.update(keywords)
96 if 'component' in flags:
97 template = "dists/%(suite)s/%(component)s/Contents-%(architecture)s"
99 template = "dists/%(suite)s/Contents-%(architecture)s"
100 BaseFileWriter.__init__(self, template, **flags)
102 class SourceContentsFileWriter(BaseFileWriter):
103 def __init__(self, **keywords):
105 The value of the keywords suite and component are strings.
106 Output files are gzip compressed only.
109 'uncompressed': False,
113 flags.update(keywords)
114 template = "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 'uncompressed': False,
128 flags.update(keywords)
129 if flags['debtype'] == 'deb':
130 template = "dists/%(suite)s/%(component)s/binary-%(architecture)s/Packages"
132 template = "dists/%(suite)s/%(component)s/debian-installer/binary-%(architecture)s/Packages"
133 BaseFileWriter.__init__(self, template, **flags)
135 class SourcesFileWriter(BaseFileWriter):
136 def __init__(self, **keywords):
138 The value of the keywords suite and component are strings. Output
139 files are gzip compressed only.
142 'uncompressed': False,
146 flags.update(keywords)
147 template = "dists/%(suite)s/%(component)s/source/Sources"
148 BaseFileWriter.__init__(self, template, **flags)