]> git.decadent.org.uk Git - dak.git/blob - dak/generate_contents.py
54b70bdecba7d51a5025df3c342f65d6e9b9f837
[dak.git] / dak / generate_contents.py
1 #!/usr/bin/env python
2 # Create all the contents files
3
4 # Copyright (C) 2008, 2009 Michael Casadevall <mcasadevall@debian.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21 # <Ganneff> there is the idea to slowly replace contents files
22 # <Ganneff> with a new generation of such files.
23 # <Ganneff> having more info.
24 # <Ganneff> of course that wont help for now where we need to generate them :)
25 ################################################################################
26
27 ################################################################################
28
29 import sys, os, popen2, tempfile, stat, time, pg
30 import gzip, apt_pkg
31 from daklib import database, utils
32 from daklib.dak_exceptions import *
33
34 ################################################################################
35
36 Cnf = None
37 projectB = None
38 out = None
39 AptCnf = None
40
41 ################################################################################
42
43 def usage (exit_code=0):
44     print """Usage: dak generate-contents
45 Generate Contents files
46
47  -h, --help                 show this help and exit
48  -s, --suite=SUITE         only write file lists for this suite
49 """
50     sys.exit(exit_code)
51
52 ################################################################################
53
54 def generate_contents(suites):
55     global projectB, Cnf
56     # Ok, the contents information is in the database
57
58     # We need to work and get the contents, and print it out on a per
59     # architectual basis
60
61     # Read in the contents file header
62     header = False
63     if Cnf.has_key("Generate-Contents::Header"):
64         h = open(Cnf["Generate-Contents::Header"], "r")
65         header = h.read()
66         h.close()
67
68     # Get our suites, and the architectures
69     for s in suites:
70         suite_id = database.get_suite_id(s)
71
72         q = projectB.query("SELECT s.architecture, a.arch_string FROM suite_architectures s JOIN architecture a ON (s.architecture=a.id) WHERE suite = '%d'" % suite_id)
73
74         arch_list = [ ]
75         for r in q.getresult():
76             if r[1] != "source" and r[1] != "all":
77                 arch_list.append((r[0], r[1]))
78
79         arch_all_id = database.get_architecture_id("all")
80
81         # Time for the query from hell. Essentially, we need to get the assiocations, the filenames, the paths,
82         # and all that fun stuff from the database.
83
84         for arch_id in arch_list:
85             q = projectB.query("""SELECT p.path||'/'||n.file, comma_separated_list(s.section||'/'||b.package) FROM content_associations c JOIN content_file_paths p ON (c.filepath=p.id) JOIN content_file_names n ON (c.filename=n.id) JOIN binaries b ON (b.id=c.binary_pkg) JOIN bin_associations ba ON (b.id=ba.bin) JOIN override o ON (o.package=b.package) JOIN section s ON (s.id=o.section) WHERE (b.architecture = '%d' OR b.architecture = '%d') AND ba.suite = '%d' AND b.type = 'deb' GROUP BY (p.path||'/'||n.file)""" % (arch_id[0], arch_all_id, suite_id))
86
87             f = gzip.open(Cnf["Dir::Root"] + "dists/%s/Contents-%s.gz" % (s, arch_id[1]), "w")
88
89             if header:
90                 f.write(header)
91
92             for contents in q.getresult():
93                 f.write(contents[0] + "\t\t\t" + contents[-1] + "\n")
94
95             f.close()
96
97         # The MORE fun part. Ok, udebs need their own contents files, udeb, and udeb-nf (not-free)
98         # This is HORRIBLY debian specific :-/
99         # First off, udeb
100
101         section_id = database.get_section_id('debian-installer') # all udebs should be here)
102
103         if section_id != -1:
104             q = projectB.query("""SELECT p.path||'/'||n.file, comma_separated_list(s.section||'/'||b.package) FROM content_associations c JOIN content_file_paths p ON (c.filepath=p.id) JOIN content_file_names n ON (c.filename=n.id) JOIN binaries b ON (b.id=c.binary_pkg) JOIN bin_associations ba ON (b.id=ba.bin) JOIN override o ON (o.package=b.package) JOIN section s ON (s.id=o.section) WHERE s.id = '%d' AND ba.suite = '%d' AND b.type = 'udeb' GROUP BY (p.path||'/'||n.file)""" % (section_id, suite_id))
105
106             f = gzip.open(Cnf["Dir::Root"] + "dists/%s/Contents-udeb.gz" % (s), "w")
107
108             if header:
109                 f.write(header)
110
111             for contents in q.getresult():
112                 f.write(contents[0] + "\t\t\t" + contents[-1] + "\n")
113
114             f.close()
115
116         # Once more, with non-free
117         section_id = database.get_section_id('non-free/debian-installer') # all udebs should be here)
118
119         if section_id != -1:
120             q = projectB.query("""SELECT p.path||'/'||n.file, comma_separated_list(s.section||'/'||b.package) FROM content_associations c JOIN content_file_paths p ON (c.filepath=p.id) JOIN content_file_names n ON (c.filename=n.id) JOIN binaries b ON (b.id=c.binary_pkg) JOIN bin_associations ba ON (b.id=ba.bin) JOIN override o ON (o.package=b.package) JOIN section s ON (s.id=o.section) WHERE s.id = '%d' AND ba.suite = '%d' AND b.type = 'udeb' GROUP BY (p.path||'/'||n.file)""" % (section_id, suite_id))
121
122             f = gzip.open(Cnf["Dir::Root"] + "dists/%s/Contents-udeb-nf.gz" % (s), "w")
123
124             if header:
125                 f.write(header)
126
127             for contents in q.getresult():
128                 f.write(contents[0] + "\t\t\t" + contents[-1] + "\n")
129
130             f.close()
131
132 ################################################################################
133
134 def main ():
135     global Cnf, projectB, out
136     out = sys.stdout
137
138     Cnf = utils.get_conf()
139
140     Arguments = [('h',"help","Generate-Contents::Options::Help"),
141                  ('s',"suite","Generate-Contents::Options::Suite","HasArg"),
142                 ]
143
144     for i in [ "help", "suite" ]:
145         if not Cnf.has_key("Generate-Contents::Options::%s" % (i)):
146             Cnf["Generate-Contents::Options::%s" % (i)] = ""
147
148     suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
149     Options = Cnf.SubTree("Generate-Contents::Options")
150
151     if Options["Help"]:
152         usage()
153
154     if Options["Suite"]:
155         suites = utils.split_args(Options["Suite"])
156     else:
157         suites = Cnf.SubTree("Suite").List()
158
159     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
160     database.init(Cnf, projectB)
161
162     generate_contents(suites)
163
164 #######################################################################################
165
166 if __name__ == '__main__':
167     main()