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