]> git.decadent.org.uk Git - dak.git/blob - dak/generate_packages_sources.py
daklib/queue.py: Remove unneeded imports.
[dak.git] / dak / generate_packages_sources.py
1 #!/usr/bin/env python
2
3 """ Generate Packages/Sources files
4
5 @contact: Debian FTPMaster <ftpmaster@debian.org>
6 @copyright: 2000, 2001, 2002, 2006  James Troup <james@nocrew.org>
7 @copyright: 2009  Mark Hymers <mhy@debian.org>
8 @copyright: 2010  Joerg Jaspert <joerg@debian.org>
9 @license: GNU General Public License version 2 or later
10
11 """
12
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27 ################################################################################
28
29 import os
30 import os.path
31 import sys
32 import apt_pkg
33 from tempfile import mkstemp, mkdtemp
34 import commands
35 from multiprocessing import Pool, TimeoutError
36
37 from daklib import daklog
38 from daklib.dbconn import *
39 from daklib.config import Config
40
41 ################################################################################
42
43 Options = None                 #: Commandline arguments parsed into this
44 Logger = None                  #: Our logging object
45 results = []                   #: Results of the subprocesses
46
47 ################################################################################
48
49 def usage (exit_code=0):
50     print """Usage: dak generate-packages-sources [OPTIONS]
51 Generate the Packages/Sources files
52
53   -s, --suite=SUITE(s)       process this suite
54                              Default: All suites not marked 'untouchable'
55   -f, --force                Allow processing of untouchable suites
56                              CAREFUL: Only to be used at point release time!
57   -h, --help                 show this help and exit
58
59 SUITE can be a space seperated list, e.g.
60    --suite=unstable testing
61   """
62
63     sys.exit(exit_code)
64
65 ################################################################################
66
67 def generate_packages_sources(arch, suite, tmppath):
68     """
69     Generate Packages/Sources files with apt-ftparchive for the given suite/arch
70
71     @type suite: string
72     @param suite: Suite name
73
74     @type arch: string
75     @param arch: Architecture name
76
77     @type tmppath: string
78     @param tmppath: The temporary path to work ing
79     """
80
81     DAILY_APT_CONF="""
82 Dir
83 {
84    ArchiveDir "/srv/ftp-master.debian.org/ftp/";
85    OverrideDir "/srv/ftp-master.debian.org/scripts/override/";
86    CacheDir "/srv/ftp-master.debian.org/database/";
87 };
88
89 Default
90 {
91    Packages::Compress "bzip2 gzip";
92    Sources::Compress "bzip2 gzip";
93    Contents::Compress "gzip";
94    DeLinkLimit 0;
95    MaxContentsChange 25000;
96    FileMode 0664;
97 }
98
99 TreeDefault
100 {
101    Contents::Header "/srv/ftp-master.debian.org/dak/config/debian/Contents.top";
102 };
103
104 """
105
106     apt_trees={}
107     apt_trees["di"]={}
108
109     apt_trees["oldstable"]="""
110 tree "dists/oldstable"
111 {
112    FileList "/srv/ftp-master.debian.org/database/dists/oldstable_$(SECTION)_binary-$(ARCH).list";
113    SourceFileList "/srv/ftp-master.debian.org/database/dists/oldstable_$(SECTION)_source.list";
114    Sections "main contrib non-free";
115    Architectures "%(arch)s";
116    BinOverride "override.squeeze.$(SECTION)";
117    ExtraOverride "override.squeeze.extra.$(SECTION)";
118    SrcOverride "override.squeeze.$(SECTION).src";
119 };
120 """
121
122     apt_trees["di"]["oldstable"]="""
123 tree "dists/oldstable/main"
124 {
125    FileList "/srv/ftp-master.debian.org/database/dists/oldstable_main_$(SECTION)_binary-$(ARCH).list";
126    Sections "debian-installer";
127    Architectures "%(arch)s";
128    BinOverride "override.squeeze.main.$(SECTION)";
129    SrcOverride "override.squeeze.main.src";
130    BinCacheDB "packages-debian-installer-$(ARCH).db";
131    Packages::Extensions ".udeb";
132    %(contentsline)s
133 };
134
135 tree "dists/oldstable/non-free"
136 {
137    FileList "/srv/ftp-master.debian.org/database/dists/oldstable_non-free_$(SECTION)_binary-$(ARCH).list";
138    Sections "debian-installer";
139    Architectures "%(arch)s";
140    BinOverride "override.squeeze.main.$(SECTION)";
141    SrcOverride "override.squeeze.main.src";
142    BinCacheDB "packages-debian-installer-$(ARCH).db";
143    Packages::Extensions ".udeb";
144    %(contentsline)s
145 };
146 """
147
148
149     cnf = Config()
150     try:
151         # Write apt.conf
152         (ac_fd, ac_name) = mkstemp(dir=tmppath, suffix=suite, prefix=arch)
153         os.write(ac_fd, DAILY_APT_CONF)
154         # here we want to generate the tree entries
155         os.write(ac_fd, apt_trees[suite] % {'arch': arch})
156         # this special casing needs to go away, but this whole thing may just want an
157         # aptconfig class anyways
158         if arch != 'source':
159             if arch == 'hurd-i386' and suite == 'experimental':
160                 pass
161             elif apt_trees["di"].has_key(suite):
162                 if arch == "amd64":
163                     os.write(ac_fd, apt_trees["di"][suite] %
164                              {'arch': arch, 'contentsline': 'Contents "$(DIST)/../Contents-udeb";'})
165                 else:
166                     os.write(ac_fd, apt_trees["di"][suite] % {'arch': arch, 'contentsline': ''})
167         os.close(ac_fd)
168
169         print "Going to run apt-ftparchive for %s/%s" % (arch, suite)
170         # Run apt-ftparchive generate
171         # We dont want to add a -q or -qq here, this output should go into our logs, sometimes
172         # it has errormessages we like to see
173         os.environ['GZIP'] = '--rsyncable'
174         os.chdir(tmppath)
175         (result, output) = commands.getstatusoutput('apt-ftparchive -o APT::FTPArchive::Contents=off -o APT::FTPArchive::SHA512=off generate %s' % os.path.basename(ac_name))
176         sn="a-f %s,%s: " % (suite, arch)
177         print sn + output.replace('\n', '\n%s' % (sn))
178         return result
179
180     # Clean up any left behind files
181     finally:
182         if ac_fd:
183             try:
184                 os.close(ac_fd)
185             except OSError:
186                 pass
187
188         if ac_name:
189             try:
190                 os.unlink(ac_name)
191             except OSError:
192                 pass
193
194 def sname(arch):
195     return arch.arch_string
196
197 def get_result(arg):
198     global results
199     if arg:
200         results.append(arg)
201
202 ########################################################################
203 ########################################################################
204
205 def main ():
206     global Options, Logger, results
207
208     cnf = Config()
209
210     for i in ["Help", "Suite", "Force"]:
211         if not cnf.has_key("Generate-Packages-Sources::Options::%s" % (i)):
212             cnf["Generate-Packages-Sources::Options::%s" % (i)] = ""
213
214     Arguments = [('h',"help","Generate-Packages-Sources::Options::Help"),
215                  ('s',"suite","Generate-Packages-Sources::Options::Suite"),
216                  ('f',"force","Generate-Packages-Sources::Options::Force")]
217
218     suite_names = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
219     Options = cnf.subtree("Generate-Packages-Sources::Options")
220
221     if Options["Help"]:
222         usage()
223
224     Logger = daklog.Logger('generate-packages-sources')
225
226     session = DBConn().session()
227
228     if Options["Suite"]:
229         # Something here
230         suites = []
231         for s in suite_names:
232             suite = get_suite(s.lower(), session)
233             if suite:
234                 suites.append(suite)
235             else:
236                 print "cannot find suite %s" % s
237                 Logger.log(['cannot find suite %s' % s])
238     else:
239         suites=session.query(Suite).filter(Suite.untouchable == False).all()
240
241     startdir = os.getcwd()
242     os.chdir(cnf["Dir::TempPath"])
243
244     broken=[]
245     # For each given suite, each architecture, run one apt-ftparchive
246     for s in suites:
247         results=[]
248         # Setup a multiprocessing Pool. As many workers as we have CPU cores.
249         pool = Pool()
250         arch_list=get_suite_architectures(s.suite_name, skipsrc=False, skipall=False, session=session)
251         Logger.log(['generating output for Suite %s, Architectures %s' % (s.suite_name, map(sname, arch_list))])
252         for a in arch_list:
253             pool.apply_async(generate_packages_sources, (a.arch_string, s.suite_name, cnf["Dir::TempPath"]), callback=get_result)
254
255         # No more work will be added to our pool, close it and then wait for all to finish
256         pool.close()
257         pool.join()
258
259     if len(results) > 0:
260         Logger.log(['Trouble, something with a-f broke, resultcodes: %s' % (results)])
261         print "Trouble, something with a-f broke, resultcodes: %s" % (results)
262         sys.exit(1)
263
264     os.chdir(startdir)
265     # this script doesn't change the database
266     session.close()
267     Logger.close()
268
269 #######################################################################################
270
271 if __name__ == '__main__':
272     main()