]> git.decadent.org.uk Git - dak.git/blob - daklib/filewriter.py
Add support for xz compression to FileWriter
[dak.git] / daklib / filewriter.py
1 #!/usr/bin/env python
2 """
3 Helper code for file writing with optional compression.
4
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
8 """
9
10 ################################################################################
11
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.
16
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.
21
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
25
26 ################################################################################
27
28 from daklib.config import Config
29
30 from subprocess import check_call
31
32 import os, os.path
33
34 class BaseFileWriter(object):
35     '''
36     Base class for compressed and uncompressed file writing.
37     '''
38     def __init__(self, template, **keywords):
39         '''
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.
45         '''
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.xz = 'xz' in compression
51         root_dir = Config()['Dir::Root']
52         relative_dir = template % keywords
53         self.path = os.path.join(root_dir, relative_dir)
54
55     def open(self):
56         '''
57         Returns a file object for writing.
58         '''
59         # create missing directories
60         try:
61             os.makedirs(os.path.dirname(self.path))
62         except:
63             pass
64         self.file = open(self.path + '.new', 'w')
65         return self.file
66
67     # internal helper function
68     def rename(self, filename):
69         tempfilename = filename + '.new'
70         os.chmod(tempfilename, 0o664)
71         os.rename(tempfilename, filename)
72
73     def close(self):
74         '''
75         Closes the file object and does the compression and rename work.
76         '''
77         self.file.close()
78         if self.gzip:
79             check_call('gzip -9cn --rsyncable <%s.new >%s.gz.new' % (self.path, self.path),
80                 shell = True)
81             self.rename('%s.gz' % self.path)
82         if self.bzip2:
83             check_call('bzip2 -9 <%s.new >%s.bz2.new' % (self.path, self.path), shell = True)
84             self.rename('%s.bz2' % self.path)
85         if self.xz:
86             check_call('xz -c <{0}.new >{0}.xz.new'.format(self.path), shell=True)
87             self.rename('{0}.xz'.format(self.path))
88         if self.uncompressed:
89             self.rename(self.path)
90         else:
91             os.unlink(self.path + '.new')
92
93 class BinaryContentsFileWriter(BaseFileWriter):
94     def __init__(self, **keywords):
95         '''
96         The value of the keywords suite, component, and architecture are
97         strings. The value of component may be omitted if not applicable.
98         Output files are gzip compressed only.
99         '''
100         flags = {
101             'compression': ['gzip'],
102         }
103         flags.update(keywords)
104         if flags['debtype'] == 'deb':
105             template = "dists/%(suite)s/%(component)s/Contents-%(architecture)s"
106         else: # udeb
107             template = "dists/%(suite)s/%(component)s/Contents-udeb-%(architecture)s"
108         BaseFileWriter.__init__(self, template, **flags)
109
110 class SourceContentsFileWriter(BaseFileWriter):
111     def __init__(self, **keywords):
112         '''
113         The value of the keywords suite and component are strings.
114         Output files are gzip compressed only.
115         '''
116         flags = {
117             'compression': ['gzip'],
118         }
119         flags.update(keywords)
120         template = "dists/%(suite)s/%(component)s/Contents-source"
121         BaseFileWriter.__init__(self, template, **flags)
122
123 class PackagesFileWriter(BaseFileWriter):
124     def __init__(self, **keywords):
125         '''
126         The value of the keywords suite, component, debtype and architecture
127         are strings.  Output files are gzip compressed only.
128         '''
129         flags = {
130             'compression': ['gzip', 'bzip2'],
131         }
132         flags.update(keywords)
133         if flags['debtype'] == 'deb':
134             template = "dists/%(suite)s/%(component)s/binary-%(architecture)s/Packages"
135         else: # udeb
136             template = "dists/%(suite)s/%(component)s/debian-installer/binary-%(architecture)s/Packages"
137         BaseFileWriter.__init__(self, template, **flags)
138
139 class SourcesFileWriter(BaseFileWriter):
140     def __init__(self, **keywords):
141         '''
142         The value of the keywords suite and component are strings. Output
143         files are gzip compressed only.
144         '''
145         flags = {
146             'compression': ['gzip', 'bzip2'],
147         }
148         flags.update(keywords)
149         template = "dists/%(suite)s/%(component)s/source/Sources"
150         BaseFileWriter.__init__(self, template, **flags)
151
152 class TranslationFileWriter(BaseFileWriter):
153     def __init__(self, **keywords):
154         '''
155         The value of the keywords suite, component and language are strings.
156         Output files are bzip2 compressed only.
157         '''
158         flags = {
159             'compression': ['bzip2'],
160             'language':     'en',
161         }
162         flags.update(keywords)
163         template = "dists/%(suite)s/%(component)s/i18n/Translation-%(language)s"
164         super(TranslationFileWriter, self).__init__(template, **flags)