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