]> git.decadent.org.uk Git - dak.git/blob - daklib/filewriter.py
Remove files that are (no longer) generated
[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 daklib.daksubprocess import check_call
31
32 import errno
33 import os, os.path
34
35 class CompressionMethod(object):
36     def __init__(self, keyword, extension, command):
37         self.keyword = keyword
38         self.extension = extension
39         self.command = command
40
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),
48 )
49
50 class BaseFileWriter(object):
51     '''
52     Base class for compressed and uncompressed file writing.
53     '''
54     def __init__(self, template, **keywords):
55         '''
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.
61         '''
62         self.compression = keywords.get('compression', ['none'])
63         self.path = template % keywords
64
65     def open(self):
66         '''
67         Returns a file object for writing.
68         '''
69         # create missing directories
70         try:
71             os.makedirs(os.path.dirname(self.path))
72         except:
73             pass
74         self.file = open(self.path + '.new', 'w')
75         return self.file
76
77     # internal helper function
78     def rename(self, filename):
79         tempfilename = filename + '.new'
80         os.chmod(tempfilename, 0o644)
81         os.rename(tempfilename, filename)
82
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)
87         if cmd is not None:
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))
91
92     def close(self):
93         '''
94         Closes the file object and does the compression and rename work.
95         '''
96         self.file.close()
97         for method in _compression_methods:
98             if method.keyword in self.compression:
99                 self.compress(method.command, method.extension, self.path)
100             else:
101                 # Try removing the file that would be generated.
102                 # It's not an error if it does not exist.
103                 try:
104                     os.unlink("{0}{1}".format(self.path, method.extension))
105                 except OSError as e:
106                     if e.errno != errno.ENOENT:
107                         raise
108         else:
109             os.unlink(self.path + '.new')
110
111 class BinaryContentsFileWriter(BaseFileWriter):
112     def __init__(self, **keywords):
113         '''
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.
117         '''
118         flags = {
119             'compression': ['gzip'],
120         }
121         flags.update(keywords)
122         if flags['debtype'] == 'deb':
123             template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-%(architecture)s"
124         else: # udeb
125             template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-udeb-%(architecture)s"
126         BaseFileWriter.__init__(self, template, **flags)
127
128 class SourceContentsFileWriter(BaseFileWriter):
129     def __init__(self, **keywords):
130         '''
131         The value of the keywords suite and component are strings.
132         Output files are gzip compressed only.
133         '''
134         flags = {
135             'compression': ['gzip'],
136         }
137         flags.update(keywords)
138         template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-source"
139         BaseFileWriter.__init__(self, template, **flags)
140
141 class PackagesFileWriter(BaseFileWriter):
142     def __init__(self, **keywords):
143         '''
144         The value of the keywords suite, component, debtype and architecture
145         are strings.  Output files are gzip compressed only.
146         '''
147         flags = {
148             'compression': ['gzip', 'xz'],
149         }
150         flags.update(keywords)
151         if flags['debtype'] == 'deb':
152             template = "%(archive)s/dists/%(suite)s/%(component)s/binary-%(architecture)s/Packages"
153         else: # udeb
154             template = "%(archive)s/dists/%(suite)s/%(component)s/debian-installer/binary-%(architecture)s/Packages"
155         BaseFileWriter.__init__(self, template, **flags)
156
157 class SourcesFileWriter(BaseFileWriter):
158     def __init__(self, **keywords):
159         '''
160         The value of the keywords suite and component are strings. Output
161         files are gzip compressed only.
162         '''
163         flags = {
164             'compression': ['gzip', 'xz'],
165         }
166         flags.update(keywords)
167         template = "%(archive)s/dists/%(suite)s/%(component)s/source/Sources"
168         BaseFileWriter.__init__(self, template, **flags)
169
170 class TranslationFileWriter(BaseFileWriter):
171     def __init__(self, **keywords):
172         '''
173         The value of the keywords suite, component and language are strings.
174         Output files are bzip2 compressed only.
175         '''
176         flags = {
177             'compression': ['bzip2'],
178             'language':     'en',
179         }
180         flags.update(keywords)
181         template = "%(archive)s/dists/%(suite)s/%(component)s/i18n/Translation-%(language)s"
182         super(TranslationFileWriter, self).__init__(template, **flags)